简体   繁体   English

JTextField无法正确显示

[英]JTextField Doesn't show properly

I'm making a application where you can manage the inventory of a store. 我正在制作一个应用程序,您可以在其中管理商店的库存。 I want to have the ability to change the current stock of certain items. 我希望能够更改某些物品的当前库存。 First you select the items you want to change from a JTable using checkboxes then you click a JButton which triggers an ActionEvent then a JOptionPane appears where you can input the new stock numbers. 首先,使用复选框从JTable选择要更改的项目,然后单击触发ActionEventJButton ,然后出现JOptionPane ,您可以在其中输入新的库存编号。

The problem is that ,depending on what you select, it doesn't show the proper info asbout the article and sometimes it doesn't even show the JTextField I use for the input 问题是,根据您选择的内容,它不会在文章中显示正确的信息,有时甚至不显示我用于输入的JTextField

Here is my Code: 这是我的代码:

    if (eventSource == bestelBrandstof) {
        ArrayList<Integer> brandstofTID = new ArrayList<Integer>();
        ArrayList<String> brandstofType = new ArrayList<String>();
        ArrayList<JTextField> aantallen = new ArrayList<JTextField>();

        for (int i = 0; i < modelBrandstof.getRowCount(); i++) {
            if ((boolean) modelBrandstof.getValueAt(i, 0)) {
                brandstofType.add((String) modelBrandstof.getValueAt(i, 1));
                brandstofTID.add(Integer.parseInt((String) modelBrandstof.getValueAt(i, 2)));
                aantallen.add(new JTextField("", 5));
            }
        }

        if (brandstofType.size() > 0) {

            bestellenBrandstof = new JPanel();
            bestellenBrandstof.setLayout(new FlowLayout());
            bestellenBrandstof.add(new JLabel("Hoeveel liter wilt u van de volgende brandstof(fen) bestellen?"));

            for (String a : brandstofType) {
                bestellenBrandstof.add(new JLabel(a + " " + brandstofTID.get(brandstofType.indexOf(a))));
                bestellenBrandstof.add(aantallen.get(brandstofType.indexOf(a)));
            }

            int n = JOptionPane.showConfirmDialog(null, bestellenBrandstof);

            if (n == JOptionPane.YES_OPTION) {
                boolean empty = false;

                for (JTextField a : aantallen) {
                    if (a.getText().equals(""))
                        empty = true;
                }

                if (empty == false) {
                    try {
                        SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
                        String datumVandaag = dateFormat.format(new Date());
                        FileWriter fw = new FileWriter("./bestellingen/Bestellen_Brandstof_" + datumVandaag + ".txt");
                        PrintWriter pw = new PrintWriter(fw);
                        for (Integer a : brandstofTID) {
                            pw.print("Nr: " + a.toString() + ", Type: " + brandstofType.get(brandstofTID.indexOf(a)) + ", Tankstation Identificatie: " + aantallen.get(brandstofTID.indexOf(a)).getText());
                            pw.print(System.lineSeparator());
                        }
                        pw.close();
                    } catch (IOException exc) {
                        exc.printStackTrace();
                    }
                    JOptionPane.showMessageDialog(null, "De Bestellijst in aangemaakt");
                } else {
                    JOptionPane.showMessageDialog(null, "Aantal Liters niet volledig ingevuld");
                }
            }
        } else {
            JOptionPane.showMessageDialog(null, "Selecteer onder het kopje 'Bestellen?' welke onderdelen u wilt bestellen");
        }
    }

Edit: Here is some similar code I wrote where it works properly 编辑:这是我写的一些类似的代码,它可以正常工作

    if (eventSource == bestelOnderdelen) {
        ArrayList<Integer> onderdeelNrs = new ArrayList<Integer>();
        ArrayList<String> onderdeelOmschrijving = new ArrayList<String>();
        ArrayList<JTextField> aantallen = new ArrayList<JTextField>();
        for (int i = 0; i < modelOnderdelen.getRowCount(); i++) {
            if ((boolean) modelOnderdelen.getValueAt(i, 0)) {
                onderdeelNrs.add(Integer.parseInt((String) modelOnderdelen.getValueAt(i, 1)));
                onderdeelOmschrijving.add((String) modelOnderdelen.getValueAt(i, 2));
                aantallen.add(new JTextField("", 5));
            }
        }

        if (onderdeelNrs.size() > 0) {

            bestellenOnderdelen = new JPanel();
            bestellenOnderdelen.setLayout(new FlowLayout());
            bestellenOnderdelen.add(new JLabel("Hoeveel wilt u van de volgende artikelen bestellen?"));

            for (Integer a : onderdeelNrs) {
                bestellenOnderdelen.add(new JLabel(Integer.toString(a) + " " + onderdeelOmschrijving.get(onderdeelNrs.indexOf(a))));
                bestellenOnderdelen.add(aantallen.get(onderdeelNrs.indexOf(a)));
            }

            int n = JOptionPane.showConfirmDialog(null, bestellenOnderdelen);

            if (n == JOptionPane.YES_OPTION) {
                boolean empty = false;

                for (JTextField a : aantallen) {
                    if (a.getText().equals(""))
                        empty = true;
                }

                if (empty == false) {
                    try {
                        SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
                        String datumVandaag = dateFormat.format(new Date());
                        FileWriter fw = new FileWriter("./bestellingen/Bestellen_Onderdelen_" + datumVandaag + ".txt");
                        PrintWriter pw = new PrintWriter(fw);
                        for (Integer a : onderdeelNrs) {
                            pw.print("Nr: " + a.toString() + ", Omschrijving: " + onderdeelOmschrijving.get(onderdeelNrs.indexOf(a)) + ", Aantal: " + aantallen.get(onderdeelNrs.indexOf(a)).getText());
                            pw.print(System.lineSeparator());
                        }
                        pw.close();
                    } catch (IOException exc) {
                        exc.printStackTrace();
                    }
                    JOptionPane.showMessageDialog(null, "De Bestellijst in aangemaakt");
                } else {
                    JOptionPane.showMessageDialog(null, "Aantallen niet volledig ingevuld");
                }
            }
        } else {
            JOptionPane.showMessageDialog(null, "Selecteer onder het kopje 'Bestellen?' welke onderdelen u wilt bestellen");
        }
    }

Edit: I added ArrayList<Integer> uniqueID = new ArrayList<Integer>(); 编辑:我添加了ArrayList<Integer> uniqueID = new ArrayList<Integer>(); and edited this part 并编辑了这部分

            for (Integer a : uniqueID) {
                bestellenBrandstof.add(new JLabel(brandstofType.get(uniqueID.indexOf(a)) + " " + brandstofTID.get(uniqueID.indexOf(a))));
                bestellenBrandstof.add(aantallen.get(uniqueID.indexOf(a)));
            }

I got it the issue is with below line 我明白了问题在于下面的线

bestellenBrandstof.add(aantallen.get(brandstofType.indexOf(a)));

If aantallen.get(brandstofType.indexOf(a)) returns same JTextField then it is not added in JPanel again hence some JTextField are not shown. 如果aantallen.get(brandstofType.indexOf(a))返回相同的JTextField ,则不会再次将其添加到JPanel ,因此不会显示某些JTextField

Please check the value of brandstofType and onderdeelNrs array. 请检查brandstofTypeonderdeelNrs数组的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM