Search the archives!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Advanced-java] Display combo box in JTable cell?
- From: greg@xxxxxxxxxxxxxxx (Greg Munt)
- Subject: [Advanced-java] Display combo box in JTable cell?
- Date: Tue, 26 Feb 2002 10:44:51 -0000
Okay.
In Swing, editor components are only displayed whilst editing a cell. At all
other times, the cell renderer is used. If you must have the appearance of a
combo box at all times, you'd need to write a renderer that extended
JComboBox, in addition to the editor. I personally find this kind of 'look'
distasteful, though.
----- Original Message -----
From: "Mark Plamann" <mark@xxxxxxxxxxx>
To: "Greg Munt" <greg@xxxxxxxxxxxxxxx>;
<advanced-java@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Monday, February 25, 2002 4:32 PM
Subject: Re: [Advanced-java] Display combo box in JTable cell?
> 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
>
- 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] Reading .doc document
- Next by Date: [Advanced-java] Display combo box in JTable cell?
- Previous by thread: [Advanced-java] Display combo box in JTable cell?
- Next by thread: [Advanced-java] Display combo box in JTable cell?
- Index(es):