Button text in JTable renderer not visible. Why? - java

I have a custom cell renderer set in JTable and it works but instead an "x" visible on buttons being table cells I see "..." (three dots). What did I miss ??
/***************************************************************************
* Listener reagujący na dodanie nowej wartości
**************************************************************************/
private static class ButtonRenderer extends JButton implements
TableCellRenderer {
/***********************************************************************
* Konstruktor
**********************************************************************/
public ButtonRenderer() {
super("x");
}
/***********************************************************************
* #see TableCellRenderer#getTableCellRendererComponent(JTable, Object,
* boolean, boolean, int, int)
**********************************************************************/
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
return this;
}
}

The size of the button isn't large enough to contain the rendered "x" plus the padding around it.
A solution would be to enlarge the table cell or reduce the padding (always assuming that the button has the same size as the table cell).

Related

How to stop minimize, scrollpane,resize from coloring all my jtable

As you can see in my pictures:
Before minimize:
After minimize
My renderer takes the last color that have used and paints all my table.
Bellow is my custom renderer class:
public class MyCellRenderer extends DefaultTableCellRenderer {
public static double fstValue;
public static double sndValue;
public MyCellRenderer() { }
public MyCellRenderer(double fstValue, double sndValue) {
this.fstValue = fstValue;
this.sndValue = sndValue;
//System.out.println(this.fstValue+" 2ndvalue"+this.sndValue+" ston constructor");
}
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if(!isSelected) {
if(compare(this.fstValue,this.sndValue)== 1){
c.setBackground(Color.GREEN);
}else if (compare(this.fstValue,this.sndValue)== -1) {
c.setBackground(Color.red);
}else{
c.setBackground(null);
}
}
return c;
}
}
I m updating the table fast, and I have no problem with that.
But when I resize or minimize or scroll down, the coloring change.
When I minimize and resize, my table change color all, but when I scroll down only the table that I scrolled change color.
I suspect that it has something to do with the repaint or paint method that my renderer calls and have trouble fixing it.
I use threads and every thread calls the code below for the update:
if( home.text().equals(hometmp.toString())==false)
{
MyCellRenderer cellRenderer = new MyCellRenderer(valuehm,valuehmt);
table1.setValueAt(home.text(),i-1,1);
}
You have two calls to super.getTableCellRendererComponent(...). Get rid of the second. Also, there is no need to cast the first call to a label. The method return a Component which has a setBackground() method.
You don't need the synchronized keyword on the method.

Set Background Color of a clicked table cell

I'm new to Java and I want to change the background color of a Specific cell, the one I clicked on, of a JTable.
I know that I have to use a MouseListener which I already did, also, the mousePressed. But at this point I am pretty lost.
EDIT: Forgot to add that the table is disabled, so you can't select a cell.
Can anyone help me? Thanks!
You must create a custom TableCellRenderer and pass it to the table
like this
public class ColorRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
// get the DefaultCellRenderer to give you the basic component
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
// apply your rules
if(table.isRowSelected(row) && table.isColumnSelected(col))
c.setBackground(Color.GREEN);
else{
c.setBackground(table.getBackground());
}
return c;
}
}
in this class we check if the given cell if the selected cell (which is pretty much what happens when we click it) and paint it differently (in my case I paint it green) , else we paint with the default color or any color you like.
don't forget to set the custom renderer you just created
table.setDefaultRenderer(Object.class, new ColorRenderer());
Edit 1
you must get the row and col of the clicked cell.
create 2 int variables that will hold the position
private int clickedRow=-1,clickedCol=-1;
add a mouse listener that updates the position variables
table.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent event) {
clickedRow= table.rowAtPoint(event.getPoint());
clickedCol= table.columnAtPoint(event.getPoint());
}
});
after that you change the renderer so it paints only the clicked cell with the special color
if( clickedRow == row && clickedCol == col){
c.setBackground(Color.GREEN);
}

Custom TableCellRenderer negative numbers align to right

I have Jtable with custom cells' renderer, which should align its content to right:
public class BriefViewCellRenderer extends JTextArea implements TableCellRenderer {
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
setText("-200");
}
}
JTable looks nice, except negative numbers. The minus sign apears behind the actual number.
Does anyone knows how to fix it?

How can I change background of several cells with a click?

I have a table which every cell keep a string. Actually I used the table as a page of book and it contains text. My problem is that I want to click on a cell and all similar words' background color in the table change to one unique color. For instance, when I click on the cell which contains 'and', all 'and' in my table become highlighted. I implemented defaulttablecellrenderer and I know that when java wants to draw table recall it for every cell. I tried to use intrinsic repetition capability and set the color but it does not work in the way I expected. These are my codes:
JTable t=new Jtabale();
//Filling my table....here....
t.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e){
int column=((JTable)e.getSource()).getSelectedColumn();
int row=((JTable)e.getSource()).getSelectedRow();
JTable table=(JTable)e.getComponent();
Object myS=table.getValueAt(row, column);//value of that cell saved
CustomCellRenderer r=(CustomCellRenderer)table.getCellRenderer(row, column);
r.setCell(myS);
table.repaint();
}
});
and this is my DefaultTableCellRenderer implementation:
public class CustomCellRenderer extends DefaultTableCellRenderer {
Object myStr;
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
Component c=super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if(myStr==value){
c.setBackground(Color.YELLOW);
}
else
c.setBackground(table.getBackground());
return c;
}
public void setCell(Object val){
myStr=val;
}
I do not know what's wrong with this code? This is only highlight that cell which I click. But I expected it change the background of several cells together! Even I put println inside the if but even it goes once in the if brace! I got confused. What is your idea?!

List selection is not happening in swing

HI, I've a JLIST and assigned it a cellRenderer. but i was not able select element in list. Actually it is selected but visually we can not see that it is selected means i was not able to see which item is selected in list.
Screen shot of my list:
and what is expected is
The second screen shot is without CellRenderer. But when i add CellRenderer i was not able to see the selected item in list.
is it normal behaviour that when you add CellRenderer to list.
what am i doing wrong ???
EDIT:-
this is my CellRenderer class:
public class ContactsRender extends JLabel implements ListCellRenderer {
private static final long serialVersionUID = 1L;
ImageIcon img;
public ContactsRender(){
setOpaque(true);
setIconTextGap(12);
setBackground(Color.WHITE);
setForeground(Color.black);
}
#Override
public Component getListCellRendererComponent(JList list,
Object value, int index, boolean isSelected,
boolean cellHasFocus) {
if(value != null){
User user = (User) value;
String pres = user.getPresence().toLowerCase();
if(pres.contains("unavailable")){
img = new ImageIcon("res/offline.jpg");
} else {
img = new ImageIcon("res/online.jpg");
}
setText(user.getName());
setIcon(img);
return this;
}
return null;
}
You implemented your cell renderer incorrectly. The renderer is responsible for setting the renderer background to the selection color.
Read the JList API and follow the link to the Swing tutorial on "How to Use Lists" where you will find working examples that use a JList. You will also find a section on writing a renderer and an example.
Edit: Also, I just noticed you are reading your icon in the renderer code. You should never do this. The icon should only be read once when the renderer is created and then you cache the Icon. Every time a cell needs to be repainted the renderer is called so it is not efficient to keep reading the icon.
On your cell renderer, you have to implement the case for isSelected is true. For your ListCellRenderer :
Component getListCellRendererComponent(JList<? extends E> list,
E value,
int index,
boolean isSelected,
boolean cellHasFocus)
{
if (!isSelected) doThis(index);
else doThatForSelectedItem(index);
}

Categories