|
Replies:
17
-
Last Post:
Dec 13, 2008 6:40 AM
by: kschaefe
|
|
|
|
|
|
|
Problem with auto complete combobox and JTable
Posted:
Nov 16, 2008 7:58 AM
|
|
|
I use table.setSurrendersFocusOnKeystroke(true);
To allow user to start typing in the cell of the JTable. The cell editor is a combobox with auto complete. The problem I am seeing is that when the editor comes on, the existing value shows up instead of a value matching the first character typed. How do I achieve this ?
|
|
|
|
|
|
|
Re: Problem with auto complete combobox and JTable
Posted:
Nov 17, 2008 5:59 AM
in response to: navinkjha
|
|
|
Are you using AutoCompleteComboBoxEditor? Can you please provide a small, runnable demo?
Karl
|
|
|
|
|
|
|
|
Re: Problem with auto complete combobox and JTable
Posted:
Nov 17, 2008 6:22 AM
in response to: kschaefe
|
|
|
Karl,
I am extending ComboBoxCellEditor and then: AutoCompleteDecorator.decorate(editor);
I am guessing I should be using AutoCompleteComboBoxEditor?
-Navin
|
|
|
|
|
|
|
|
Re: Problem with auto complete combobox and JTable
Posted:
Nov 17, 2008 8:27 AM
in response to: navinkjha
|
|
|
> I am guessing I should be using > AutoCompleteComboBoxEditor?
Yes.
Karl
|
|
|
|
|
|
|
|
Re: Problem with auto complete combobox and JTable
Posted:
Nov 17, 2008 8:52 AM
in response to: kschaefe
|
|
|
Karl,
AutoCompleteComboBoxEditor is a ComboBoxEditor implementation but TableColumn needs TableCellEditor implementation, am I missing something?
Is there a sample code for using AutoCompleteComboBoxEditor with TableColumn?
-Navin
Message was edited by: navinkjha
|
|
|
|
|
|
|
|
Re: Problem with auto complete combobox and JTable
Posted:
Nov 17, 2008 10:52 AM
in response to: navinkjha
|
|
|
Oops, when grabbing the docs this morning, I looked at the wrong item.
You should use ComboBoxCellEditor.
JTable table = ...;
JComboBox combo = ...;
AutoCompleteDecorator.decorate(combo);
TableColumn column = table.getColumnModel().getColumn(0);
column.setCellEditor(new ComboBoxCellEditor(comboBox));
Karl
|
|
|
|
|
|
|
|
Re: Problem with auto complete combobox and JTable
Posted:
Nov 17, 2008 12:43 PM
in response to: kschaefe
|
|
|
Karl, This is exactly what I do. Unfortunately when you type the first letter, only the editor comes up, matching does not happen. Looks like the key typed doesn't get handled in that case. After this it works fine. For a user it is confusing since you think that nothing exists matching that character.
|
|
|
|
|
|
|
|
Re: Problem with auto complete combobox and JTable
Posted:
Nov 17, 2008 12:57 PM
in response to: navinkjha
|
|
|
Don't extend ComboBoxCellEditor. Use a normal editor and have the ComboBoxCellEditor wrap it.
Karl
|
|
|
|
|
|
|
|
Re: Problem with auto complete combobox and JTable
Posted:
Nov 18, 2008 10:33 AM
in response to: kschaefe
|
|
|
Karl, If I extend DefaultCellEditor instead of ComboBoxCellEditor then How do I wrap it? ComboBoxCellEditor has a constructor with JComboBox as parameter.
|
|
|
|
|
|
|
|
Re: Problem with auto complete combobox and JTable
Posted:
Nov 18, 2008 11:27 AM
in response to: navinkjha
|
|
|
OK. I'm confused about what you want and where the problem is. I should have not attempted to solve this without a small, runnable demo. Please provide one.
Thanks,
Karl
|
|
|
|
|
|
|
|
Re: Problem with auto complete combobox and JTable
Posted:
Nov 21, 2008 9:02 AM
in response to: kschaefe
|
|
|
Karl, I put together a test sample quickly. When you run the sample, just tab to the column that has value "orange". then start typing, I want to be able to show the value in the drop down that matches the first letter typed. So I want show "kiwi" if I typed k once the focus is on the column. Currently I have to type k twice, once to bring editor and then again to get to "kiwi". how do I accomplish this? -------------------------------------------------------- package test;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator; import org.jdesktop.swingx.autocomplete.ComboBoxCellEditor;
import javax.swing.*; import javax.swing.table.AbstractTableModel; import javax.swing.table.TableModel; import java.awt.*;
public class TestAutoFill { public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) { System.out.println("error setting l &f " + e); }
JFrame f = new JFrame();
TableModel dataModel = new AbstractTableModel() { public int getColumnCount() { return 10; } public int getRowCount() { return 10;} public boolean isCellEditable(int rowIndex, int columnIndex) { return true; } public Object getValueAt(int row, int col) { if (col == 2) return "orange"; else return new Integer(row*col); } }; JTable table = new JTable(dataModel); JScrollPane scrollpane = new JScrollPane(table); table.getColumnModel().getColumn(2).setCellEditor(new SomeEditor(new JComboBox())); f.add(scrollpane);
table.setSurrendersFocusOnKeystroke(true);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true); }
private static class SomeEditor extends ComboBoxCellEditor { private JComboBox combo; public SomeEditor(JComboBox combo) {
super(combo);
this.combo = combo;
AutoCompleteDecorator.decorate(this.combo); // add auto-complete } public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
combo.addItem("apple"); combo.addItem("orange"); combo.addItem("kiwi"); combo.addItem("banana"); combo.addItem("pineapple");
if (value != null) { combo.setSelectedItem(value); } return combo; } } }
Message was edited by: navinkjha
|
|
|
|
|
|
|
|
Re: Problem with auto complete combobox and JTable
Posted:
Dec 2, 2008 7:44 AM
in response to: navinkjha
|
|
|
Navin,
The behavior is no different than the behavior for an "normal" combo box. I actually think that that is a misbehavior with the normal code.
The problem is that JComboBox.processKeyBindings doesn't seem to be doing the right thing. Not sure where to go with that right now. I'll give it some thought.
Karl
|
|
|
|
|
|
|
|
Re: Problem with auto complete combobox and JTable
Posted:
Dec 2, 2008 7:57 AM
in response to: kschaefe
|
|
|
Karl,
In another case I had to provide excel like behavior for text fields in JTable. All I would in that case is not set the value in getTableCellEditorComponent(...) method and the typed value would automatically get into the text editor. I am wondering how is it different in case of combo box ? Somehow the key event is not propogated to the combo box?
-Navin
|
|
|
|
|
|
|
|
Re: Problem with auto complete combobox and JTable
Posted:
Dec 2, 2008 9:58 AM
in response to: navinkjha
|
|
|
Navin,
It is, but not in a way that the combo box is going to do the right thing with it. JTable.processKeyBinding forwards to the editor component (if it is a JComponent subclass) as ((JComponent) editorComponent).processKeyBindings, not sure why it does not "do the right thing," but there's clearly some potential investigation there. Yes, text fields work as expected, but the combos do not.
I've been toying with a custom JComboBox.processKeyBindings and had it work, but also simultaneously caused an OutOfMemory recursion error. So, if you want to make a stab in there and see if you can't come up with something; I'll do the same.
Karl
|
|
|
|
|
|
|
|
Re: Problem with auto complete combobox and JTable
Posted:
Dec 2, 2008 8:11 PM
in response to: kschaefe
|
|
|
This is what I have so far (first column is the test column):
import java.awt.event.KeyEvent;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.table.TableCellEditor;
public class AutoStartEditTest extends JFrame {
protected void frameInit() {
super.frameInit();
setDefaultCloseOperation(EXIT_ON_CLOSE);
JTable table = new JTable(2, 10);
table.getColumnModel().getColumn(0).setCellEditor(createExampleEditor());
table.setSurrendersFocusOnKeystroke(true);
add(new JScrollPane(table));
pack();
}
private TableCellEditor createExampleEditor() {
JComboBox combo = new JComboBox(new Object[] {
"apple",
"orange",
"kiwi",
"pineapple",
"banana",
}) {
protected boolean processKeyBinding(KeyStroke ks, KeyEvent e,
int condition, boolean pressed) {
boolean retValue = super.processKeyBinding(ks, e, condition, pressed);
if (!retValue && isStartingCellEdit() && editor != null) {
// this is where the magic happens
// not quite right; sets the value, but doesn't advance the
// cursor position for AC
editor.setItem(String.valueOf(ks.getKeyChar()));
}
return retValue;
}
private boolean isStartingCellEdit() {
JTable table = (JTable) SwingUtilities.getAncestorOfClass(
JTable.class, this);
return table != null
&& table.isFocusOwner()
&& !Boolean.FALSE.equals((Boolean) table
.getClientProperty("JTable.autoStartsEdit"));
}
};
AutoCompleteDecorator.decorate(combo);
return new ComboBoxCellEditor(combo);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
System.out.println("error setting l &f " + e);
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new AutoStartEditTest().setVisible(true);
}
});
}
}
Karl
|
|
|
|
|
|
|
|
Re: Problem with auto complete combobox and JTable
Posted:
Dec 3, 2008 6:28 AM
in response to: kschaefe
|
|
|
Thanks Karl. I will take a stab at it although you seem to be getting somewhere with it.
|
|
|
|
|
|
|
|
Re: Problem with auto complete combobox and JTable
Posted:
Dec 12, 2008 12:31 PM
in response to: navinkjha
|
|
|
Karl,
I noticed that the processKeyBinding(...) gets called twice. Not sure if that causes a problem.
-Navin
|
|
|
|
|
|
|
|
Re: Problem with auto complete combobox and JTable
Posted:
Dec 13, 2008 6:40 AM
in response to: navinkjha
|
|
|
I don't think so. You can eliminate the second called by adding pressed (on or off) to the list of checks for the if-statement.
I think the real solution will evade us since we cannot call protected methods on a JComponent. I'm tempted to fool with the OpenJDK source to test that hypothesis, but I'm not sure I want to go through all the trouble.
Karl
|
|
|
|
|