Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Multiple JButtons in a JTable cell: handling events

843807Sep 26 2009 — edited Sep 26 2009
Hello!

I'm trying to develop a Swing application which makes use of tables in several panels.
Each row of each table should present the user two buttons, one for editing the row, one for viewing details of that row. Both editing and viewing are done in another panel.
I thought I could add the buttons as the last column of each row, so I made a panel which holds the two buttons and the id of the row, so that I know what I have to edit or view.

I managed to insert the panel as the last column, but I can't have the buttons click.
I studied cell renderers and editors from several tutorials and examples, but evidently I can't understand either of them: whatever I try doesn't change the outcome... :(

Below is the code I use, except for imports and packages:

ActionPanel.java - The panel which holds the buttons and the row id
public class ActionPanel extends JPanel{

  private static final long serialVersionUID = 1L;

  public static String ACTION_VIEW="view";

  public static String ACTION_EDIT="edit";

  private String id;

  private JButton editButton;

  private JButton viewButton;

  public String getId() {
    return id;
  }
  public void setId(String id) {
    this.id = id;
  }

  public JButton getEditButton() {
    return editButton;
  }
  public void setEditButton(JButton editButton) {
    this.editButton = editButton;
  }

  public JButton getViewButton() {
    return viewButton;
  }
  public void setViewButton(JButton viewButton) {
    this.viewButton = viewButton;
  }

  public ActionPanel() {
    super();
    init();
  }

  public ActionPanel(String id) {
    super();
    this.id = id;
    init();
  }

  private void init(){
    setLayout(new FlowLayout(FlowLayout.CENTER, 10, 0))
    editButton=new JButton(new ImageIcon("./images/icons/editButtonIcon.png"));
    editButton.setBorderPainted(false);
    editButton.setOpaque(false);
    editButton.setAlignmentX(TOP_ALIGNMENT);
    editButton.setMargin(new Insets(0,0,0,0));
    editButton.setSize(new Dimension(16,16));
    editButton.setMaximumSize(new Dimension(16, 16));
    editButton.setActionCommand(ACTION_EDIT);
    editButton.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        JOptionPane.showMessageDialog(null, id, "Editing", JOptionPane.INFORMATION_MESSAGE);
      }
    });

    viewButton=new JButton(new ImageIcon("./images/icons/viewButtonIcon.png"));
    viewButton.setMaximumSize(new Dimension(16, 16));
    viewButton.setActionCommand(ACTION_VIEW);
    viewButton.setBorderPainted(false);
    viewButton.setOpaque(false);
    viewButton.setMargin(new Insets(0,0,0,0));
    viewButton.setSize(new Dimension(16,16));
    viewButton.setMaximumSize(new Dimension(16, 16));
    viewButton.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        JOptionPane.showMessageDialog(null, id, "Viewing", JOptionPane.INFORMATION_MESSAGE);
      }
    });
    add(viewButton);
    add(editButton);
  }
}
ActionPanelRenerer.java - the renderer for the above panel
public class ActionPanelRenderer implements TableCellRenderer{
  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    Component ret=(Component)value;
    if (isSelected) {
      ret.setForeground(table.getSelectionForeground());
      ret.setBackground(table.getSelectionBackground());
    } else {
      ret.setForeground(table.getForeground());
      ret.setBackground(UIManager.getColor("Button.background"));
    }
    return ret;
  }
}
ActionPanelEditor.java - this is the editor, I can't figure out how to implement it!!!!
public class ActionPanelEditor extends AbstractCellEditor implements TableCellEditor{
  public Component getTableCellEditorComponent(JTable table,Object value,boolean isSelected,int row,int column) {
    return (ActionPanel)value;
  }
  public Object getCellEditorValue() {
    return null;
  }
}
ServicesModel.java - The way I fill the table is through a model:
public class ServicesModel extends AbstractTableModel  {
  private Object[][] data;
  private String[] headers; 

  public ServicesModel(Object[][] services, String[] headers) {
    this.data=services;
    this.headers=headers;
  }

  public int getColumnCount() {
    return headers.length;
  }

  public int getRowCount() {
    return data.length;
  }

  public Object getValueAt(int row, int col) {
    if(col==data.length-1)
      return new ActionPanel(""+col);
    else
      return data[row][col];
  }

  public boolean isCellEditable(int row, int col) {
    return false;
  }

  public Class getColumnClass(int column) {
    return getValueAt(0, column).getClass();
  }

}
ServicesList.java - The panel which holds the table (BasePanel is a custom class, not related to the table)
public class ServicesList extends BasePanel {

  private JLabel label;
  private JTable grid;

  public ServicesList(SessionManager sessionManager){
    super(sessionManager);
    grid=new JTable() ;
    add(new JScrollPane(grid), BorderLayout.CENTER);
    layoutComponents();
  }

  public void layoutComponents(){

    ConfigAccessor dao=new ConfigAccessor(connectionUrl, connectionUser, connectionPass);
    String[] headers=I18N.get(dao.getServiceLabels());
    grid.setModel(new ServicesModel(dao.getServices(), headers));
    grid.setRowHeight(20);
    grid.getColumnModel().getColumn(headers.length-1).setCellRenderer(new ActionPanelRenderer());
    grid.setDefaultEditor(ActionPanel.class, new ActionPanelEditor());
    grid.removeColumn(grid.getColumnModel().getColumn(0));
    dao.close();

  }
}
Please can anyone at least address me to what I'm doing wrong? Code would be better, but examples or hints will do... ;)

Thank you very much in advance!!!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 24 2009
Added on Sep 26 2009
3 comments
972 views