Add JButton only to a specific JTable cell (or more) - java

I wanna add a button to some specific cells where i might get null characters from my database. I tried overriding 'TableCellRenderer' . But it keep adding buttons to entire column. What should i do? (example wd b better)

But it keep adding buttons to entire column.
Yes, a renderer is designed to work for the entire column.
If you want a different renderer for a different row then you can override the getCellRenderer(...) method. Something like:
public TableCellEditor getCellEditor(int row, int column)
{
Object value = getValueAt(row, column);
if (value == null)
{
return super.getCellEditor(row, column);
}
else
return getDefaultRenderer(value.getClass());
}
You would also need similar code for the getCellEditor(...) method.
You can also check out Table Button Column for an example of a button renderer/editor.

Related

How to change the background color of a specific table header and font color of a specific column in a JTable?

Is there a way to change the background color of the 3 circled table headers of a JTable?
I also want to change the font color of all the 3 circled columns to red.
An image of my table is down below. Thanks.
The way I know of is to create a TableCellRenderer that will handle the coloring of the foreground and background color.
You could create a simple coloring scheme where the color doesn't change. You often see this in tables with alternating colored rows. You could also use it for "conditional formatting". For example, highlight a cell if a value falls under a certain value. With a cell renderer, you will have better control of this.
Here is a link to Oracle's tutorial on renderers.
Lastly, since your question specifically asks to do this on specific cells, the link documentation states that
To specify a cell-specific renderer, you need to define a JTable subclass that overrides the getCellRenderer method. For example, the following code makes the first cell in the first column of the table use a custom renderer:
TableCellRenderer weirdRenderer = new WeirdRenderer();
table = new JTable(...) {
public TableCellRenderer getCellRenderer(int row, int column) {
if ((row == 0) && (column == 0)) {
return weirdRenderer;
}
// else...
return super.getCellRenderer(row, column);
}
};

Selected row in JTable is not highlighting

Well, this is my first post here. But I am little frustrated because I have search everywhere but nothing is working. I have JTable and code works as expected below lines right after if (value.equals("CMAU1294522")) .. however only one cell is show square box. I mouse clicked on a particular row, I want the entire row to show light gray (I think that is standard).
table = new JTable(sorter) {
public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
Component comp = super.prepareRenderer(renderer, row, col);
Object value = getModel().getValueAt(row, col);
UIManager.put("Table.editRow", new javax.swing.plaf.ColorUIResource(Color.YELLOW));
if(editingRow ==1){
table.setSelectionBackground(Color.red);
}
if (getSelectedRow() == row) {
table.setSelectionBackground(Color.red);
}
if (value.equals("CMAU1294522")) {
comp.setBackground(Color.red);
} else if (value.equals("PNCT")) {
comp.setBackground(Color.green);
} else if (NewJApplet.contReady.containsKey(value)) {
comp.setBackground(Color.CYAN);
} else if (NewJApplet.badCont.containsKey(value)) {
comp.setBackground(Color.red);
} else {
comp.setBackground(Color.white);
}
return comp;
}
Is there any way to do it in prepareRenderer function I already have?
I there any way to do it in PreparRenerere function I already have?
You code to get the "value" is wrong. You always get the value of the current cell being rendered. If you want to highlight the entire row based on a specific value then you need to hardcode the column:
Object value = getModel().getValueAt(row, ???);
Also, it looks like you are sorting the data in the table so you should be using getValueAt(...) instead of getModel().getValueAt(), so you check the data in the sort table, not the data in the unsorted model.
Check out the example code in Table Row Rendering for a working example.

Change the background color of a row in a JTable

In Netbeans(IDE 7.0) at advanced search keyrealese event I want to change the Jtable row colors under the condition.If system date(current date) is equal to that table(in database) coloumn name "Date" and system time(current time) is less than to table coloum name "Time" then I want to display that row in Green color else want to display in Red colour.Please share your knowledge.It will very usefull to me.:.respect.
hints-The table name is in DB is "Vehicle",The coloumns are "Date","Time","Veh_No","Model"..
I think we want to use "If" condition,and create two other variables to store system date and system time before give and condition.Plz help me..
Write a custom cell renderer.
public class CustomCellRenderer implements TableCellRenderer {
public Component getTableCellRendererComponent(....) {
Color color = getRowBackGroundColor(table, value, isSelected, hasFocus, row, column);
comp.setBackground(color);
return comp;
}
}
Override the prepareRenderer method of JTable. Logic remains similar to getTableCellRendererComponent().
I prefer overriding the prepareRenderer method.

Creating a JTable with alternating row colors

I am pretty new to java swing and i just started working on JTable. I want to create a JTable Which look like the above image? Can anybody help me beacause i am not so familiar with JTable?
Overriding the prepareRender(...) method of the JTable allows you to customize rendering for the entire row without providing custom renderers.
The basic logic would be something like:
JTable table = new JTable( model )
{
public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
{
Component c = super.prepareRenderer(renderer, row, column);
// Alternate row color
if (!isRowSelected(row))
c.setBackground(row % 2 == 0 ? getBackground() : Color.LIGHT_GRAY);
return c;
}
};
Check out Table Row Rendering for more information and working examples.
How can I achieve the border of the table header like the figure?
You can obtain a copy of the default table header renderer for a given L&F, as shown here, and modify it as desired. With some caveats, you can modify the renderer for a particular TableColumn, as shown here.

AbstractTableModel setValueAt not firing upon Jbutton Click

I have a custom TableModel that extends the AbstractTableModel.
public class IndicatorPropertyTableModel extends AbstractTableModel
The setValueAt function I wrote fires appropriately in cases when you type a new value into the editable cell and then move on to another cell. The problem I am having is that for the last editable cell the user will immediately click a JButton to continue. This does not trigger the setValueAt function thus not saving the data. Is there a good way to ensure that this value is always stored when a user immediately hits a button after editing a cell? setValueAt function below for reference if needed.
public void setValueAt(Object value, int row, int col) {
if (value == null)
return;
if (col == 0) {
//this.indicatorArguments[row].setIsSelected(value);
} else if (col == 2) {
this.indicatorArguments[row].setValue(value);
}
this.fireTableCellUpdated(row, col);
}
Thanks for any help you can provide.
I assume that your button is outside the table.
In the event listener or action of your button you would need a reference to the table. This table you could then ask whether isEditing returns true. Then you would get the current editor and after validating the last input value either call stopCellEditing to commit the value or cancelCellEditing to undo the value.
In short:
if(table.isEditing()){
table.getCellEditor().stopCellEditing();
}
would always commit not yet entered values.
Checking if the table is editing requires you to add code to all buttons on your GUI.
You may be able to use a simple one line solution that avoids this. Check out Table Stop Editing.

Categories

Resources