Solved

How to put a JButton in a JTable ?

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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 …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

747 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

15 Experts available now in Live!

Get 1:1 Help Now