Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-10-25
8
Medium Priority
?
234 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
Comment
Question by:chenwei
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
8 Comments
 
LVL 37

Expert Comment

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

Expert Comment

by:zzynx
ID: 12400005
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
ID: 12400009
Typo.
Read
         return theButton();
as
         return theButton;



0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:chenwei
ID: 12400102
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
ID: 12400393
See my previous comments. Just try them out.

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

Accepted Solution

by:
zzynx earned 120 total points
ID: 12400414
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 37

Expert Comment

by:zzynx
ID: 12408632
Thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

609 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