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?
Related
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.
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);
}
I am trying to implement a button such that when it is clicked, the color of a cell in that row changes colors. I have a cellRenderer class:
public class MyCellRenderer extends javax.swing.table.DefaultTableCellRenderer {
public java.awt.Component getTableCellRendererComponent(javax.swing.JTable table, java.lang.Object value, boolean isSelected, boolean hasFocus, int row, int column) {
java.awt.Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
cellComponent.setBackground(java.awt.Color.RED);
return cellComponent;
}
}
that gets invoked as follows:(for column 15 let's say):
MyCellRenderer mcr = new MyCellRenderer();
Mytable.getColumnModel().getColumn(15).setCellRenderer(mcr);
notice how it gets invoked on a ColumnModel object. Is there a method to select a cell (row, column) and change its color based on the coordinates. Such that it can be invoked by a JTable object? For example:
Mytable.colorCell(1,7); //colors cell in row 1, column 7
Thank you
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?!
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).