0

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.

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

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>(); 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)));
            }
Liam de Haas
  • 1,258
  • 3
  • 18
  • 39
  • 1
    Beware, of the table is sorted in some way, then using getValueAt of the model may not return the same value on the screen? You may need to use JTable#convertRowIndexToModel – MadProgrammer Mar 17 '14 at 20:21
  • @MadProgrammer thought about that, but I get if the item is selected via a checkbox in the model and via the view. – Liam de Haas Mar 17 '14 at 20:25
  • Where is the problem: 1. Is it returning always `false` by using `getValueAt(i,0)`? 2. Is there some issue in rendering `MessageDialog`? – Braj Mar 17 '14 at 20:32
  • Is Label *Hoeveel liter wilt u van de volgende brandstof(fen) bestellen?* displayed on `Dialog` without `JTextField`? or an empty `Dialog` is displayed? – Braj Mar 17 '14 at 20:35
  • the issue is in the rendering of the `MessageDialog`, depending on what items I select is renders properly and other times it doesn't render properly – Liam de Haas Mar 17 '14 at 20:36
  • Displayed in the dialog without JTextField. i have another part of code where it works beautifullt, I will add it – Liam de Haas Mar 17 '14 at 20:38
  • There is a compilation error in your code at `if ((boolean) modelBrandstof.getValueAt(i, 0))` that says *Cannot cast from Object to boolean* – Braj Mar 17 '14 at 20:39
  • @Braj I've set the Class of the row to boolean by overriding `getColumnClass` – Liam de Haas Mar 17 '14 at 20:41
  • OK, It was not mentioned in the code. Is it something `Layout` issue? – Braj Mar 17 '14 at 20:42
  • @Braj it should look like this [link](https://imageshack.com/i/jj6lr6p) with a textfield next to each "brandstof" but sometimes it shows this [link](https://imageshack.com/i/e9ppp5p) – Liam de Haas Mar 17 '14 at 20:49
  • If you get the selected index from the table, the index will be in the context of the view, not the model – MadProgrammer Mar 17 '14 at 20:52
  • @MadProgrammer I know, but I dont select normal for a JTable. i have schackboxes where you select what you want. – Liam de Haas Mar 17 '14 at 21:03
  • there simply not enough context to accurately diagnose the issue, if it some times woks and sometimes doesn't it could be a multitude of issues which are available in the code snippet you've provided – MadProgrammer Mar 17 '14 at 21:05
  • @MadProgrammer [PasteBin](http://pastebin.com/u9tae1Qe) here is my entire class – Liam de Haas Mar 17 '14 at 21:08

1 Answers1

0

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.

Please check the value of brandstofType and onderdeelNrs array.

Braj
  • 46,415
  • 5
  • 60
  • 76
  • This may be it, because I have multiples of the same String in `brandstofType – Liam de Haas Mar 17 '14 at 21:03
  • You can't add same component again in `FlowLayout`. Why are you accessing it from `List.get(index)` where index may be repeatable. It will be dropped next time. Hence you will see just `Label` without any `JTextField` – Braj Mar 17 '14 at 21:06
  • Use some unique key/value pair to store `JTextField` such as `HashMap` – Braj Mar 17 '14 at 21:07
  • Assign a unique ID to each row and use it as `index` to get it from `List`. – Braj Mar 17 '14 at 21:10