How to add object as row-element in a table?

My program looks as follow:

import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;


/**
 * @ *
 * Folgendes auswählen, um die Schablone für den erstellten Typenkommentar zu ändern:
 * Fenster>Benutzervorgaben>Java>Codegenerierung>Code und Kommentare
 */
public class TestSwing extends JFrame {

     private javax.swing.JPanel jContentPane = null;

     private javax.swing.JScrollPane jScrollPane = null;
     private javax.swing.JTable jTable = null;
     private DefaultTableModel model = null;
     
     public static void main(String[] args) {
          new TestSwing().show();
     }
     /**
      * This is the default constructor
      */
     public TestSwing() {
          super();
          initialize();
     }
     /**
      * This method initializes this
      *
      * @return void
      */
     private void initialize() {
          this.setSize(300, 200);
          this.setContentPane(getJContentPane());
          this.setTitle("Test Swing");
     }
     /**
      * This method initializes jContentPane
      *
      * @return javax.swing.JPanel
      */
     private javax.swing.JPanel getJContentPane() {
          if (jContentPane == null) {
               jContentPane = new javax.swing.JPanel();
               jContentPane.setLayout(null);
               jContentPane.add(getJScrollPane(), null);
          }
          return jContentPane;
     }
     /**
      * This method initializes jScrollPane
      *
      * @return javax.swing.JScrollPane
      */
     private javax.swing.JScrollPane getJScrollPane() {
          if(jScrollPane == null) {
               jScrollPane = new javax.swing.JScrollPane();
               jScrollPane.setViewportView(getJTable());
               jScrollPane.setBounds(6, 5, 285, 97);
          }
          return jScrollPane;
     }
     /**
      * This method initializes jTable
      *
      * @return javax.swing.JTable
      */
     private javax.swing.JTable getJTable() {
          model = new DefaultTableModel();
          model.addColumn("click me");
          if(jTable == null) {
               jTable = new javax.swing.JTable(model);
          }
          model.addRow(???);  // what should I do here?

          return jTable;
     }
}

I want to add a JButton as an element into the table. How can I do that?

chenweiAsked:
Who is Participating?
 
zzynxSoftware engineerCommented:
So
- have a class MyButtonTableCellRenderer like the one I posted.
- call
      getColumnModel().getColumn(0).setCellRenderer( new MyButtonTableCellRenderer() );
  on your table and you'll see buttons appear in the first column of your table.
0
 
zzynxSoftware engineerCommented:
Write your own table cell renderer
0
 
zzynxSoftware engineerCommented:
In it's easiest form:

public class MyButtonTableCellRenderer extends DefaultTableCellRenderer {
   
    JButton theButton = new JButton("click me");

    public MyButtonTableCellRenderer() {
    }
   
    public Component getTableCellRendererComponent(JTable table, Object value,
                                                   boolean isSelected,
                                                   boolean hasFocus,
                                                   int row,
                                                   int column) {
        return theButton();
    }
}

To "install" it:

// x = the index of the column you want to see the button appear
yourTable.getColumnModel().getColumn(x).setCellRenderer( new MyButtonTableCellRenderer() );  
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
zzynxSoftware engineerCommented:
Typo.
Read
         return theButton();
as
         return theButton;



0
 
chenweiAuthor Commented:
On my program I use

model.addRow(...);

to add an element. How can I add the button then?
0
 
zzynxSoftware engineerCommented:
See my previous comments. Just try them out.

You didn't expect a model.addButton(), did you?
0
 
zzynxSoftware engineerCommented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.