I've a main GUI class from where I create 2 others JPanel (PanelA and PanelB for example). In the first panel there are a JComboBox (JCA) and a Jlabel (JLA) and in the second panel there are a JList (JLB) and a JLabel (JLB).
I'd like to have 2 listeners:
1- on JCA in order to display the item selected in JLB
2- on JLB in order to display the item selected in JLA

What should I implement?

Please help me

7 Years
Discussion Span
Last Post by javaAddict

Are the 2 panels declared in the same class. I mean are all your components visible in the same class. If yes, then:

Check the API for JList and see what type of listeners does it take. There should be a method add..listener... Check if there is a method in that interface that the description says that is executed when an item is selected at the list. Then have your Main GUI class implement that interface and add that listener to the JList:

class MainGUI extends JFrame implements ListenerInterface {


Then when you implement that method check the API for the argument, and try toy get the selected item. When you do display that item to the combo box by using its methods.


Thanks for your answer...but the 2 panels are not declared in the same class.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JPanel;

public class MainPanel extends JPanel {
	private static final long serialVersionUID = 1L;

	private FirstPanel panelA;
	private SecondPanel panelB;

	public MainPanel() {

		this.setLayout(new GridLayout(1, 2, 5, 5));
		this.setPreferredSize(new Dimension(500, 200));
		panelA= new FirstPanel();
		panelB= new SecondPanel();




Edited by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).


Then you can have the other 2 panel's constructors have as arguments the other panel:

class FirstPanel extends JPanel {
  private SecondPanel second;

   public FirstPanel(SecondPanel second) {
      this.second = second;

Do the same for the SecondPanel. Then the components of those panels (combo box, JList) can either be public or have get methods, so you can access them and modify their values

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.