-1

I am using JComboBox to display a String[] in my applet. Now I have a need in my project to populate the comboBox after some action performed in previous panel.

Here below is the code I have written:

JComboBox comboBox = new JComboBox(model);
comboBox.setMaximumRowCount(100);
comboBox.setEditable(true);
comboBox.setBounds(142, 196, 277, 20);

How to make this react accordingly to my action performed in previous panel?

Andrew Thompson
  • 166,747
  • 40
  • 210
  • 420
nanda kumar
  • 35
  • 1
  • 3
  • *"How to make this react accordingly to my action performed in previous panel?"* What have you tried? What is the expected behavior? What is the current behavior? General tips: 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). .. – Andrew Thompson May 27 '15 at 08:00
  • .. 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson May 27 '15 at 08:01
  • .. 3) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). – Andrew Thompson May 27 '15 at 08:02
  • Hi Andrew, I am trying to develop an desktop application which helps to fetch record count from db and save in an excel file. And now to retrive these counts from the file and display as a dropdown list. so I tried using DefaultComboBoxModel, but not sure how to work on it. – nanda kumar May 27 '15 at 08:08
  • *"..so I tried.."* Let the code do the talking, or specifically, an MCVE. Hard code some data to replace the DB. I still do not understand why the question mentions 'applet' if you're making a `desktop application`. A desktop application would not be embedded in a web page, but an applet would. – Andrew Thompson May 27 '15 at 08:15
  • Have you read the documentation at https://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html ??? – kazanaki May 27 '15 at 09:41

1 Answers1

1

When you change "model", the combo box view should automatically be updated (because a standard or well-writen ComboBoxModel would notify the view that it needs to change).

E.g. make "model" to be of type DefaultComboBoxModel, and then when performing that "action on that other pane" - make sure your code has access to "model", and change it. Naiively speaking this could be a direct access:

actionPerformed(...){ // action from other pane
    ... // some logic
    otherPane.model.removeAllElements();
    otherPan.model.addElement(...)
}

However, a nicer design would be less direct, e.g. your action could fire an event of your own (e.g "productsAddedToMyStore"), and the pane could listen to it and then update the model. This reduces coupling between compoenents.

Pelit Mamani
  • 2,201
  • 2
  • 12
  • 11