IntelliJ Java IDE
The moose likes Swing / AWT / SWT / JFace and the fly likes Need help with adding an action on JButon Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT / JFace
Reply Bookmark "Need help with adding an action on JButon" Watch "Need help with adding an action on JButon" New topic
Author

Need help with adding an action on JButon

bobby, morkos
Ranch Hand

Joined: Jan 04, 2002
Posts: 82
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.io.*;
public class JButtonInJTable extends JFrame implements ActionListener {
JTable table;
DefaultTableModel dModel;
DefaultTableModel dm;
public JButtonInJTable () {
JButton A = new JButton("A");
A.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
System.out.println(">> Button action >> A");
}
});
JButton B = new JButton("B"); B.addActionListener(this);
JButton C = new JButton("C"); A.addActionListener(this);
JButton D = new JButton("D"); A.addActionListener(this);
JButton E = new JButton("E"); A.addActionListener(this);
Object [][] data = {{ A ,"aaa"},{B, "bbb"},{C,"ccc"},{D,"ddd"},{E,"eee"}};
Object [] value ={"label 1","label 2"};

dModel = new DefaultTableModel(data,value);
table = new JTable(dModel);
table.setPreferredScrollableViewportSize(new Dimension(750, 250));
table.setRowSelectionAllowed(true);
JButtonRenderer jbr = new JButtonRenderer();
table.getColumn("label 1").setCellRenderer(jbr);
JScrollPane scrollPane = new JScrollPane(table);
JPanel centerPanel = new JPanel();
centerPanel.add(scrollPane);
this.getContentPane().add(centerPanel,BorderLayout.CENTER);
this.setVisible(true);
this.setBounds(100,100,200,200);
pack();
}
public static void main(String [] a){
new JButtonInJTable();
}
public void actionPerformed(java.awt.event.ActionEvent event) {
if(event.getActionCommand()=="A") {
System.out.println(">> Button action >> A");
}
if(event.getActionCommand()=="B") {
System.out.println(">> Button action >> B");
}
}
}

class JButtonRenderer implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column) {
//if (value==null) return null;
// System.out.println("row : " + row + "column : " + column);
return (Component)value;
}
}
Shane Roylance
Ranch Hand

Joined: Aug 29, 2001
Posts: 72
I don't know if this is the problem you are asking about, but if the action listener you are implementing doesn't work I think it is because your conditional statement is wrong.
it should be:
if( event.getActionCommand().equals("A") ) the == will not work in this case.
Renee Zhang
Ranch Hand

Joined: Sep 10, 2001
Posts: 72
Hi, bobby:
Please add this class to your source file
class JTableButtonMouseListener implements MouseListener {
private JTable __table;
private void __forwardEventToButton(MouseEvent e) {
TableColumnModel columnModel = __table.getColumnModel();
int column = columnModel.getColumnIndexAtX(e.getX());
int row = e.getY() / __table.getRowHeight();
Object value;
if(row >= __table.getRowCount() || row < 0 ||
column >= __table.getColumnCount() || column < 0)
return;
value = __table.getValueAt(row, column);
if(value instanceof JButton) {
JButton button = (JButton)value;
MouseEvent buttonEvent = (MouseEvent)SwingUtilities.convertMouseEvent(__table, e, button);
button.dispatchEvent(buttonEvent);
System.out.println(button.getActionCommand());
}
else if(value instanceof JComboBox) {
JComboBox combobox = (JComboBox)value;
MouseEvent comboBoxEvent = (MouseEvent)SwingUtilities.convertMouseEvent(__table, e, combobox);
combobox.dispatchEvent(comboBoxEvent);
}
else if(value instanceof JCheckBox) {
JCheckBox checkbox = (JCheckBox)value;
MouseEvent checkboxEvent = (MouseEvent)SwingUtilities.convertMouseEvent(__table, e, checkbox);
checkbox.dispatchEvent(checkboxEvent);
}
else {
System.out.println("Is something else.");
}
// This is necessary so that when a button is pressed and released
// it gets rendered properly. Otherwise, the button may still appear
// pressed down when it has been released.
__table.repaint();
}
public JTableButtonMouseListener(JTable table) {
__table = table;
}
public void mouseClicked(MouseEvent e) {
__forwardEventToButton(e);
}
public void mouseEntered(MouseEvent e) {
// __forwardEventToButton(e);
}
public void mouseExited(MouseEvent e) {
//__forwardEventToButton(e);
}
public void mousePressed(MouseEvent e) {
// __forwardEventToButton(e);
}
public void mouseReleased(MouseEvent e) {
//__forwardEventToButton(e);
}
}

Then add one line to your code
table.addMouseListener(new JTableButtonMouseListener (table));

Good luck!
bobby, morkos
Ranch Hand

Joined: Jan 04, 2002
Posts: 82
Thanks it works fine now. But, I want to retrieve "aaa" when a click button 1 and retrieve "bbb" when a click button 2. What I'm trying to do is when a click a button the cell value associated with the button is retrieved. I hope you can help.
Renee Zhang
Ranch Hand

Joined: Sep 10, 2001
Posts: 72
That is simply! Delete JTableButtonMouseListener and addMouseListener to your table
table.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
JTable table =(JTable) me.getSource();
Point p = me.getPoint();
int row = table.rowAtPoint(p);
int col = table.columnAtPoint(p);
if (col == 0) {
System.out.println("The data you want is " + table.getValueAt(row, 1));
}
}
});
Good luck!
 
IntelliJ Java IDE
 
subject: Need help with adding an action on JButon
 
Threads others viewed
Adding Column Dynamically to JTable
jtable
JTable ........
adding a row to a jtable by pressing a key
render jtree in jtable
developer file tools