Solved

How to put a JButton in a JTable ?

Posted on 1998-08-26
5
2,290 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
solarwind tftp server 2 44
Java and GPO 11 66
Problem to start Neon 20 82
what is a "java.lang.System Property"   ? 20 56
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 last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

943 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

16 Experts available now in Live!

Get 1:1 Help Now