Search the archives!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Advanced-java] Display combo box in JTable cell?
- From: terry.williams@xxxxxxxxxxxxx (Terry Williams)
- Subject: [Advanced-java] Display combo box in JTable cell?
- Date: Tue, 26 Feb 2002 12:20:25 +0000
Mark Plamann wrote:
> At 04:07 PM 2/25/2002 +0000, Greg Munt wrote:
>
> >Why would you want to render a combo box? A combo box as an editor, I can
> >understand.. But a renderer?
>
> You are correct. I am looking to write an editor for a combo box. I have
> come up with an editor that displays the combo box when the JTable cell is
> selected, but once the user clicks another cell, the combo box is no longer
> display, just the text of the option in the combo box that is selected.
>
> I apologize that my questions may be pretty basic for Swing gurus. I'm new
> to Swing and Applet programming, so this is a learning experience for me.
>
> Here is my code for the editor. If anyone can make suggestions how to just
> display the combo box and not the text of the selected combo box option, I
> would appreciate it. Also, if you see blatant stupidity, corrections are
> alway appreciated. :)
>
> public class RowEditor implements TableCellEditor {
>
> protected Hashtable editors;
> protected TableCellEditor editor, defaultEditor;
> JTable table;
>
> public RowEditor(JTable table) {
> this.table = table;
> editors = new Hashtable();
> defaultEditor = new DefaultCellEditor(new JTextField());
> }
>
> public void setEditorAt(int row, TableCellEditor editor) {
> editors.put(new Integer(row),editor);
> }
>
> public Component getTableCellEditorComponent(JTable table,
> Object value, boolean isSelected, int row, int column) {
> //editor = (TableCellEditor)editors.get(new Integer(row));
> //if (editor == null) {
> // editor = defaultEditor;
> //}
> return editor.getTableCellEditorComponent(table,
> value, isSelected, row, column);
> }
>
> public Object getCellEditorValue() {
> return editor.getCellEditorValue();
> }
> public boolean stopCellEditing() {
> return editor.stopCellEditing();
> }
> public void cancelCellEditing() {
> editor.cancelCellEditing();
> }
> public boolean isCellEditable(EventObject anEvent) {
> selectEditor((MouseEvent)anEvent);
> return editor.isCellEditable(anEvent);
> }
> public void addCellEditorListener(CellEditorListener l) {
> editor.addCellEditorListener(l);
> }
> public void removeCellEditorListener(CellEditorListener l) {
> editor.removeCellEditorListener(l);
> }
> public boolean shouldSelectCell(EventObject anEvent) {
> selectEditor((MouseEvent)anEvent);
> return editor.shouldSelectCell(anEvent);
> }
>
> protected void selectEditor(MouseEvent e) {
> int row;
> if (e == null) {
> row = table.getSelectionModel().getAnchorSelectionIndex();
> } else {
> row = table.rowAtPoint(e.getPoint());
> }
> editor = (TableCellEditor)editors.get(new Integer(row));
> if (editor == null) {
> editor = defaultEditor;
> }
> }
> }
>
> _______________________________________________
> Advanced-java mailing list
> Advanced-java@xxxxxxxxxxxxxxxxxxxxxx
> http://lists.xcf.berkeley.edu/mailman/listinfo/advanced-java
Hi,
this is roughly how you should do it. Obviously you should write a more
generic version of this, the coulmns are set expressly but anything you write
should register an Editor for a given Object type (I didn't have an example
lying around so I knocked one up - useful for when others ask exactly the same
question in 3 months time :-) )
Let me know if you have any further questions
Terry
--------------------------------------------------------------------
import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class TableTest extends JFrame
{
public TableTest()
{
super("Test");
this.getContentPane().setLayout(new BorderLayout());
Vector col1 = new Vector();
col1.add("red");
col1.add("white");
col1.add("blue");
Object[][] data = { { "terry", "red" }, { "sara", "white" } };
MyDataModel myData = new MyDataModel(data, new Object[] { "Name",
"Colour" } );
JTable t = new JTable(myData);
JScrollPane sp = new JScrollPane(t);
TableColumnModel model = t.getColumnModel();
TableColumn col = model.getColumn(1);
col.setCellEditor(new DefaultCellEditor(new JComboBox(col1)));
this.getContentPane().add(sp, BorderLayout.CENTER);
}
public class MyDataModel extends DefaultTableModel
{
public MyDataModel(Object[][] obj, Object[] names)
{
super(obj, names);
// this should have your proprly implement TableModel here
}
}
public static void main(String args[])
{
TableTest test = new TableTest();
test.setSize(400,400);
test.setVisible(true);
}
}
- References:
- [Advanced-java] Display combo box in JTable cell?
- From: Mark Plamann
- [Advanced-java] Display combo box in JTable cell?
- From: Mark Plamann
- [Advanced-java] Display combo box in JTable cell?
- Prev by Date: [Advanced-java] Display combo box in JTable cell?
- Next by Date: [Advanced-java] RMI - choices discussion
- Previous by thread: [Advanced-java] Display combo box in JTable cell?
- Next by thread: [Advanced-java] package for creation of graphs
- Index(es):