Ok so I have two JFrames. The first one is my main program. The second one is accessed by selecting an option on the menu of the first one. This second frame allows the user to enter values for different grades. For example, they are able to set the range of the grade "A" from 90 - 100 or from 85 - 100. Furthermore, they can add new grades like "A+" or "B-", and set ranges for them too.
If the user wants to add a grade, he uses the "Add Row" option on the menu titled "Actions". The frame prompts the user to enter the grade he wants to add (A+, or B-), he enters it and it should create a new row, with that grade, in the right place.
So there is where my problem begins. The headings and the grades are stored in an ArrayList of JLabels, and I wrote an algorithm to accurately predict the index of the new grade entered by the user ("B+" would be after "A-" and before "B"). So the entered grade is on the right position in my labels ArrayList. BUT for some reason, the new label doesn't add itself to my panel, and so cannot be seen by the user.
Some other information: 1. The panel uses grid bag layout - columns = 3 and rows = number of grades. 2. Each grade has two text fields, where the user enters the range value (I haven't added any functionality to the text fields, they're just there so I don't forget about them)
Here is the code:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Adjustments implements ActionListener {
private JMenuBar menubar; // for adding more grades
private ArrayList<JTextField> textFields;
private ArrayList<JLabel> labels;
private JPanel panel;
private JFrame frame;
private ArrayList<GridBagConstraints> gbcs;
private JButton button;
@Override
public void actionPerformed(ActionEvent e) {
frame = new JFrame();
frame.setSize(Frame.LENGTH, Frame.HEIGHT);
frame.setDefaultCloseOperation(1);
frame.setTitle("Adjustments");
createComponents();
frame.setVisible(true);
}
private void createComponents() {
panel = new JPanel();
panel.setLayout(new GridBagLayout());
createLabels();
createTextFields();
createMenuBar();
createLayout();
frame.setJMenuBar(menubar);
}
private void createLayout() {
// This creates the layout. First makes the constraints, then adds everything to the panel
gbcs = new ArrayList<GridBagConstraints>();
for (int i = 0; i < labels.size() - 2; i++) {
for (int j = 0; j < 3; j++) {
gbcs.add(new GridBagConstraints());
gbcs.get((i * 3) + j).gridx = 100 + (100 * j);
gbcs.get((i * 3) + j).gridy = 100 + (100 * i);
}
}
// This is for the three headings on the top - "Grade", "From" and "To"
for (int i = 0; i < 3; i++) {
panel.add(labels.get(i), gbcs.get(i));
}
int l = 3, tf = 0; // l is the labels count, tf is for text fields
for (int i = 3; i < gbcs.size(); i++) {
if (i % 3 == 0) {
panel.add(labels.get(l), gbcs.get(i));
l++;
} else {
panel.add(textFields.get(tf), gbcs.get(i));
tf++;
}
}
frame.add(panel);
}
private void createLabels() {
// all the labels are made
labels = new ArrayList<JLabel>();
labels.add(new JLabel("Grade"));
labels.add(new JLabel("From"));
labels.add(new JLabel("To"));
labels.add(new JLabel("A"));
labels.add(new JLabel("B"));
// labels.add(new JLabel("B-"));
labels.add(new JLabel("C"));
labels.add(new JLabel("D"));
}
private void createTextFields() {
// there are two text fields on each row, starting from the 2nd
textFields = new ArrayList<JTextField>();
for (int i = 0; i < 8; i++) {
textFields.add(new JTextField(5));
}
}
private void createMenuBar() {
menubar = new JMenuBar();
// create and add the menu/menu items to the menubar
JMenu actions = new JMenu("Action");
JMenuItem addrow = new JMenuItem("Add Row");
actions.add(addrow);
class Listener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String newGrade = JOptionPane.showInputDialog(frame, "What grade is this?");
if (newGrade.length() == 2) { // e.g.: ("B+")
String s = newGrade.substring(0, 1);
int index = 0; // index is the location of grade in the labels ArrayList
for (int i = 3; i < labels.size(); i++) {
if (s.equals(labels.get(i).getText())) {
index = i;
break;
}
}
String s2 = newGrade.substring(1);
if (s2.equals("-")) index++;
labels.add(index, new JLabel(newGrade));
textFields.add(new JTextField(5));
textFields.add(new JTextField(5));
}
panel.removeAll(); // removes everything from the panel to be readied again
createLayout(); // creates the layout everything
}
}
addrow.addActionListener(new Listener());
menubar.add(actions);
}
}
Why won't the new grades display on my frame? Thanks for your help!