-
ComboBox ina JXTable
Hello
In first , excuse me for my english...
I'm a newbee in Java but I would like to find a solution for this problem :
I would like to put a JCombobox in one cell of a JXtable
exemple :
the cell (0,0) contains a name of a town , and the cell (0,1) contain a JComboBox with the list of the borough.
If somebody could help me ...
thanks
-
Hi
I think I have the same problem. I have several different ComboBoxes. I want to define them like
JComboBox cb1 = new JComboBox()
JComboBox cb2 = new JComboBox()
....
and then add them to the table,
for column1/row1 cb1,
for column1/row2 cb2
...and so on.
The solutions I found so far is to set one ant the same ComboBox for the entire column with getColumnModel and setCellEditor. But I want to have different ComboBoxes for different cells.
Another solution is create my own TableCellEditor (example at http://www.java2s.com/Code/Java/Swin...ndcomobobx.htm)... but I am not so sure how to make it work and if its really the right approach...
Does anyone has a easy example or is it really so complicated to place an object in a specific table cell?
hope someone can help us 
thanks
-
hello,
I implement this code :
Code:
MyDefaultTableCellRenderer cellRendererTown = new MyDefaultTableCellRenderer();
final MyDefaultTableCellEditor cellEditorTown= new MyDefaultTableCellEditor();
//I create un ActionListener on my combo because, the methode getCellEditorValue is executed only when the combo lost the focus, not when a value is selected!
cellEditorProfil.getCombo().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Town town = (StudyGeneric) JXTableListHelper.getSelectedRowValue(table);
setBorough(cellEditorProfil.getCombo().getSelectedItem());
}
});
table.getColumnExt(3).setCellEditor(cellEditorTown);
table.getColumnExt(3).setCellRenderer(cellRendererTown);
public class MyDefaultTableCellEditor extends AbstractCellEditor implements TableCellEditor {
JComboBox combo;
public MyDefaultTableCellEditor() {
super();
combo = new JComboBox(new ComboBoxListModel());
combo.setRenderer(new ComboBoxMapRenderer());
}
public Object getCellEditorValue() {
return combo.getSelectedItem();
}
public Component getTableCellEditorComponent(JTable tableau, Object value, boolean isSelected, int row, int column) {
Town town = (Town) value;
//this line is necessary because the selection of the combo not select the line of my table
// --> actionEvent of combo crash when I recup the Line selected of the table
JXTableListHelper.setSelectedRow(table, town);
if (column == 3) {
updating the liste of the borough in my combo (depend of the town)
updateLstBoroughItems(combo, town);
return combo;
}
}
public JComboBox getCombo() {
return combo;
}
}
public class MyDefaultTableCellRenderer extends DefaultTableRenderer {
JComboBox combo;
public MyDefaultTableCellRenderer() {
super();
combo = new JComboBox(new ComboBoxListModel());
combo.setRenderer(new ComboBoxMapRenderer());
}
public JComboBox getCombo() {
return combo;
}
public Component getTableCellRendererComponent(JTable jTable, Object object, boolean b, boolean b1, int row, int col) {
Town town= (Townobject;
if (col == 3) {
updateLstBoroughItems(combo, study);
return combo;
}
}
}
But I don't know if it's the good methode ! I have another little probleme in one case : If you want to do a control of the valule selected in the combo , if I use the methode cancelCellEditing of the cellEditor, I lost my old value ...
I found a solution (good ??) :
Code:
cellEditorProfil.getCombo().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Town town = (StudyGeneric) JXTableListHelper.getSelectedRowValue(table);
if(!setBrough(cellEditorProfil.getCombo().getSelectedItem())){
//if the control is not good the value of the borough is not updated in my persistent model , so I 'm updating the combo cellEditorFonction.getCombo().getModel().setSelectedItem(getBrough(town);
}
}
});
a+
-
Thanks @testeurforme, your example helped me to fix my problem. Here's how I did it.
With this code, I can add objects to the table and access them without looping through the table.
Code:
JTable table = new JTable(tableModel);
DefaultTableModel tableModel = new MyTableModel();
table.getColumn("AColumnTitle").setCellEditor(new MyTableCellEditor(table.getRowCount(), table.getColumnCount()));
MyTableCellEditor sets the ComboBox and CheckBox objects and handles the displayed value:
Code:
import java.util.*;
import java.awt.Component;
import javax.swing.*;
import javax.swing.table.*;
public class MyTableCellEditor extends AbstractCellEditor implements TableCellEditor {
static final long serialVersionUID = 2;
DefaultCellEditor[][] cellEditors;
int row;
int column;
public MyTableCellEditor(int rows, int columns) {
this.cellEditors = new DefaultCellEditor[rows][columns];
//Define the editors row by row
for ( int setRow = 0; setRow < rows; setRow++ ) {
for ( int setColumn = 0; setColumn < columns; setColumn++ ) {
if ( setColumn == 1 ) {
//Set a combo box
cellEditors[setRow][setColumn] = new DefaultCellEditor(A_JCOMBO_BOX);
} else if ( setColumn == 4 ) {
//Align the checkbox
A_JCHECK_BOX.setHorizontalAlignment(JCheckBox.CENTER);
//Set the check box
cellEditors[setRow][setColumn] = new DefaultCellEditor(A_JCHECK_BOX);
} else {
cellEditors[setRow][setColumn] = null;
}
}
}
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
this.row = row;
this.column = column;
return cellEditors[row][column].getTableCellEditorComponent(table, "",
isSelected, row, column);
}
public Object getCellEditorValue() {
//Retrun a value according to the component
if ( cellEditors[row][column].getComponent() instanceof JComboBox ){
JComboBox c = (JComboBox)cellEditors[row][column].getComponent();
return c.getSelectedItem();
} else if ( cellEditors[row][column].getComponent() instanceof JCheckBox ){
JCheckBox c = (JCheckBox)cellEditors[row][column].getComponent();
return c.isSelected();
}
return null;
}
}
The table model handles the appearance of the cell content:
Code:
import javax.swing.*;
import javax.swing.table.*;
public class MyTableModel extends DefaultTableModel {
static final long serialVersionUID = 2;
public Class<?> getColumnClass(int columnIndex) {
if ( (columnIndex == 4) || (columnIndex == 7) ) {
//Checkbox
return Boolean.class;
} else if ( columnIndex == 1 ) {
//Combobox
return JComboBox.class;
} else {
//No special model.
return super.getColumnClass(columnIndex);
}
}
public boolean isCellEditable(int rowIndex, int vColIndex) {
//Some locked cells
if ( (vColIndex == 0) || (vColIndex == 2) ) {
return false;
}
return true;
}
}
Hope that helps someone with the same problem... It took me a while to figure out which method handles what...
Last edited by mabuhay; 05-19-2008 at 12:46 PM.
Similar Threads
-
By Narasimhan in forum .NET
Replies: 6
Last Post: 03-27-2010, 01:49 AM
-
By subrama6 in forum .NET
Replies: 4
Last Post: 02-13-2008, 09:19 AM
-
By qvqnytowl in forum VB Classic
Replies: 1
Last Post: 08-28-2006, 06:51 PM
-
By Stan Shankman in forum .NET
Replies: 2
Last Post: 09-07-2001, 03:06 AM
-
By xiao_john@yahoo.com in forum VB Classic
Replies: 0
Last Post: 02-20-2001, 01:23 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks