Hey all, I've been trying to solve this issue I have now sometime but haven't had much luck with it. I want to be able to update a label in a JPanel from another JPanel EG panel1 has an actionListener that updates a JLabel in panel2.
I've been searching around but have not been able to solve my problem. I've got a feeling it's because I'm using a tabbed pane which is why I'm not unable to update it.
So here's my code.
This is the main tabbed frame.
import java.awt.*;
import java.awt.event.*;
public class LayoutDemo
{
public static void main (String[] args)
{
JFrame frame = new JFrame ("Layout Manager Demo");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
JTabbedPane tp = new JTabbedPane();
// tp.addTab("Intro", new IntroPanel());
// tp.addTab ("Nested", new NestedPanel());
// tp.addTab ("Border", new BorderPanel());
tp.addTab ("Test", new test());
tp.addTab ("Order", new orderDetails());
frame.add(tp);
frame.pack();
frame.setVisible(true);
}
}
This is the actionListener to 'update' the JLabel in the other JPanel. It's a comboBox which depending on the num selected will update accordingly.
private JComboBox sizeCB,CB2;
private String[] size = {"0","1","2","3","4","5"};
orderDetails d = new orderDetails();
sizeCB = new JComboBox(size);
sizeCB.setSelectedIndex(0);
sizeCB.addActionListener(new sizeListener());
private class sizeListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
int index = sizeCB.getSelectedIndex();
if(index==2)
d.headerLabel.setText("lol");
}
}
This is the JPanel that has the JLabel to be updated. orderDetails class.
JLabel headerLabel = new JLabel("STEP 1: Confirm your order");;
public JPanel header1() // headerPanel
{
JPanel headerPanel = new JPanel();
headerPanel.setLayout(new BorderLayout());
headerPanel.setBackground(Color.white);
headerPanel.setPreferredSize(new Dimension(400,20));
headerLabel.setFont(new Font("Arial",Font.BOLD,15));
headerPanel.add(headerLabel,BorderLayout.WEST);
return headerPanel;
}
Any help would be greatly appreciated. Thank you