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);
}
}
}