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

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?

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.

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.

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.

Related

Is there a Java Swing component similar to "..."-Buttons in PL/SQL Developer?

I want to add a button to my JTable. When this button is clicked, the contents of the corresponding cell should be shown in a new JTextArea. In PL/SQL Developer there is such a button, as you can see here:
Clickable :
Edit: I tried it like this:
public class TestKonfigTableCellRenderer extends DefaultTableCellRenderer {
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
JLabel l = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
JButton openButton = new JButton("...");
l.add(openButton);
return l;
}
}
But it doesn't seem to work. What did I do wrong ?
If by "it does not seem to work", you mean you cannot click on the button, that is to be expected.
The component returned by the renderer is not added to the Swing hierarchy. Instead, only its painted representation is used in the table for performance reasons. That is the reason a typical renderer always returns the same component after updating its state. See the "Concept: renderers and editors" section in the JTable tutorial for more information.
There are a number of posts available on how to include a clickable button in a JTable. For example this one.

Customrenderer for JTable is getting called multiple times

I want to color certain rows in JTable based on its value. But my customrenderer is getting called infinite times and the whole table is getting colored instead of certain rows.
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,int row, int column)
{
Component c = render.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
DefaultTableModel model = (DefaultTableModel)table.getModel();
String flag = (String)model.getValueAt(row, 0);
if(flag.equalsIgnoreCase("java"))
{
c.setBackground(new Color(0xE8F2FE)); //light blue
}
return c;
}
The same renderer is used for all cells. Once you change the background of the renderer that background is used for all the cells.
So you need an "else condition" to set the background to the default color. Something like:
else
c.setBackground( table.getBackground() );
You can also check out Table Row Rendering for an alternative approach to highlighting a rows background which can be simpler especially when different types of data are found in each column.

Adding Scrollbar to JTable cell with HTML rendering

I want to add scroll bar to a cell in JTable which has HTML contents that needs to be rendered. Tried creating a custom cell renderer with following code snap. Scroll bar appears in the cell however I am unable to scroll.
public Component getTableCellRendererComponent( JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column )
{
theLabel.setText((String)value);
JScrollPane thePane = new JScrollPane(theLabel);
return thePane;
}
Any clues will be appreciated
Thanks for comments in this question, realized this problem needs a combination of TableCellEditor and TableCellRenderer. Could resolve this issue with both editor and renderer.

Java - Is it possible to put an image and a String in the same JTable cell?

I know how to put a String into a JTable cell, and I know how to put an image into a JTable cell. But is it possible to put an image and a String into the SAME JTable cell?
The reason for this is that I have a 'status' column in my JTable, which at the moment contains either a green, amber or red image. And in order to fulfill the design requirement, I need to add some explanatory text alongside each image (so the text next to the green image would be "Online", the text next to the amber image would be "Unknown" and the text next to the red image would be "Offline"). I need to do this in a single column (or what looks/behaves like a single column) rather than two columns.
I have researched this, but found no info at all.
Yes.
You need to use a custom cell renderer. Check out How to use Tables for more details.
You actually have two choices, you could simply set the icon and the text of the cell or you could use the renderers tooltip text instead...
public class IconTextCellRemderer extend DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
setText(...);
setIcon(...);
setToolTipText(...);
return this;
}
}
Of course you need to apply the renderer to the column...
TableColumnModel tcm = table.getColumnModel();
tcm.getColumn(x).setCellRenderer(new IconTextCellRemderer());

TableCellRenderer selected cell problem

I want to implement a tablecellrenderer of a JTable component, which should show a different color depending on the cell data. I got this, but I can't change the color of the selected cell. I tried to do this:
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex)
{
if (isSelected) {
this.setBackground((Color)UIManager.get("Table.selectionBackground"));
this.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
} else {
this.setForeground((Color)UIManager.get("Table.foreground"));
this.setBackground((Color)UIManager.get("Table.background"));
this.setBorder(BorderFactory.createEmptyBorder());
}
...
}
but it does not work :S .. I can not see the problem because the JTable does not show anything different when I click on a cell.
I want to implement a tablecellrenderer of a JTable component, which should show a different color depending on the cell data
The code you posted does not do this. Basically all your code does is duplicate the default behaviour of the renderer
You may find the Table Row Rendering approach easier to implement.
Assuming you're using a JLabel as the base of the component, setting the background will have no effect unless you also set opaque to true. JLabels default to not opaque and so do not paint the background.

Categories

Resources