Like the title says, is there a simple way to change the width of the gridlines in JTable?
I found out, that there is a simple way to change the color of the showed gridlines in the Jtable but I could not find any methods to change the gridline width.
One of the ways is to override the prepareRenderer(...) method of JTable class, using BorderFactory to create custom border:
JTable table = new JTable( model ) {
public Component prepareRenderer( TableCellRenderer renderer, int row, int column) {
JComponent jc = (JComponent)super.prepareRenderer(renderer, row, column);
jc.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
return jc;
}
};
Related
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.
I created inventory application, when display transaction list from database I use JTabel inside JScrollPane. Set JScrollPane horizontalScrollBar Policy to AS_NEED and JTable setAutoResize to false and Column width set using TableColumnModel as I need. When Width set manually table width going out of viewport and HorizotalScrollBar activate. But I run program and Use scrollbar to see column outside viewport, text and header text get blur during scroll.
Follow given code an screen shoot of project
SetTableModel using DefaultTableModel
DefaultTableModel dm;
dm = new DefaultTableModel(0, 0){
#Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
String titleCol[] = new String[]{"ID","Invoice No","Date","Account Name","Total Amount","Taxeble Amount","Tax %","Tax Amount","Discount Amount","Grand Total"};
dm.setColumnIdentifiers(titleCol);
table1.setModel(dm);
table1.getTableHeader().setFont(new Font("Tahoma", Font.BOLD, 14));
Set Width of Column using TableColumnModel
table1.getTableHeader().setPreferredSize(new Dimension(table1.getTableHeader().getPreferredSize().width, 35));
TableColumnModel columnModel = table1.getColumnModel();
columnModel.getColumn(0).setMinWidth(50);
columnModel.getColumn(0).setMaxWidth(50);
columnModel.getColumn(1).setMinWidth(85);
columnModel.getColumn(1).setMaxWidth(85);
columnModel.getColumn(2).setMinWidth(100);
columnModel.getColumn(2).setMaxWidth(100);
columnModel.getColumn(3).setMinWidth(250);
columnModel.getColumn(4).setMinWidth(100);
columnModel.getColumn(4).setMaxWidth(100);
columnModel.getColumn(5).setMinWidth(130);
columnModel.getColumn(5).setMaxWidth(130);
columnModel.getColumn(6).setMinWidth(75);
columnModel.getColumn(6).setMaxWidth(75);
columnModel.getColumn(7).setMinWidth(130);
columnModel.getColumn(7).setMaxWidth(130);
columnModel.getColumn(8).setMinWidth(130);
columnModel.getColumn(8).setMaxWidth(130);
columnModel.getColumn(9).setMinWidth(130);
columnModel.getColumn(9).setMaxWidth(130);
Screenshot of Before using Horizotal ScrollBar
Screenshot of After using Horizotal ScrollBar
So Is there any solution to avoid blur.
Your problem in next line:
table1.getTableHeader().setPreferredSize(new Dimension(table1.getTableHeader().getPreferredSize().width, 35));
Set height of your header with help of TableCellRenderer, for example:
DefaultTableCellRenderer defaultRenderer = (DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer();
defaultRenderer.setPreferredSize(new Dimension(0,35));
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.
I have a sortable JTable set up to use a custom extension of the AbstractTableModel. However, some behavior of this table is what I expected, and I would love some advice on how to figure this out.
I have the JTable set up to be sortable using:
thisJTable.setAutoCreateRowSorter(true);
This allows me to sort the table by clicking on the column headers as expected.
However, I find that when I sort the table by clicking on the column headers, the formatting (background and foreground color) of my rows are not sorted as well.
I had set up those rows to be color-coded based on the values they contain. When I sort by column header the formatting at a given row NUMBER stays the same (although the content that was previously in that row moved).
The color of the row is set by overriding the default prepareRenderer call for the JTable:
thisTable = new JTable(thisModel){
//Set up custom rendering - Sets background color of row to correct value
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
CustTableModel thisModel = (CustTableModel) getModel();
c.setBackground(thisModel.getRowBackgroundColor(row));
c.setForeground(thisModel.getRowForeColor(row));
return c;
}
};
Is there a better/different way to approach this?
Should I be using a different method to do my rendering, a method which would update the rendering of the JTable on a sort?
Or do I want to look into writing my own sorting method?
Solution (Thanks mKorbel!)
I thought I would post my solution, since I had to play with this a bit since I wasn't sure if the new index would be passed to the prepareRenderer as well.
thisTable = new JTable(thisModel){
//Set up custom rendering - Sets background color of row to correct value
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
int viewIdx = row;
int modelIdx = convertRowIndexToModel(viewIdx);
Component c = super.prepareRenderer(renderer, row, column);
CustTableModel thisModel = (CustTableModel) getModel();
c.setBackground(thisModel.getRowBackgroundColor(modelIdx));
c.setForeground(thisModel.getRowForeColor(modelIdx));
return c;
}
};
you have to convert row index from View to the Model
int modelRow = convertRowIndexToModel(row);
You can enable table sorting by clicking on header using this
table.setAutoCreateRowSorter(true);
for more information visit this site http://www.codejava.net/java-se/swing/6-techniques-for-sorting-jtable-you-should-know
i try to change the color of fields in a JTable according to their value. I don't want to change any color of the first column but it changes anyway in a buggy way(some fileds are not correctly filed like University and Possible_Reviewer):
My code is as following:
table.setDefaultRenderer(Object.class, new CustomRenderer());
private class CustomRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,boolean hasFocus, int row, int col){
Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
try {
Double val = Double.parseDouble(value.toString());
if(col == 0){
comp.setBackground(Color.white);
} else {
comp.setBackground(changeColor(val));
}
} catch (NumberFormatException e){}
return( comp );
}
private Color changeColor(Double val) {
//returns a Color between red and green depending on val
}
}
The weird thing is that when i use "col == 2" it turns the second column white but the first remains strangely colored.
Anyone an idea?
You should extend JTable class and override this method:
public TableCellRenderer getCellRenderer(int row, int column){}
Otherwise JTable will use the same renderer for each cell in the same column.
EDIT:
Like #Mark Bramnik pointed out, it's better to not instantiate a new TableCellRenderer object for every getCellRenderer call. You could implement a method like the following:
setCellRenderer(int row, int col, TableCellRenderer render)
and store the renderer in the extended JTable itself.
How to Use Tables: Using Custom Renderers mentions this alternative approach: "To specify that cells in a particular column should use a renderer, you use the TableColumn method setCellRenderer()."
Addendum: A benefit of this approach is that the renderer "sticks" to the column if the user drags it to a different position. In this example, replace setDefaultRenderer() with setCellRenderer().
table.getColumnModel().getColumn(DATE_COL).setCellRenderer(new DateRenderer());