Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

JTable question - URGENT

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic