Using JComboBoxes in a JT
To: comp.lang.java.gui
I am trying to make a list of JComboBoxes. My first try was to put the
JComboBoxes in a JList but my solution did never really work. I
managed to get them in the JList, but I could not interact with the
JComboBoxes.
My current solution for getting a list of JComboBoxes is to put them
in a JTable. As before I manage to get them in there, but they dont
behave as they should. In this solution I can interact with the
JComboBoxes but the problem is that when I select a value from one
JComboBox, then the JComboBoxes that I have not interacted with all
get that value selected in their JComboBoxes too.
In the sample below I have 4 JComboBoxes, here is the code:
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
public class NewJPanel extends javax.swing.JPanel {
public class MyComboBoxRenderer extends JComboBox implements
TableCellRenderer {
public MyComboBoxRenderer(String[] items) {
super(items);
}
public java.awt.Component getTableCellRendererComponent(JTable
table, Object value,
boolean isSelected, boolean hasFocus, int row, int
column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
// Select the current value
setSelectedItem(value);
return this;
}
}
/**
* Auto-generated main method to display this
* JPanel inside a new JFrame.
*/
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new NewJPanel());
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public NewJPanel() {
super();
initGUI();
}
private void initGUI() {
try {
this.setLayout(new FlowLayout());
setPreferredSize(new Dimension(400, 300));
JTable table = new JTable();
DefaultTableModel model = (DefaultTableModel)table.getModel();
// Add some columns
model.addColumn("A");
model.addRow(new String[]{""});
model.addRow(new String[]{""});
model.addRow(new String[]{""});
model.addRow(new String[]{""});
// These are the combobox values
String[] values = new String[]{"item1", "item2", "item3"};
// Set the combobox editor on the 1st visible column
int vColIndex = 0;
TableColumn col = table.getColumnModel().getColumn(vColIndex);
col.setCellEditor(new DefaultCellEditor(new JComboBox(values)));
// If the cell should appear like a combobox in its
// non-editing state, also set the combobox renderer
col.setCellRenderer(new MyComboBoxRenderer(values));
add (table);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Can someone tell me how I could correct my code to make it work?
Or have someone incorporated JComboBoxes in a JList or in a JTable and
feel like sharing the code I would be very happy.
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24