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)));
}
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.