• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JcomboBox vanishing occasionally

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
My problem is like this:
I have a panel which uses a combobox as a cell editor for a Jtable.The combobox is uneditable. There is a button in the panel which opens up a dialog which again shows the same combobx. I am passing the same combobox instance to the dialog. Now the problem is when I edit the cell in the table (in the panel) and then click to open the dialog, the combobox disappears in the dialog. But if I do not touch the table cells and just click to open the dialog, it shows the combobox properly.

Following is the code of the panel:

public final class MyPanel extends JPanel implements ActionListener, IDataHandler, MouseListener
{
private final JComboBox myComboBox = new JComboBox();
private final JButton m_addButton = new JButton("Add");
private final TableModel m_tableModel = new TableModel();
private final JTable m_table;
private final IClientContext m_ctx;

public MyPanel(IClientContext ctx)
{
m_table = m_ctx.getSwingContext().createTableFor(m_tableModel,
"myConfig");
m_table.getColumnModel().getColumn(TableModel.BID_SHADE).setCellEditor(new DefaultCellEditor(m_bidComboBox));
}
}
 
Mous Bhatta
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry. My previous post is incomplete. I am posting the entire thing this time.

Hi,
My problem is like this:
I have a panel which uses a combobox as a cell editor for a Jtable.The combobox is uneditable. There is a button in the

panel which opens up a dialog which again shows the same combobx. I am passing the same combobox instance to the dialog.

Now the problem is when I edit the cell in the table (in the panel) and then click to open the dialog, the combobox

disappears in the dialog. But if I do not touch the table cells and just click to open the dialog, it shows the combobox

properly.

Following is the code of the panel:

public final class MyPanel extends JPanel implements ActionListener, IDataHandler, MouseListener
{
private final JComboBox myComboBox = new JComboBox();
private final JButton m_addButton = new JButton("Add");
private final TableModel m_tableModel = new TableModel();
private final JTable m_table;
private final JPopupMenu m_popupMenu = new JPopupMenu();
private final JMenuItem m_resetPair;
private final IClientContext m_ctx;

public MyPanel(IClientContext ctx)
{
m_table = m_ctx.getSwingContext().createTableFor(m_tableModel,
"myConfig");

m_table.getColumnModel().getColumn(TableModel.COLUMN1).setCellEditor(new DefaultCellEditor(myComboBox));

JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
buttonPane.add(m_addButton);

setLayout(new BorderLayout());
add(new JScrollPane(m_table), BorderLayout.CENTER);
add(buttonPane, BorderLayout.SOUTH);

for (int i = 0; i < 21; i++)
{
double f = 0.0001 - (0.00001 * i);
myComboBox.addItem(f);
}

m_resetPair = new JMenuItem("Reset All Shades for Selected Pairs");
m_resetPair.addActionListener(this);
m_popupMenu.add(m_resetPair);
m_addDialog = new MyDialog(myComboBox);
}

public void actionPerformed(ActionEvent e)
{
if (e.getSource() == m_resetPair)
{
//do some databaseOperation
}
else if ("add".equalsIgnoreCase(e.getActionCommand()))
{
m_addDialog.reset();
m_addDialog.setLocationRelativeTo(this);
m_addDialog.setVisible(true);
}

}

public void mouseClicked(MouseEvent e)
{
}

public void mousePressed(MouseEvent e)
{
maybeShowPopup(e);
}

public void mouseReleased(MouseEvent e)
{
maybeShowPopup(e);
}

public void mouseEntered(MouseEvent e)
{

}

public void mouseExited(MouseEvent e)
{
}

private void maybeShowPopup(MouseEvent e)
{
if (e.isPopupTrigger())
{
m_popupMenu.show(e.getComponent(), e.getX(), e.getY());
}
}

private final class TableModel extends AbstractTableModel implements ITableCellColorModel
{

static final int COLUMN1= 0;
private final String[] COLUMN_NAMES = { "My Column"};
private final Class[] COLUMN_CLASSES = { String.class};

public boolean isCellEditable(int row, int column)
{
return true;
}

public void setValueAt(Object value, int row, int column)
{
String myval =null;

switch (column)
{
case ID:
myval = (String) value;
break;
}
}

}

}

The code for the dialog:

final class MyDialog extends JDialog implements ActionListener
{
private final JComboBox comboBox;

MyDialog(JComboBox box)
{
comboBox=box;
addTextRows(new JLabel[] {"Label1"}, new JComponent[] { comboBox},(GridBagLayout) pane.getLayout(), pane, 10);
}

public void reset()
{
double f=0.0;
comboBox.setSelectedItem(f);
}

private static void addTextRows(JComponent[] components, JComponent[] textFields, GridBagLayout gridbag, Container
container, int left)
{
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.WEST;
for (int i = 0; i < components.length; i++)
{
c.insets = new Insets(5, left, 5, 5);
c.gridwidth = GridBagConstraints.RELATIVE;
c.fill = GridBagConstraints.NONE;
c.weightx = 0.0; //reset to default
gridbag.setConstraints(components[i], c);
container.add(components[i]);

c.insets = new Insets(5, 0, 5, 5);
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
gridbag.setConstraints(textFields[i], c);
container.add(textFields[i]);
}
}

public void actionPerformed(ActionEvent e)
{

if ("close".equalsIgnoreCase(e.getActionCommand()))
{
setVisible(false);
}
}
}
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mous Bhatta:
uses a combobox as a cell editor for a Jtable. The combobox is uneditable. There is a button in the panel which opens up a dialog which again shows the same combobx. I am passing the same combobox instance to the dialog.



You can't add any component instance to more than one container. That's why adding it to the dialog causes it to disappear from where it had been.

If you like, you can create a new JComboBox instance that uses the same ComboBoxModel and add that to your dialog:
JComboBox otherComboBox = new JComboBox(firstComboBox.getModel());

[edit: PS. You could have edited your first post instead of reposting.]
[ January 23, 2008: Message edited by: Brian Cole ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic