JTables with JButtons, I'm overlooking something
Ok, I have two columns of my JTable rendered to hold JButtons. When those JButtons are pressed, it will fire a method that opens the appropriate JFrame. My problem is that I can't get the ActionListener for each JButton to respond. Here is the snippets of code that are relevant for the issue at hand.
Code java:
public class UserControl extends JFrame
{
...
Object[][] data;
String[] columnNames;
JTable table;
DefaultTableModel model;
...
public UserControl(boolean isSOC)
{
...
table.getColumnModel().getColumn(4).setCellRenderer(new TableCellRenderer()
{
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,boolean isFocused, int row, int col)
{
final String val = value+"";
JButton rendererComponent = new JButton(val);
rendererComponent.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.out.println("Action performed");
UserControl.this.openContact(val);
}
});
return rendererComponent;
}
});
table.getColumnModel().getColumn(5).setCellRenderer(new TableCellRenderer()
{
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,boolean isFocused, int row, int col)
{
final String val = value+"";
JButton rendererComponent = new JButton(val);
rendererComponent.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.out.println("Action performed");
UserControl.this.openContact(val);
}
});
return rendererComponent;
}
});
...
}
public void openContact(String s)
{
for(int i=0;i<contacts.size();i++)
{
if(contacts.get(i).name.equalsIgnoreCase(s))
{
new ContactInfo(s, contacts.get(i).toString());
}
}
}
...
}
Any thoughts?
Re: JTables with JButtons, I'm overlooking something
Ok, scratch that issue. Figured it out. I used the SelectionModel, although it is not ideal for what I'm wanting, since it registers the cell as being selected and not the JButton as being pressed. But for now it is fine. If there are any better ways of doing it, I would appreciate learning them.
Now I have a much more infuriating issue with my JTable. Something that I have never managed to do correctly. How do I make it so only a ONE cell can be selected at a time. My issue is that when a Button is selected, the entire row is selected. Which means the other JButton in that row cannot be pressed since the row is already selected. I know it is possible to fix this, but I have never figured out how and I cannot find anything on Google.
Re: JTables with JButtons, I'm overlooking something
Code java:
table.setCellSelectionEnabled(true);
Re: JTables with JButtons, I'm overlooking something
Check out Rob Camick's blog post:
Table Button Column Java Tips Weblog
db
Re: JTables with JButtons, I'm overlooking something
Quote:
Originally Posted by
Darryl.Burke
Ok, so I followed his instructions and wrote this par with his article:
Code java:
Action open = new AbstractAction(){
public void actionPerformed(ActionEvent e)
{
System.out.println("ActionPerformed");
String val = ((JButton)(e.getSource())).getText();
System.out.println("val: "+val);
UserControl.this.openContact(val);
}
};
ButtonColumn buttonColumn = new ButtonColumn(table,open,4);
ButtonColumn buttonCol = new ButtonColumn(table,open,5);
But the event doesnt register when a button is pressed. Any thoughts?
EDIT: Nvm, I think I may have gotten it. We'll see when we do an extensive test.