Hello all,
Thank you for taking the time to read my post and helping me.
I am trying to remove some JLabels and JCheckBox MenuItem that have been previously added to a JPanel, so it does not show anymore on the screen, but it is not working.
This is the current code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
public class Main extends JDialog {
private final String[] graphChoices = {"", "Graph", "Histogram"};
public Main() {
super();
setTitle("Define Elements");
add(createAllPanels(), BorderLayout.NORTH);
add(createButtons(), BorderLayout.SOUTH);
setResizable(false);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setVisible(true);
}
private JPanel createAllPanels() {
JPanel panel = new JPanel();
panel.setBorder(new EmptyBorder(10, 5, 10, 5));
this.setSize(400, 400);
panel.setLayout(new GridLayout(3, 2, 10, 10));
int size = 3 * 2;
for (int i = 0; i != size; ++i)
panel.add(createNewPanel(i));
return panel;
}
private JPanel createNewPanel(int index) {
final JPanel panel = new JPanel();
Border border1 = BorderFactory.createLineBorder(Color.BLACK);
Border border2 = new EmptyBorder(5, 5, 5, 5);
panel.setBorder(BorderFactory.createCompoundBorder(border1, border2));
panel.setLayout(new GridLayout(2, 1));
JComboBox chooseGraph = new JComboBox(graphChoices);
JPanel extraInfo = createExtraInfo(chooseGraph, index);
JPanel comboBox = createComboBoxPanel(chooseGraph, extraInfo);
panel.add(comboBox);
panel.add(extraInfo);
return panel;
}
private JPanel createComboBoxPanel(JComboBox chooseGraph, final JPanel extraInfo) {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, 0));
panel.add(new JLabel("Type:"));
class ChoosingGraph implements ActionListener {
public void actionPerformed(ActionEvent event) {
JComboBox temp = (JComboBox) event.getSource();
String graphName = (String) temp.getSelectedItem();
extraInfo.removeAll();
if (graphName.equals(graphChoices[1])) {
addGraphInfo(extraInfo);
} else if (graphName.equals(graphChoices[2])) {
addHistogramInfo(extraInfo);
}
extraInfo.revalidate();
}
}
chooseGraph.addActionListener(new ChoosingGraph());
panel.add(chooseGraph);
return panel;
}
private JPanel createExtraInfo(JComboBox chooseGraph, int index) {
JPanel panel = new JPanel();
if (index % 2 == 0) {
chooseGraph.setSelectedIndex(1);
addGraphInfo(panel);
} else if (index % 3 == 0) {
chooseGraph.setSelectedIndex(2);
addHistogramInfo(panel);
} else
chooseGraph.setSelectedIndex(0);
return panel;
}
private void addHistogramInfo(JPanel extraInfo) {
extraInfo.setLayout(new GridLayout(1, 0));
extraInfo.add(new JLabel("Merge:"));
JCheckBoxMenuItem merge = new JCheckBoxMenuItem();
extraInfo.add(merge);
extraInfo.add(new JLabel("Resize:"));
JCheckBoxMenuItem resize = new JCheckBoxMenuItem();
extraInfo.add(resize);
}
private void addGraphInfo(JPanel extraInfo) {
extraInfo.setLayout(new GridLayout(1, 0));
extraInfo.add(new JLabel("Load from file:"));
JCheckBoxMenuItem load = new JCheckBoxMenuItem();
extraInfo.add(load);
}
private JPanel createButtons() {
JPanel panel = new JPanel();
JButton ok = new JButton("OK");
JButton cancel = new JButton("Cancel");
panel.add(ok, BorderLayout.EAST);
panel.add(cancel, BorderLayout.WEST);
return panel;
}
public static void main(String[] args) {
new Main();
}
}
My problem is when you click on one of the JComboBox that is selected with "Graph" or "Histogram" (it currently displays some labels like "Merge" and "Resize"), and you click on the empty line/string, it does not remove them from the GUI.
Thank you very much for your help.