Ok,
I read other posts on the same topic, but i didn't get what i exactly wanted..
1- i want to add JCheckboxes in one of the JTable columns
2- i want to handle events for it.. e.g adding new row when checkbox is clicked etc
I referred the java docs, while adding the Boolean to a column makes checkboxes appear in the column, but they don't work!(they don't change state marked/unmarked) Also tried creating own TableModel extending AbstractTableModel and adding CellRenderer but then the checkboxes disappear and a string true/false is displayed in it.. There's something i'm missing out, can someone give me a direction..please
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
Have a look at
http://download.oracle.com/javase/tutorial/uiswing/components/table.html#renderer
in particular the section "Using Other Editors" that starts quite near the bottom...
ksj 0 Newbie Poster
Have a look at
http://download.oracle.com/javase/tutorial/uiswing/components/table.html#renderer
in particular the section "Using Other Editors" that starts quite near the bottom...
Thank you very much..that helped
It worked fine with arrays..
Now, I'm trying to use a vector instead of an array, so I can add rows at run-time..
But the checkbox and combobox don't work now. Here's the code.. Something wrong? please tell me
import java.awt.Component;
import java.awt.Dimension;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
public class TableDemo extends JPanel {
public TableDemo(){
MyTableModel tableModel = new MyTableModel();
JTable table = new JTable(tableModel);
table.setPreferredScrollableViewportSize(new Dimension(500, 100));
table.setFillsViewportHeight(true);
tableModel.addRow(0, new Object[]{"", new Boolean(false),"Kim","Option 1"});
//Set up column sizes.
initColumnSizes(table);
//Setting cell renderer and editor
TableColumn selectColumn = table.getColumnModel().getColumn(3);
JComboBox options = new JComboBox();
options.addItem("Option 1");
options.addItem("Option 2");
selectColumn.setCellEditor(new DefaultCellEditor(options));
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setToolTipText("Click for Combobox");
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
}
private void initColumnSizes(JTable table) {
MyTableModel model = (MyTableModel)table.getModel();
TableColumn column = null;
Component comp = null;
int headerWidth = 0;
int cellWidth = 0;
Object[] longValues = model.longValues;
TableCellRenderer headerRenderer =
table.getTableHeader().getDefaultRenderer();
for (int i = 0; i < 4; i++) {
column = table.getColumnModel().getColumn(i);
comp = headerRenderer.getTableCellRendererComponent(
null, column.getHeaderValue(),
false, false, 0, 0);
headerWidth = comp.getPreferredSize().width;
comp = table.getDefaultRenderer(model.getColumnClass(i)).
getTableCellRendererComponent(
table, longValues[i],
false, false, 0, i);
cellWidth = comp.getPreferredSize().width;
column.setPreferredWidth(Math.max(headerWidth, cellWidth));
}
}
class MyTableModel extends AbstractTableModel{
String columnNames[] = {"","Check","Name","Select"};
Vector<Object[]> data = new Vector<Object[]>();
public final Object[] longValues = {"",new Boolean(false),"","Option 2"};
@Override
public int getRowCount() {
System.out.println("getRowCount");
Iterator<Object[]> iter = data.iterator();
int rowCount = 0;
while(iter.hasNext())
{
iter.next();
rowCount++;
}
return rowCount;
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Object value = null;
Object currRow[] = null;
int currRowIndex = 0;
Iterator<Object[]> iter = data.iterator();
while(iter.hasNext())
{
currRowIndex++;
currRow = iter.next();
if(currRowIndex == rowIndex)
{
value = currRow[columnIndex];
}
}
return value;
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
Object currRow[] = null;
int currRowIndex = 0;
Iterator<Object[]> iter = data.iterator();
while(iter.hasNext())
{
currRowIndex++;
currRow = iter.next();
if(currRowIndex == rowIndex)
{
currRow[columnIndex] = aValue;
addRow(currRowIndex, currRow);
}
}
}
public void addRow(int rowIndex, Object values[]){//, int rowIndex, int columnIndex){
data.add(rowIndex,values);
}
@Override
public String getColumnName(int column) {
return columnNames[column];
}
@Override
public Class getColumnClass(int columnIndex) {
return getValueAt(1, columnIndex).getClass();
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
if(columnIndex==0)
return false;
else
return true;
}
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("TableRenderDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
TableDemo newContentPane = new TableDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
mKorbel 274 Veteran Poster
remove AbstactTableModel, start with DefaultTableModel and set Class for TableColumn corectly
with class I meaning somthing like as ....
@Override
public Class<?> getColumnClass(int colNum) {
switch (colNum) {
case 0:
return Integer.class;
case 1:
return Date.class;
case 2:
return Date.class;
case 3:
return Integer.class;
case 4:
return Double.class;
case 5:
return Double.class;
case 6:
return Double.class;
case 7:
return Double.class;
case 8:
return Double.class;
default:
return String.class;
}
}
but anyway you can search for TableCellRenderer and TableCellEditor, there are everything
http://download.oracle.com/javase/tutorial/uiswing/components/table.html
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.