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

Way back in LayoutDemo, when you create the two panels, you can send the Test instance a reference to the to OrderDetails instance (use a public set... method in Test that accepts an instance of OrderDetails as parameter and stores a copy of it.
Now your Test instance can access non-private variables or (better) non-private methods in the OrderDetails instance.
This is a quick fix for this simple case, but it won't scale well to multiple panels.
In real life you would do better to separate model and view classes (like things such as JTable do). The view (GUI) classes just update the model, and all the other views listen for changes in the model. That way each GUI class just needs a reference to the model, they don't need to communicate directly. At larger scales/complexities the full MVC (model view controller) pattern comes in, with a dedicated controller class that manages all the views and their interactions both with the model and each other.

Thanks for the input, still don't quite understand that but will work on that. But anyways I just wanted to know why my previous methods wouldn't work.

also try to extends ur current class(subclass), with the class concerned(superclass), and see if you cannot solve it,

i suggest you research much on inheritence.

Thanks for the input, still don't quite understand that but will work on that. But anyways I just wanted to know why my previous methods wouldn't work.

OK, ignore evrything after "In real life ". The quickest answer is in the first part of my post.
In brief to change an <anything> in <another class> you need to do the following:
Add a public method to <another class> that makes the desired change (you may need to pass it a parameter to specify what to display or whatever).
Make sure the original class has a reference to the instance of <another class> so it can call the public method. See original post for a bit more detail

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.