• Post Reply Bookmark Topic Watch Topic
  • New Topic

JTable question - URGENT  RSS feed

 
verduka fox
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello --
I am new to JTable and have some questions. I want one column of the table to be displayed as JButtons instead of JLabels. When the user clicks on the button I want to do some processing and then close the JFrame. I have tried following an example from http://java.sun.com/docs/books/tutorial/uiswing/components/example-swing/TableRenderDemo.java .
This example has the following line:
JComboBox comboBox = new JComboBox();
. . .
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
However, when I try to change the comboBox reference to a JButton reference, I get a compile error. It seems that DefaultCellEditor is only set up to handle JCheckBox, JCombBox, and JTextField. How do I capture that the user is selecting a row? Is there a way to capture a double click on a row? This would satisfy the business rules as well . . .
Sample code and explanation would be greatly appreciated.
 
Julie Stella
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is something like what you want...you'll have to edit to fit your needs but the basic deal is that you need a button renderer AND an editor.
Also...about capturing a double-click on a row--I'd advise against it. I tried it for awhile and noticed that JTable usually tries to interpret it as two separate single-clicks. You can put a timer in there to measure the time between clicks and use that to determine if it is a double or single click but it is very unreliable and hard to test in an IDE.
Hope this helps...
Stella
ButtonRender.java
-----------------
public class ButtonRenderer extends JButton implements TableCellRenderer {
public ButtonRenderer() {
super();
this.setEnabled(true);
this.setFont(new Font("Dialog", Font.ITALIC, 10));
this.setBackground(Color.orange);
}
public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
setText( (value ==null) ? "" : ((JButton)value).getText() );
return this;
}
}
ButtonEditor.java
-----------------
public class ButtonEditor extends DefaultCellEditor {
protected JButton button;
private String label;
private boolean isPushed;
private JTable table;
private TableModel model;
private int row;
private int col;
public ButtonEditor(JCheckBox checkBox, JTable table) {
super(checkBox);
button = new JButton();
button.setOpaque(true);
button.setFont(new Font("Dialog", Font.ITALIC, 10));
button.setBackground(Color.orange);
this.table = table;
model = table.getModel();
this.clickCountToStart = 1;
}
public Component getTableCellEditorComponent(JTable table, Object value,boolean isSelected, int row, int column) {
button = (JButton)value;
button.setFont(new Font("Dialog", Font.ITALIC, 10));
button.setBackground(Color.orange);
this.editorComponent = button;
return button;
}
public Object getCellEditorValue() {
button = (JButton)model.getValueAt(row, col);
return button;
}
public boolean shouldSelectCell(EventObject e) {
row = ((JTable)e.getSource()).getEditingRow();
col = ((JTable)e.getSource()).getEditingColumn();
return true;
}
public boolean stopCellEditing() {
return super.stopCellEditing();
}
protected void fireEditingStopped() {
super.fireEditingStopped();
}
}
 
verduka fox
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Julie Stella:
Also...about capturing a double-click on a row--I'd advise against it. I tried it for awhile and noticed that JTable usually tries to interpret it as two separate single-clicks.

Julie,
Since I couldn't figure out the JButton idea, I went ahead and coded for the double click using the code below. I am not using an IDE (good ole vi instead) and have not noticed a problem catching the double clicks so far. The user would prefer to have the double click action instead of the JButton in the first place. Is this a performance issue? Are there things I should know before implementing this solution into production? Please advise . . .

[This message has been edited by verduka fox (edited January 24, 2001).]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!