I have the following program :
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class Java_Test extends JPanel
{
static JTabbedPane Tabbed_Pane=new JTabbedPane();
JComboBox Contacts_ComboBox;
Customer_Info_Panel A_Customer_Info_Panel=new Customer_Info_Panel();
Java_Test()
{
JPanel A_Tab_Panel=new JPanel();
A_Tab_Panel.setPreferredSize(new Dimension(300,200));
A_Tab_Panel.add(A_Customer_Info_Panel);
JPanel B_Tab_Panel=new JPanel();
B_Tab_Panel.setPreferredSize(new Dimension(300,200));
Contacts_ComboBox=new JComboBox(A_Customer_Info_Panel.Contacts_Id_Vector);
// Contacts_ComboBox=new JComboBox(A_Customer_Info_Panel.Contacts_Id_ComboBox.getModel());
Contacts_ComboBox.setMaximumRowCount(30);
Contacts_ComboBox.setSelectedIndex(-1);
Contacts_ComboBox.setPreferredSize(new Dimension(129,22));
B_Tab_Panel.add(new JLabel("Select Customer Info From List :"));
B_Tab_Panel.add(Contacts_ComboBox);
Tabbed_Pane.addTab("A : Enter Customer Info",null,A_Tab_Panel,null);
Tabbed_Pane.addTab("B : Use Entered Info",null,B_Tab_Panel,null);
add(Tabbed_Pane);
setPreferredSize(new Dimension(500,300));
}
static void Out(String message) { System.out.println(message); }
public static void main(String[] args)
{
final Java_Test demo=new Java_Test();
Dimension Screen_Size=Toolkit.getDefaultToolkit().getScreenSize();
final JFrame frame=new JFrame("JComboBox Test");
frame.add(demo);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e) { System.exit(0); }
public void windowDeiconified(WindowEvent e) { demo.repaint(); }
public void windowGainedFocus(WindowEvent e) { demo.repaint(); }
public void windowOpening(WindowEvent e) { demo.repaint(); }
public void windowResized(WindowEvent e) { demo.repaint(); }
public void windowStateChanged(WindowEvent e) { demo.repaint(); }
});
frame.pack();
frame.setBounds((Screen_Size.width-demo.getWidth())/2,(Screen_Size.height-demo.getHeight())/2-10,demo.getWidth(),demo.getHeight()+38);
frame.setVisible(true);
}
}
class Customer_Info_Panel extends JPanel
{
Vector<String> Contacts_Id_Vector=new Vector<String>();
JComboBox Contacts_Id_ComboBox=new JComboBox(Contacts_Id_Vector);
Customer_Info_Panel()
{
Contacts_Id_ComboBox=new JComboBox(Contacts_Id_Vector);
Contacts_Id_ComboBox.setMaximumRowCount(30);
Contacts_Id_ComboBox.setSelectedIndex(-1);
Contacts_Id_ComboBox.setEditable(true);
Contacts_Id_ComboBox.setPreferredSize(new Dimension(129,22));
Contacts_Id_ComboBox.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
String New_Item=Contacts_Id_ComboBox.getSelectedItem()==null?"":Contacts_Id_ComboBox.getSelectedItem().toString().trim();
if (!Contacts_Id_Vector.contains(New_Item)) Contacts_Id_Vector.add(New_Item);
// Contacts_Id_ComboBox.firePopupMenuWillBecomeVisible();
}
});
add(new JLabel("Please Enter Customer Info Below :"));
add(Contacts_Id_ComboBox);
setPreferredSize(new Dimension(270,170));
}
}
It complies and demonstrates the problem detailed below :
<1> In Tab A, enter "123" into the JComboBox and hit the "Enter" key, now I have one item in the drop down list
<2> In Tab A again, enter "ABC" and hit the "Enter" key again, now I have both "123" and "ABC" in Tab A's drop down list
<3> Now in Tab B you can see both items you entered in Tab A, try select one of them, it seems to work fine
<4> Goto Tab A and enter "111" and hit the "Enter" key, now if you click on the drop down list, they are all gone, you can't see any of them, but if you look at Tab B, they are all there, sometimes Tab B shows an empty list, but if you click in the empty list, they will show up correctly. But in Tab A no matter how you click, you can't see any item.
I tried to enter all three items in Tab A one after another : 123[Enter key] + ABC[Enter key] + 111[Enter key] then look them up in Tab B, they are all there, but if I go back to Tab A and try to enter "333"+[Enter key], all items in A will be gone again.
I found this problem a year ago, but I am now running Java 1.6.0_03 on Windows XP, the problem is still there.
Maybe it's my program's problem, but how to fix it ? I need a JComboBox in Tab A that user can enter info and in Tab B I need another JComboBox that user can use the entered info from Tab A, any time the info in Tab A gets changed, it needs to be reflected in Tab B's JComboBox, am I doing it the wrong way ? If so, what's the right way ?
The above program will compile and run to show you the problem.
Frank