Solved

How to put a JButton in a JTable ?

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping3 challenge 14 79
reverse digits of a number using for loop 5 40
Desingning Refactoring existing code 2 24
How to determine if a string is a valid SHA value 7 28
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

813 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