Solved

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

Posted on 2004-10-25
233 Views
Last Modified: 2012-08-13
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?

0
Question by:chenwei
    8 Comments
     
    LVL 37

    Expert Comment

    by:zzynx
    Write your own table cell renderer
    0
     
    LVL 37

    Expert Comment

    by:zzynx
    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
     
    LVL 37

    Expert Comment

    by:zzynx
    Typo.
    Read
             return theButton();
    as
             return theButton;



    0
     

    Author Comment

    by:chenwei
    On my program I use

    model.addRow(...);

    to add an element. How can I add the button then?
    0
     
    LVL 37

    Expert Comment

    by:zzynx
    See my previous comments. Just try them out.

    You didn't expect a model.addButton(), did you?
    0
     
    LVL 37

    Accepted Solution

    by:
    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
     
    LVL 92

    Expert Comment

    by:objects
    0
     
    LVL 37

    Expert Comment

    by:zzynx
    Thanks
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Title # Comments Views Activity
    Null Boolean Values? 3 31
    linearIn  challenge 23 39
    java constructor error 8 46
    factorial example challenge 10 34
    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    856 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now