javalearner
javalearner

Reputation: 103

how set Image in jTable cell when click(Mouse Event) the row?

How to set the Image in jTable cell when click(Mouse event) the row?If I select first row the image will display in that row.Then i click the second row, the image will show in the second row?how to do this using table cell renderer or prepare renderer?

Upvotes: 0

Views: 1585

Answers (3)

camickr
camickr

Reputation: 324118

This is your 4th question on displaying an image in a JTable, so I'm guessing you already know how to do that.

So if you want to update a row when the selection changes then you will need to use a ListSelectionListener. Then when the listener fires you will need to update the TableModel to remove the icon from the previous row and update the icon in the current row.

JList: previous selected item shows you you can get the row numbers to update.

Upvotes: 1

trashgod
trashgod

Reputation: 205785

If you just want the image to appear in the table cell, use the default renderer for ImageIcon and ensure that your TableModel returns ImageIcon.class for that column.

If you want the image to appear in response to a click, consider using a variation of TablePopupEditor with setClickCountToStart(1) and your image as an Icon.

Upvotes: 2

NotANormalNerd
NotANormalNerd

Reputation: 415

The best way to do this, is to make you own table cell renderer.

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            if(isSelected){
               return new Image(); // if selected
            }
return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); // if not selected do the normal stuff
}

Something like this.

Upvotes: -2

Related Questions