How can effect data of one combobox to another which are available in two different classes in swing?

I have two combo boxes in two different classes. My requirement is that if I change the data in one class that data should be displayed in other class. Are there any listeners or some other way? Can u help me?

Answers


You can use Object of the second class and then access textfield of that class set the value

Sample

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ComboBox{
  JComboBox combo;
  JTextField txt;
  static ComboBox b1 = null;
  static ComboBox b2 = null;
  public static void main(String[] args) {
  b1 = new ComboBox();
  b2 = new ComboBox();
  }

  public ComboBox(){
  String course[] = {"BCA","MCA","PPC","CIC"};
  JFrame frame = new JFrame("Creating a JComboBox Component");
  JPanel panel = new JPanel();
  combo = new JComboBox(course);
  combo.setBackground(Color.gray);
  combo.setForeground(Color.red);
  txt = new JTextField(10);
  panel.add(combo);
  panel.add(txt);
  frame.add(panel);
  combo.addItemListener(new ItemListener(){
  public void itemStateChanged(ItemEvent ie){
  String str = (String)combo.getSelectedItem();
  b1.txt.setText(str);
  b2.txt.setText(str);
  }
  });
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setSize(400,400);
  frame.setVisible(true);
  }
}

Although this is a GUI program, the principles of Java OOP practices continue to remain unchanged. You give one class public setter method(s) to allow other classes to change its properties, and you allow other classes (usually the "control" classes such as the class listening to the first JComboBox) call these methods by calling the setter methods of an instance of the first class.


You should learn the basics of the model-view-controller principle, often abbreviated to "MVC". A combo box consists of

  • a view (the visual representation),
  • a controller (clicking it open, selecting an entry) and
  • a model (in the case of a JComboBox, it's a ComboBoxModel).

For your purposes, it sounds like what you wish to do is either

  • share a model among two combo boxes or
  • add an ActionListener and/or ItemListener to a combo box that will notify the other one of changes.

Mind that a shared model would also imply that what gets selected in one box, gets selected in the other. The model encapsulates selection status as well as the item list. One way to get around this would be to have a sort of model in a model. Both combo boxes could have their own ComboBoxModel with its own selection state and such, but sharing an underlying secondary model for the item list. Updates to the item list would then become visible to both. This, however, implies that you implement ComboBoxModel yourself or subclass DefaultComboBoxModel in a way that overrides the default behaviour.

I suggest you check the Swing tutorial as well as reading up on using the model-view-controller pattern.


Need Your Help

How to avoid the ssh username prompt if only ssh key is permitted

linux ssh

I've configured a remote debian to only accept ssh login with key for a determined user, which will only use one local machine to access it.

What is the 'best' way to do distributed transactions across multiple databases using Spring and Hibernate

java hibernate spring transactions xa

I have an application - more like a utility - that sits in a corner and updates two different databases periodically.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.