I want to update a JPanel according to the value of a ComboBox in another JPanel.
I have a JFrame in which I am adding two panels:
package gui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class MainFrame extends JFrame {
public MainFrame() {
}
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
frame.setSize(1200, 1200);
PanelClass1 panelClass1= new PanelClass1 ();
PanelClass2 panelClass2= new PanelClass2 ();
frame.getContentPane().add(panelClass1.contentPanel, BorderLayout.PAGE_START);
frame.getContentPane().add(panelClass2.contentPanel, BorderLayout.PAGE_END);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
Now in Panel1 I have a ComboBox in which depending on its value I need to hide or show fields in Panel2. I am saving the value of this ComboBox in a singleton.
public class PanelClass1 extends JPanel {
testComboBox = new JComboBox<ComboItem>();
testComboBox.setEditable(true);
testComboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
POJOSingleton.getInstance().setValor(testComboBox.getSelectedItem().toString());
checkFields();
}
});
}
Then in PanelClass2 I am reading that variable and then making some components visible or not according to the value. The whole logic of the panelClass2 is based on the value I set in the first JPanel.
public class panelClass2 extends JPanel {
public panelClass2 () {
if(POJOSingleton.getInstance().getValor() != null) {
int value1 = posicionesTenion.get(0);
pos1.setVisible(true);
lblPos1TensionAT.setVisible(true);
...
What should I do so when I change the ComboBox I trigger a refresh in my second panel?.