sharif2008
sharif2008

Reputation: 2798

Hide a JTable row with click event [ java ]

I have JTable like this

enter image description here

I want to hide the row whenever the respective clear button(JButton) is pressed.And do other task such delete the row from mysql as the table is populated form database.

As i have two override function :-

one:

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

two:

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

where and how i have to change to achieve this.Or any other implementations details that would help me.Thanks

Upvotes: 0

Views: 1463

Answers (2)

camickr
camickr

Reputation: 324128

Table Button Column shows one way to add an ActionListener to a column of a table.

Upvotes: 1

roippi
roippi

Reputation: 25954

So basically you want the JButton to delete the row from your TableModel. You haven't shown any of the relevant code, or what kind of TableModel you have, but generally:

-write the listener, it will need a way to access your model

class MyListener
    implements ActionListener
{
    private TableModel model;

    public MyListener( TableModel m )
    {
        this.model = m;
    }

    public void actionPerformed( ActionEvent e )
    {
        // do something to this.model
    }
}

-attach it to your button

button.addActionListener( new MyListener(myModel) );

-lastly, realize that what you're looking at in the table is not a fully-featured JButton, but instead just a cell that's painted to look like a button. It still won't work when you click on it, even though you attached a listener.

You need to work around this last issue. There are a number of different approaches, many of which are laid out in this thread. The most basic is to hijack the cell editor to have it forward mouse events to the JButton, that's what I've done in the past. There are some other options in the linked thread. Good luck.

Upvotes: 1

Related Questions