Updating JComboBox items in JTabbedPane

Hi,

I'm currently working on a quite simple applications.

It has a JTabbedPane with 4 panels embedded, one of each with various swing's contents.

Every panel shares data to others by a .dat file on hard disk.

The question:

Panel 3 has a JComboBox, its items (choices) are captured from the data file produced by panel 1.

When I modify the data file on Panel1, the JComboBox on Panel3 doesn't update its contents (items) automatically with the new values, although the file read by it for settings its items has new values.

I'd like to have new values inserted automatically on panel3's JComboBox ,when I switch to panel1 to panel3.

How to make this? maybe I have to force update on every panel when switching?

This is the code I use for initializing the tabbed Panel structure

privatevoid jbInit()throws Exception{

contentPane = (JPanel) getContentPane();

contentPane.setLayout(borderLayout1);

this.setResizable(true);

setSize(new Dimension(800, 600));

JPanel panel1 =new JPanel();

tabbedPane.addTab("P1", panel1);

JPanel panel2 =new JPanel();

tabbedPane.addTab("P2", panel2);

JPanel panel3 =new JPanel();

tabbedPane.addTab("P3", panel3);

JPanel panel4 =new JPanel();

tabbedPane.addTab("P4", panel4);

contentPane.add(tabbedPane, java.awt.BorderLayout.CENTER);

}

thanks for help

[1896 byte] By [BenNJa] at [2007-10-2 11:39:06]
# 1

You could add a change listener to be fired each time tab selection changes and then call some init method.

public interface Refreshable{

void refresh();

}

>private void jbInit() throws Exception {

> contentPane = (JPanel) getContentPane();

> contentPane.setLayout(borderLayout1);

> this.setResizable(true);

> setSize(new Dimension(800, 600));

>

> JPanel panel1 = new JPanel();

> tabbedPane.addTab("P1", panel1);

>

> JPanel panel2 = new JPanel();

> tabbedPane.addTab("P2", panel2);

>

> JPanel panel3 = new JPanel();

> tabbedPane.addTab("P3", panel3);

>

> JPanel panel4 = new JPanel();

> tabbedPane.addTab("P4", panel4);

>

tabbedPane.addChangeListener(new javax.swing.event.ChangeListener() {

public void stateChanged(javax.swing.event.ChangeEvent e) {

((Refreshable)tabbedPane.getSelectedComponent()).refresh();

}

});

> contentPane.add(tabbedPane,

> bedPane, java.awt.BorderLayout.CENTER);

>}

BaltimoreJohna at 2007-7-13 5:26:59 > top of Java-index,Desktop,Core GUI APIs...
# 2
I would recommend putting some sort of a signal in so that you don't reload the file when it has not been updated.
BaltimoreJohna at 2007-7-13 5:26:59 > top of Java-index,Desktop,Core GUI APIs...
# 3

thanks!

but the code you have posted give me a ClassCastException on

try {

((Refreshable) tabbedPane.

getSelectedComponent()).refresh();

} catch (RefreshFailedException ex) {

}

BenNJa at 2007-7-13 5:26:59 > top of Java-index,Desktop,Core GUI APIs...
# 4

Right. I was thinking that each panel was a seperate class in which you would implement an interface so that you could call the interface method of any of your panels by doing the cast to the interface. So...

public class Panel1 implements Refreshable{

public void refresh(){

// your code here

}

}

etc

etc

You could of course use some other method of telling each panel to refresh itself.

John

BaltimoreJohna at 2007-7-13 5:26:59 > top of Java-index,Desktop,Core GUI APIs...
# 5

> the JComboBox on Panel3 doesn't update its contents (items) automatically

> with the new values, although the file read by it for settings its items has new

> values.

this is not MS Excel. There is no automatic re-calculation.

you loaded the combo with data, that's it, that's the data it displays.

to change it, when you change the data in the file, create a new

DefaultComboBoxModel() from the data in the (now new) file, then call

combo.setModel(the_new_DefaultComboBoxModel)

Michael_Dunna at 2007-7-13 5:26:59 > top of Java-index,Desktop,Core GUI APIs...
# 6

unfortunately it doesn't works... I tried implementing refresh() in every panel, but when switching the comboBox doesn't update.

I did:

public void refresh() throws RefreshFailedException {

try {

jbInit();

} catch (Exception ex) {

}

}

so the jbInit() method re-create the panel with the comboBox.

I debugged the code and all seems to be ok: when switching the refreshed panel re-init and read the new file with new items, inserting them in the comboBox.

But into the comboBox it doesn't display the new values!! There are only the OLD values, read into the old file.

I'm going mad!!

I also tried the comboBoxModel, but nothing to do.

BenNJa at 2007-7-13 5:26:59 > top of Java-index,Desktop,Core GUI APIs...
# 7
maybe the OLD panel is set in first level, and the new panel rest in secondo level (not visible).It's very strange, because debugging the code all the variables and component are OK and setted with new values
BenNJa at 2007-7-13 5:26:59 > top of Java-index,Desktop,Core GUI APIs...
# 8

You shouldn't call the init method that recreates your components. You should just update the combobox model with the new data. Something like this..

JComboBox myCombo;

private void jbInit () {

// creates all the components here

myCombo = new JComboBox(new DefaultComboBoxModel());

updateComboBoxModel();

}

private void updateComboBoxModel (){

DefaultComboBoxModel model = (DefaultComboBoxModel)myCombo.getModel();

model.removeAllElements();

// get your elements from the file

// add your elements to the model

model.addElement(element1);

...

...

}

private void refresh () {

updateComboBoxModel();

}

BaltimoreJohna at 2007-7-13 5:26:59 > top of Java-index,Desktop,Core GUI APIs...
# 9

> I also tried the comboBoxModel, but nothing to do.

here's a simple demo

'linesFromFile' represents the file contents

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class Testing extends JFrame

{

String[] linesFromFile = {"A","B","C","D","E"};

JComboBox cbo = new JComboBox(linesFromFile);

public Testing()

{

setSize(150,75);

setLocation(400,300);

setDefaultCloseOperation(EXIT_ON_CLOSE);

JPanel jp = new JPanel(new GridLayout(2,1));

JButton btn = new JButton("Change Data");

jp.add(cbo);

jp.add(btn);

getContentPane().add(jp);

btn.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent ae){

linesFromFile = new String[]{"1","2","3","4","5"};

DefaultComboBoxModel newModel = new DefaultComboBoxModel(linesFromFile);

cbo.setModel(newModel);}});

}

public static void main(String args[]){new Testing().setVisible(true);}

}

Michael_Dunna at 2007-7-13 5:26:59 > top of Java-index,Desktop,Core GUI APIs...
# 10
it works!Thank BaltimoreJohn , and thanks Michael too.
BenNJa at 2007-7-13 5:26:59 > top of Java-index,Desktop,Core GUI APIs...