Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Jeanne Boyarsky
Ron McLeod
Sheriffs:
Paul Clapham
Liutauras Vilda
Devaka Cooray
Saloon Keepers:
Tim Holloway
Roland Mueller
Bartenders:
Forum:
Swing / AWT / SWT
Select item in jcombobox in jtable
Nuno Prudencio
Greenhorn
Posts: 4
posted 9 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi everyone,
I can insert a jcombobox in a jtable cell. Is updated by a button. But i cannot select a default object in a combobox.
I have the following code in the button "update"
TableColumn sportColumn = table.getColumnModel().getColumn(0); JComboBox comboBox = new JComboBox(); comboBox.addItem("Snowboarding"); comboBox.addItem("Rowing"); comboBox.addItem("Knitting"); comboBox.addItem("Speed reading"); comboBox.addItem("Pool"); comboBox.addItem("None of the above"); comboBox.setSelectedItem("Speed reading"); DefaultCellEditor defaultCellEditor = new DefaultCellEditor(comboBox); sportColumn.setCellEditor(defaultCellEditor); sportColumn.setCellRenderer(new CheckBoxCellRenderer(comboBox)); table.repaint();
and i have the following TableCellRender
class CheckBoxCellRenderer extends JComboBox implements TableCellRenderer { public CheckBoxCellRenderer(JComboBox comboBox) { for (int i = 0; i < comboBox.getItemCount(); i++) { this.addItem(comboBox.getItemAt(i)); } } @Override public Component getTableCellRendererComponent(JTable jtable, Object value, boolean isSelected, boolean hasFocus, int row, int column) { this.setSelectedItem(value); return this; } @Override public void setSelectedItem(Object selecao) { this.setSelectedItem(selecao); } }
Anybody can help me?
Regards
m Korbel
Ranch Hand
Posts: 174
posted 9 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
- use XxxTableModel.setValueAt(string_thats_match_with_JComboBox_Items), then you are set the initial value for XxxTableCellEditor/Renderer
____________________________________________________________________________
"I can insert a jcombobox in a jtable cell. Is updated by a button. But i cannot select a default object in a combobox."
- your implemetation of TableCellRenderer is wrong, JTable is designated to show value stored in XxxTableModel (see XxxTableModel.setValueAt)
- for more (and to use exclusivelly) information to read Oracle tutorial How to use Tables, part Using a Combo Box as an Editor
There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Right Align numbers in Jtable
Transfer focus to inside JComboBox from JTable
JTables & Combo Boxes
adding a row to a jtable by pressing a key
Why I can't catch NumberFormatException in the tableModel
More...