T_Shah
asked on
How to add Button to JTable cell?
Hello,
how do I add JButton JTable cells.
I have created a default model. I pass Object data[ ][ ] and Object column[ ].
I have 5 columns. Out of 5 columns, 4 columns contain string value and in the fifth column I have to create JButton for each row.
Any help would be appreciated.
Thanks
how do I add JButton JTable cells.
I have created a default model. I pass Object data[ ][ ] and Object column[ ].
I have 5 columns. Out of 5 columns, 4 columns contain string value and in the fifth column I have to create JButton for each row.
Any help would be appreciated.
Thanks
http://www.objects.com.au/java/examples/swing/examples/JButtonTableExample.java
hi,
i think this will provide u a required information and help u a lot
www.devx.com/getHelpOn/10MinuteSolution/20425
www.codeguru.com/java/Swing/JTable/index.shtml
www.javalobby.org/java/forums/t53618.html
i think this will provide u a required information and help u a lot
www.devx.com/getHelpOn/10MinuteSolution/20425
www.codeguru.com/java/Swing/JTable/index.shtml
www.javalobby.org/java/forums/t53618.html
ASKER
Thanks for reply.
I have table with 5 columns. In Last column, I am trying to create date button for each row.
User click the button and select a date.
My problems are:
* when table loads, it does not display button in last column. but when I click on the cell
button pop up the calendar from which user can pick a date. Is there any solution to display button?
* When I select a date, I can see the date on the button but it does not set the value in table model.
It updates the value, only when I click on some other cell in the table atfet I pick the date.
Any clue?
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- ---------- -----
Class Test extends JDialog implements ActionListener
TableValues tv = new TableValues(data);
table = new JTable(tv);
table.setRowHeight(30);
TableColumnModel tcm = table.getColumnModel();
TableColumn tc = tcm.getColumn(4);
tc.setCellEditor(new TableCellEditor());
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- ---------- -----
public class TableValues extends AbstractTableModel
Object[][] values;//contains all the data that displays in JTable
public int getRowCount()
{
return values.length;
}
public int getColumnCount()
{
return values[0].length;
}
public Object getValueAt(int row, int column)
{
return values[row][column];
}
public String getColumnName(int column)
{
return columnNames[column];
}
public boolean isCellEditable(int row, int column)
{
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if(column == 4)
{
return true;
}
else
{
return false;
}
}
public void setValueAt(Object value, int row, int column)
{
values[row][column] = value;
}
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- ---------- -----
public class TableCellEditor extends AbstractCellEditor implements javax.swing.table.TableCel lEditor
{
// This is the component that will handle the editing of the cell value
DateButton component = new DateButton();
// This method is called when a cell value is edited by the user.
public Component getTableCellEditorComponen t(JTable table, Object value,
boolean isSelected, int rowIndex, int vColIndex)
{
// 'value' is value contained in the cell located at (rowIndex, vColIndex)y
if (isSelected)
{
// cell (and perhaps other cells) are selected
}
// Configure the component with the specified value
component.setText((String) value);
// Return the configured component
return component;
}
// This method is called when editing is completed.
// It must return the new value to be stored in the cell.
public Object getCellEditorValue()
{
return ((DateButton)component).ge tText();
}
}
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- ---------- -----
I have table with 5 columns. In Last column, I am trying to create date button for each row.
User click the button and select a date.
My problems are:
* when table loads, it does not display button in last column. but when I click on the cell
button pop up the calendar from which user can pick a date. Is there any solution to display button?
* When I select a date, I can see the date on the button but it does not set the value in table model.
It updates the value, only when I click on some other cell in the table atfet I pick the date.
Any clue?
//------------------------
Class Test extends JDialog implements ActionListener
TableValues tv = new TableValues(data);
table = new JTable(tv);
table.setRowHeight(30);
TableColumnModel tcm = table.getColumnModel();
TableColumn tc = tcm.getColumn(4);
tc.setCellEditor(new TableCellEditor());
//------------------------
public class TableValues extends AbstractTableModel
Object[][] values;//contains all the data that displays in JTable
public int getRowCount()
{
return values.length;
}
public int getColumnCount()
{
return values[0].length;
}
public Object getValueAt(int row, int column)
{
return values[row][column];
}
public String getColumnName(int column)
{
return columnNames[column];
}
public boolean isCellEditable(int row, int column)
{
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if(column == 4)
{
return true;
}
else
{
return false;
}
}
public void setValueAt(Object value, int row, int column)
{
values[row][column] = value;
}
//------------------------
public class TableCellEditor extends AbstractCellEditor implements javax.swing.table.TableCel
{
// This is the component that will handle the editing of the cell value
DateButton component = new DateButton();
// This method is called when a cell value is edited by the user.
public Component getTableCellEditorComponen
boolean isSelected, int rowIndex, int vColIndex)
{
// 'value' is value contained in the cell located at (rowIndex, vColIndex)y
if (isSelected)
{
// cell (and perhaps other cells) are selected
}
// Configure the component with the specified value
component.setText((String)
// Return the configured component
return component;
}
// This method is called when editing is completed.
// It must return the new value to be stored in the cell.
public Object getCellEditorValue()
{
return ((DateButton)component).ge
}
}
//------------------------
ASKER
I forget to add code for CellRender in previous post.
OK, I fix the problem to show button.
Now Only one problem:
* When I select a date, I can see the date on the button but it does not set the value in table model.
It updates the value, only when I click on some other cell in the table atfet I pick the date.
Any clue?
OK, I fix the problem to show button.
Now Only one problem:
* When I select a date, I can see the date on the button but it does not set the value in table model.
It updates the value, only when I click on some other cell in the table atfet I pick the date.
Any clue?
ASKER
any clue??
u need an action listener on your button that ends cell editting (as shown in the example i posted)
ASKER
Thanks for your reply. I tried the way your example show but does not work.
The problem is :
The date button is a custom class that is been provided. I can not see its code. This class create a button. when you click the button, it popup a calendar from which you can pick a date. Also, it has ok and clear button. When I pick the date and click ok button, new date set to that button. If I call fireEditingStopped(); on OK button, it should work fine but I don't know how to get action event of that button.
Is there any way, I can force fully call SetValueAt method at point where new date is set to that button?
If so, can you tell me where exactly I should call that?
I tried so many different ways but none works...
I really appreciate your help
The problem is :
The date button is a custom class that is been provided. I can not see its code. This class create a button. when you click the button, it popup a calendar from which you can pick a date. Also, it has ok and clear button. When I pick the date and click ok button, new date set to that button. If I call fireEditingStopped(); on OK button, it should work fine but I don't know how to get action event of that button.
Is there any way, I can force fully call SetValueAt method at point where new date is set to that button?
If so, can you tell me where exactly I should call that?
I tried so many different ways but none works...
I really appreciate your help
try the popup updating the table model directly then.
ASKER
How do I do that?
What indication do you have that the user has entered a date?
ASKER
my code is mentioned above.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for your help. I solved the problem.