Solved

How to put a JButton in a JTable ?

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

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