Solved

How to put a JButton in a JTable ?

Posted on 1998-08-26
5
2,330 Views
Last Modified: 2008-02-01
How to add a JButton in the cells of a JTable and add some behavior to them ?
0
Comment
Question by:mmouries
[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
  • 3
  • 2
5 Comments
 
LVL 3

Expert Comment

by:sailwind
ID: 1222608
You would need to write a custom renderer for the buttons. Here's
an example of what it should probably look like:

// renderer for a list item with an icon, draws the icon
public class TableItemRenderer extends Object
implements TableCellRenderer
{
    JButton theButton;

    // constructor
    public TableItemRenderer() {
        theButton = new JButton();
        theButton.setOpaque(true); // sets it so that it is not transparent
    }

    public Component getTableCellRendererComponent(JTable table,
                            Object value,
                            boolean isSelected,
                            boolean cellHasFocus,
                            int row,
                            int column)

    {
        TableItem ti=null;

        if(value instanceof TableItem) ti = (TableItem) value;

        theLabel.setFont(table.getFont());

        if(ti != null)
        {
            theButton.setText(" "+ti.getTitle());
        }
        else
        {
            theButton.setText("");
        }
       return theButton;
    }

}

And write your own tableButton class. In your tableButton class,
add actionListeners to them and define the button's behavior.

In your table model, you will be adding the tableButtons to the
cells. Remember to add the renderer to every column that contains
the buttons.

This page have some great examples:

http://java.sun.com/docs/books/tutorial/ui/swing/table.html
0
 

Author Comment

by:mmouries
ID: 1222609
>And write your own tableButton class. In your tableButton class,
>add actionListeners to them and define the button's behavior.

if i click on a button in the Jtable, how can i know the line and the column of the
button clicked ?

>In your table model, you will be adding the tableButtons to the
>cells. Remember to add the renderer to every column that contains  the buttons.

how to add the tableButton in the table model when it is a JDBCAdapter ?
I have to to set the Renderer after the model is read and from a configuration file

This page have some great examples: http://java.sun.com/docs/books/tutorial/ui/swing/table.html

yes but they are trivials cause they use boolean and the JCheckBox is the Default
editing and rendering for boolean
0
 
LVL 3

Expert Comment

by:sailwind
ID: 1222610
      if i click on a button in the Jtable, how can i know the line and the column of the
       button clicked ?

Like you would for any other table, invoke:

public int getSelectedRow() and
public int getSelectedColumn().
0
 

Author Comment

by:mmouries
ID: 1222611
Adjusted points to 150
0
 
LVL 3

Accepted Solution

by:
sailwind earned 150 total points
ID: 1222612
posting answers again hoping that it can solve your problem.
--
if i click on a button in the Jtable, how can i know the line and the column of the
button clicked ?
--

You can get the line and column of the button clicked by invoking

public int getSelectedRow();
public int getSelectedColumn();

in addition to:

http://java.sun.com/docs/books/tutorial/ui/swing/table.html 

sun now has a great example on how to customize your table items. Please
check them out at:

http://java.sun.com/products/jfc/tsc/swingdoc-current/trees-tables.html

---
how to add the tableButton in the table model when it is a JDBCAdapter ?
      I have to to set the Renderer after the model is read and from a configuration file
---

You should create your own wrapper class to cast your JDBC objects into the
tableButton objects. This way you can inline the data coming from the adapter
to the button.



0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb entity bean example 2 39
Cisco ASA: Java web start no go, asdm launcher no go 3 58
Setup GlassFish 4 37
Netbeans and org.apache.commons.lang3 issue 3 30
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

730 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