I know there are a lot of questions on this subject but for some reason just cant find my way around this thing.
I need to change row color if two columns are not having the same values.
seen this link: http://tips4java.wordpress.com/2010/01/24/table-row-rendering/
but can't make it work....
tried this also:
public void colorRow(JTable table){
for(int column=0;column<table.getRowCount();column++){
for(int row=0;row<table.getRowCount();row++){
TableCellRenderer renderer = table.getCellRenderer(row, column);
Component comp = table.prepareRenderer(renderer, row, column);
Float sinh = (Float)table.getModel().getValueAt(row,6);
Float kol = (Float)table.getModel().getValueAt(row, 5);
if(!kol.equals(sinh)){
comp.setBackground(Color.YELLOW);
}
}
}
}
but it paints all the rows in the table if there is a row where columns 5 and 6 are not having the same value.
would appreciate any help
Update(SSCCE):
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class table extends JFrame {
private JPanel contentPane;
private JTable table;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
table frame = new table();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public table() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 495, 317);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new GridLayout(1, 0, 0, 0));
DefaultTableModel model = new DefaultTableModel();
model.addColumn("ID");
model.addColumn("Value 1");
model.addColumn("Value 2");
int id1=1;
int id2=2;
int id3=3;
Float one1=3.0f;
Float two1=4.0f;
Float one2=2.0f;
Float two2=2.0f;
Float one3=1.0f;
Float two3=2.0f;
model.insertRow(0, new Object [] {id1, one1,two1});
model.insertRow(0, new Object [] {id2, one2,two2});
model.insertRow(0, new Object [] {id3, one3,two3});
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane);
table = new JTable(model);
scrollPane.setViewportView(table);
colorRow(table);
}
public void colorRow(JTable table){
for(int column=0;column<table.getColumnCount();column++){
for(int row=0;row<table.getRowCount();row++){
TableCellRenderer renderer = table.getCellRenderer(row, column);
Component comp = table.prepareRenderer(renderer, row, column);
comp.setBackground(getBackground());
Float sinh = (Float)table.getModel().getValueAt(row,2);
Float kol = (Float)table.getModel().getValueAt(row, 1);
if(!kol.equals(sinh)){
comp.setBackground(Color.YELLOW);
}
}
}
}
}
I just tried the solution from this post with your SSCCE and it works. So delete the colorRow() method, then in your constructor replace the table initialization table = new JTable(model); with this:
table = new JTable(model) {
#Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component comp = super.prepareRenderer(renderer, row, column);
int modelRow = convertRowIndexToModel(row);
Float sinh = (Float) getModel().getValueAt(modelRow, 2);
Float kol = (Float) getModel().getValueAt(modelRow, 1);
comp.setBackground(kol.equals(sinh) ? getBackground() : Color.YELLOW);
return comp;
}
};
You fixed the problem I pointed out in the original code sample: for(int column=0; column < table.getRowCount(); column++){ by using the column count, but that didn't resolve the reason for your post.
Related
Only the most recent comboBox values are showing up in both rows.
UPDATED MVCE Example:
package main;
import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
public class SwingDemo {
public static void main(String[] argv) throws Exception {
DefaultTableModel model = new DefaultTableModel();
model.addColumn("Column 1");
JTable table = new JTable(model);
Font font = new Font("Verdana", Font.PLAIN, 12);
table.setFont(font);
table.setRowHeight(30);
table.setBackground(Color.orange);
table.setForeground(Color.white);
JFrame frame = new JFrame();
frame.setSize(600, 400);
frame.add(new JScrollPane(table));
frame.setVisible(true);
//add 1st row
model.addRow("".split(""));
TableColumn testColumn1stRow = table.getColumnModel().getColumn(0);
JComboBox<String> comboBoxTest = new JComboBox<String>();
ArrayList<String> testArray = new ArrayList<String>();
testArray.add("one");
testArray.add("two");
testArray.add("three");
for (int i = 0; i < testArray.size(); i++) {
comboBoxTest.addItem(testArray.get(i));
}
testColumn1stRow.setCellEditor(new DefaultCellEditor(comboBoxTest));
//add 2nd row
model.addRow("".split(""));
TableColumn testColumn2ndRow = table.getColumnModel().getColumn(0);
JComboBox<String> comboBoxTest2 = new JComboBox<String>();
ArrayList<String> testArray2 = new ArrayList<String>();
testArray2.add("four");
testArray2.add("five");
testArray2.add("six");
for (int i = 0; i < testArray2.size(); i++) {
comboBoxTest2.addItem(testArray2.get(i));
}
testColumn2ndRow.setCellEditor(new DefaultCellEditor(comboBoxTest2));
}
}
Essentially every time I add a row it runs this code and then every row in column has the value of the most recently added combobox.
Any ideas on how to get around this?
I think there is a misconception: a JTable doesn't have TableColumn objects for each row and each column - it has only TableColumn objects for columns.
That means that with
TableColumn testColumn1stRow = table.getColumnModel().getColumn(0);
// [...]
TableColumn testColumn2ndRow = table.getColumnModel().getColumn(0);
the variable testColumn1stRow and testColumn2ndRow reference the same TableColumn object (the one for the first column.)
If you need different JComboBoxes as cell editors for different rows, you must create your own JTable subclass and override public TableCellEditor getCellEditor(int row, int column):
public class MyTable extends JTable {
// [...]
#Override
public TableCellEditor getCellEditor(int row, int column) {
if (column == 0) {
List<String> values = getValuesForRow(row);
JComboBox<String> comboBoxTest = new JComboBox<String>();
for (int i = 0; i < testArray.size(); i++) {
comboBoxTest.addItem(testArray.get(i));
}
return new DefaultCellEditor(comboBoxTest);
} else {
return super.getCellEditor(row, column);
}
}
// [...]
}
I've invented a method List<String> getValuesForRow(int row) to fill in the possible values for a row just to keep the example short.
This should be simple, but I can't figure out what is going wrong. I need my table to display with a fairly large font size but the table painter is not honoring the height of the cell renderer.
I've seen this post and it's working brilliantly for when my preferences window has been given a changed font size. But although the renderer knows what font size to use, the table on initial display is using the standard row height (16). Surely the table painter should automatically take into account the preferred height of the renderer? Or do I actually have to manually tell it what height to use?
I've tried a call to doLayout() as shown below, but it doesn't make any difference.
Here's an SSCCE to demonstrate the problem:
public class IncorrectRowHeight extends JPanel
{
private class MyCellRenderer extends JTextField implements TableCellRenderer
{
private MyCellRenderer()
{
setFont(new Font("SansSerif", Font.PLAIN, 30));
setBorder(BorderFactory.createEmptyBorder());
}
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column)
{
setText(value.toString());
return this;
}
}
public IncorrectRowHeight()
{
super(new BorderLayout());
add(new JTextField(25), BorderLayout.PAGE_START);
String[][] data = {
{"a", "b", "c", "d", "e"},
{"f", "g", "h", "i", "j"},
{"k", "l", "m", "n", "o"},
{"p", "q", "r", "s", "t"}
};
String[] cols = {"h1", "h2", "h3", "h4", "h5"};
JTable t = new JTable(data, cols);
t.setGridColor(Color.GRAY);
t.setPreferredScrollableViewportSize(new Dimension(300, 65));
TableColumnModel model = t.getColumnModel();
for (int i = 0; i < model.getColumnCount(); i++)
{
TableColumn column = model.getColumn(i);
column.setCellRenderer(new MyCellRenderer());
}
JScrollPane scroller = new JScrollPane(t);
add(scroller, BorderLayout.CENTER);
//t.doLayout(); // doesn't help matters
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("IncorrectRowHeight");
JComponent newContentPane = new IncorrectRowHeight();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
It produces the following:
This ain't rocket science! What am I doing wrong?
Probably this method help you
public static void updateRowHeight(JTable table, int margin) {
final int rowCount = table.getRowCount();
final int colCount = table.getColumnCount();
for (int i = 0; i < rowCount; i++) {
int maxHeight = 0;
for (int j = 0; j < colCount; j++) {
final TableCellRenderer renderer = table.getCellRenderer(i, j);
maxHeight = Math.max(maxHeight, table.prepareRenderer(renderer, i, j).getPreferredSize().height);
}
table.setRowHeight(i, maxHeight + margin);
}
}
Use this method after the table is populated with the data. margin parameter is used for additional increment of row height. If not required - use 0.
Here is your example with the correct cell height.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
/**
* <code>IncorrectRowHeight</code>.
*/
public class IncorrectRowHeight extends JPanel {
private class MyCellRenderer extends JTextField implements TableCellRenderer {
private MyCellRenderer() {
setFont(new Font("SansSerif", Font.PLAIN, 30));
setBorder(BorderFactory.createEmptyBorder());
}
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
setText(value.toString());
return this;
}
}
public IncorrectRowHeight() {
super(new BorderLayout());
add(new JTextField(25), BorderLayout.PAGE_START);
String[][] data = {{"a", "b", "c", "d", "e"}, {"f", "g", "h", "i", "j"}, {"k", "l", "m", "n", "o"}, {"p", "q", "r", "s", "t"}};
String[] cols = {"h1", "h2", "h3", "h4", "h5"};
JTable t = new JTable(data, cols);
t.setGridColor(Color.GRAY);
t.setPreferredScrollableViewportSize(new Dimension(300, 65));
TableColumnModel model = t.getColumnModel();
for (int i = 0; i < model.getColumnCount(); i++) {
TableColumn column = model.getColumn(i);
column.setCellRenderer(new MyCellRenderer());
}
updateRowHeight(t, 0);
JScrollPane scroller = new JScrollPane(t);
add(scroller, BorderLayout.CENTER);
}
public static void updateRowHeight(JTable table, int margin) {
final int rowCount = table.getRowCount();
final int colCount = table.getColumnCount();
for (int i = 0; i < rowCount; i++) {
int maxHeight = 0;
for (int j = 0; j < colCount; j++) {
final TableCellRenderer renderer = table.getCellRenderer(i, j);
maxHeight = Math.max(maxHeight, table.prepareRenderer(renderer, i, j).getPreferredSize().height);
}
table.setRowHeight(i, maxHeight + margin);
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("IncorrectRowHeight");
JComponent newContentPane = new IncorrectRowHeight();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGUI();
}
});
}
}
Use table.setRowHeight(table.getFontMetrics(font).getHeight());
package com.logicbig.example;
import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import java.awt.*;
public class IncorrectRowHeight extends JPanel {
private static final Font font = new Font("SansSerif", Font.PLAIN, 30);
private class MyCellRenderer extends JTextField implements TableCellRenderer {
private MyCellRenderer() {
setFont(font);
setBorder(BorderFactory.createEmptyBorder());
}
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
setText(value.toString());
return this;
}
}
public IncorrectRowHeight() {
super(new BorderLayout());
add(new JTextField(25), BorderLayout.PAGE_START);
String[][] data = {
{"a", "b", "c", "d", "e"},
{"f", "g", "h", "i", "j"},
{"k", "l", "m", "n", "o"},
{"p", "q", "r", "s", "t"}
};
String[] cols = {"h1", "h2", "h3", "h4", "h5"};
JTable t = new JTable(data, cols);
t.setRowHeight(t.getFontMetrics(font).getHeight());
t.setGridColor(Color.GRAY);
t.setPreferredScrollableViewportSize(new Dimension(300, 65));
TableColumnModel model = t.getColumnModel();
for (int i = 0; i < model.getColumnCount(); i++) {
TableColumn column = model.getColumn(i);
column.setCellRenderer(new MyCellRenderer());
}
JScrollPane scroller = new JScrollPane(t);
add(scroller, BorderLayout.CENTER);
//t.doLayout(); // doesn't help matters
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("IncorrectRowHeight");
JComponent newContentPane = new IncorrectRowHeight();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
I get an error when I remove a row a sorted row in a JTable.
The error appears only when the table is sorted, and I know where the error source is:
the method updateRowHeights() in the tableChanged causes an Exception java.lang.ArrayIndexOutOfBoundsException.
I guess that the line int rowHeight = table.getRowHeight(); causes the problem,
but I don't know why.
Here is my code:
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.*;
public class TableExample {
String [] title = new String [] {"Title A", "Title B"};
Object [][] data = new String [][] {{"aaaaaaaaaaaa aaaaaa aaaaaaa", "bbbbbbbb bbbb bbbbbb bbbbbb"},
{"cccccccccc cccccccc ccccccc", "ddddddd ddd dddddddd dddddd"},
{"eeeeeeeeee eeeeeeee eeeeeee", "fffffff ffff ffffff fffffff"}};
private JTable table;
private JFrame frame;
private DefaultTableModel model;
private JScrollPane pane1;
TableExample() {} //constructor
public JPanel createTable() {
JPanel panel = new JPanel();
//creating tables and table models
model = new DefaultTableModel(data, title);
table = new JTable(model);
table.getModel().addTableModelListener(new TableModelListener() {
#Override
public void tableChanged(TableModelEvent e) {
updateRowHeights();
}
});
//enable table sorting
table.setAutoCreateRowSorter(true);
pane1 = new JScrollPane(table);
pane1.setPreferredSize(new Dimension(300,300));
updateRowHeights();
panel.add(pane1);
//delete a row after del keystroke
keyBindings();
return panel;
}
void showTable() {
//create and show frame
JPanel testPanel = createTable();
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(testPanel);
frame.pack();
frame.setVisible(true);
}//showTable
void updateRowHeights() {
for (int row = 0; row < table.getRowCount(); row++) {
int rowHeight = table.getRowHeight();
Component comp = table.prepareRenderer(table.getCellRenderer(row, 1), row, 1);
rowHeight = Math.max(rowHeight, comp.getPreferredSize().height);
table.setRowHeight(row, rowHeight);
}
}
void keyBindings() {
int condition = JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT;
InputMap inputMap = table.getInputMap(condition);
ActionMap actionMap = table.getActionMap();
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "delete");
actionMap.put("delete", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
model.removeRow(row);
}
});
}
public static void main(String[] args) {
TableExample example = new TableExample();
example.showTable();
}//main
}//TableExample
How can I solve this problem?
As noted here, "When using a sorter, always remember to translate cell coordinates." In your delete action, for example,
row = table.convertRowIndexToModel(row);
A similar problem afflicts updateRowHeights(), although I did not pursue this.
Also consider overriding getPreferredScrollableViewportSize(), instead of calling setPreferredSize(); more details here.
As tested:
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.*;
public class TableExample {
String[] title = new String[]{"Title A", "Title B"};
Object[][] data = new String[][]{
{"aaaaaaaaaaaa aaaaaa aaaaaaa", "bbbbbbbb bbbb bbbbbb bbbbbb"},
{"cccccccccc cccccccc ccccccc", "ddddddd ddd dddddddd dddddd"},
{"eeeeeeeeee eeeeeeee eeeeeee", "fffffff ffff ffffff fffffff"}};
private JTable table;
private JFrame frame;
private DefaultTableModel model;
private JScrollPane pane1;
public JPanel createTable() {
JPanel panel = new JPanel();
//creating tables and table models
model = new DefaultTableModel(data, title);
table = new JTable(model);
//enable table sorting
table.setAutoCreateRowSorter(true);
pane1 = new JScrollPane(table);
panel.add(pane1);
//delete a row after del keystroke
keyBindings();
return panel;
}
void showTable() {
//create and show frame
JPanel testPanel = createTable();
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(testPanel);
frame.pack();
frame.setVisible(true);
}//showTable
void keyBindings() {
int condition = JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT;
InputMap inputMap = table.getInputMap(condition);
ActionMap actionMap = table.getActionMap();
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "delete");
actionMap.put("delete", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
row = table.convertRowIndexToModel(row);
model.removeRow(row);
}
});
}
public static void main(String[] args) {
TableExample example = new TableExample();
example.showTable();
}//main
}//TableExample
I am making a GUI in which i tried to indent the column name.I am also pasting my source code. I am using the GridBagConstraints. This my code where i create a GridBagConstraints but nothing seems to happen
private final static Dimension VOLUME_FIELD_DIMENSION = new Dimension(200, 25);
private final static Dimension NAME_FIELD_DIMENSION = new Dimension(200, 25);
private final static Dimension IMAGE_FIELD_DIMENSION = new Dimension(200, 25);
private final static Dimension DATE_FIELD_DIMENSION = new Dimension(150, 25);
private final static Dimension STATUS_FIELD_DIMENSION = new Dimension(100, 25);
private final static Dimension IMAGE_PANEL_DIMENSION = new Dimension(1000,250);
private static void ImagePanel(ArrayList<ResourceListObject> arr, JPanel imagelist)
{
for(int i=0 ; i < arr.size();i++)
{
try {
// column 1: name
JTextField t1 = createTextField(arr.get(i).getName(), NAME_FIELD_DIMENSION);
// column 2: Created At
JTextField t2 = createTextField(arr.get(i).getCreatedDate(), DATE_FIELD_DIMENSION);
// column 3: Status
JTextField t3 = createTextField(arr.get(i).getStatus(), STATUS_FIELD_DIMENSION);
// column 4: Image Id
JTextField t4 = createTextField(arr.get(i).getID(), IMAGE_FIELD_DIMENSION);
//column 5:Volume Id
JTextField t5 = createTextField(arr.get(i).getVolume_id(), VOLUME_FIELD_DIMENSION);
// construct the row (panel), and then add it to the list panel
JPanel rowPanel = new JPanel();
rowPanel.setLayout(new BoxLayout(rowPanel, BoxLayout.X_AXIS));
rowPanel.add(t1);
rowPanel.add(t2);
rowPanel.add(t3);
rowPanel.add(t4);
rowPanel.add(t5);
imagelist.add(rowPanel);
} catch (Exception e) {
}
}
}
private static JTextField createTextField(String text, Dimension d) {
JTextField tf = new JTextField(text);
tf.setPreferredSize(d);
tf.setBackground(Color.WHITE);
tf.setEditable(false);
return tf;
}
private static JPanel getColumnHeadings() {
GridBagConstraints regularFieldConstraints = new GridBagConstraints();
regularFieldConstraints.fill = GridBagConstraints.BOTH;
String nameColumn = "Image Name";
String secondColumn = "Created At";
String thirdColumn = "Status";
String fourthColumn = "Image Id";
String fifthColumn = "Volume Id";
JLabel name = new JLabel(nameColumn);
JLabel created_at = new JLabel(secondColumn);
JLabel status = new JLabel(thirdColumn);
JLabel id = new JLabel(fourthColumn);
JLabel volume_id = new JLabel(fifthColumn);
name.setPreferredSize(NAME_FIELD_DIMENSION);
created_at.setPreferredSize(DATE_FIELD_DIMENSION);
status.setPreferredSize(STATUS_FIELD_DIMENSION);
id.setPreferredSize(IMAGE_FIELD_DIMENSION);
volume_id.setPreferredSize(VOLUME_FIELD_DIMENSION);
JPanel headingsPanel = new JPanel(new GridBagLayout());
headingsPanel.add(name, regularFieldConstraints);
headingsPanel.add(created_at, regularFieldConstraints);
headingsPanel.add(status, regularFieldConstraints);
headingsPanel.add(id, regularFieldConstraints);
headingsPanel.add(volume_id, regularFieldConstraints);
return headingsPanel;
}
The class sun.swing.table.DefaultTableCellHeaderRenderer is not part of the published API. If you are reluctant to depend on it, consider these alternative options:
Use the existing renderer, as shown here and below.
Use Darryl Burke's Default Table Header Cell Renderer.
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
public class Demo {
/** #see https://stackoverflow.com/a/6644956/230513 */
private static class HeaderRenderer implements TableCellRenderer {
TableCellRenderer renderer;
public HeaderRenderer(JTable table) {
renderer = table.getTableHeader().getDefaultRenderer();
}
#Override
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int col) {
// assumes JLabel
JLabel label = (JLabel) renderer.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, col);
label.setHorizontalAlignment(JLabel.LEADING);
return label;
}
}
private void initGUI() {
Object[] columnNames = new Object[]{
"Image Name", "Created At", "Status", "Image Id", "Volume Id"};
DefaultTableModel dtm = new DefaultTableModel(columnNames, 0);
dtm.addRow(new Object[]{"Some picture's name", "2013-10-05",
"Status unknown", "0123456789", "9876543210"});
JTable table = new JTable(dtm);
table.getTableHeader().setDefaultRenderer(new HeaderRenderer(table));
JScrollPane scrollPane = new JScrollPane(table);
JPanel content = new JPanel(new BorderLayout());
content.setPreferredSize(new Dimension(600, 250));
content.add(scrollPane);
JFrame frame = new JFrame("Demo");
frame.setContentPane(content);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Demo().initGUI();
}
});
}
}
I want that all the column name are indents like Image Id
If I'm understanding right, you want column header labels left-aligned just as Image ID. If so, you can use a TableCellRenderer to achive that using JTable.getHeader() method. (if not so, sorry for misunderstand your question)
For instance you can use a sun.swing.table.DefaultTableCellHeaderRenderer which extends from DefaultTableCellRenderer.
Sample Code
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import sun.swing.table.DefaultTableCellHeaderRenderer;
public class Demo{
private void initGUI(){
Object[] columnNames = new Object[]{"Image Name", "Created At", "Status", "Image Id", "Volume Id"};
DefaultTableModel dtm = new DefaultTableModel(columnNames, 0);
dtm.addRow(new Object[]{"Some picture's name", "2013-10-05", "Status unknown", "0123456789", "9876543210"});
/*
* Create your own TableCellRender
*/
TableCellRenderer headerRenderer = new DefaultTableCellHeaderRenderer(){
#Override
public Component getTableCellRendererComponent(JTable jtable, Object o, boolean bln, boolean bln1, int i, int i1) {
super.getTableCellRendererComponent(jtable, o, bln, bln1, i, i1);
setHorizontalAlignment(SwingConstants.LEADING);
return this;
}
};
JTable table = new JTable(dtm);
table.getTableHeader().setDefaultRenderer(headerRenderer);
JScrollPane scrollPane = new JScrollPane(table);
JPanel content = new JPanel(new BorderLayout());
content.setPreferredSize(new Dimension(600,250));
content.add(scrollPane);
JFrame frame = new JFrame("Demo");
frame.setContentPane(content);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(frame);
}
catch(ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex){
ex.printStackTrace();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Demo().initGUI();
}
});
}
}
Here is an example table of what I have been using:
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane);
table = new JTable();
DefaultTableModel tableModel = new DefaultTableModel(new Object[]{"Row 1","Row 2"},0);
table.setModel(tableModel);
scrollPane.setViewportView(table);
I've attempted to use the following autoscrolling methods both with the same result.
// Method 1
JScrollBar vertical = scrollPane.getVerticalScrollBar();
vertical.setValue(vertical.getMaximum());
// Method 2
table.scrollRectToVisible(table.getCellRect(table.getRowCount(), 0, true));
Both examples go to the near-bottom of the table, up until the last (bottom) row that the user has to scroll down to see, only to happen again when another row is added.
EDIT: Example code to replicate the problem
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;
public class ScrollTableEx extends JFrame {
private static final long serialVersionUID = 1L;
public int i = 100;
public JScrollPane scrollPane;
public JPanel contentPane;
public JTable table;
public JButton add;
public DefaultTableModel model = new DefaultTableModel(new Object[] { "Int (+)", "Int (-)" }, 0);
public static void main(String[] args) {
ScrollTableEx frame = new ScrollTableEx();
frame.setVisible(true);
}
public ScrollTableEx() {
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
setSize(500,400);
scrollPane = new JScrollPane();
table = new JTable();
table.setCellSelectionEnabled(true);
table.setModel(model);
scrollPane.setViewportView(table);
for (int i = 0; i < 100; i++) {
model.addRow(new Object[] { i, i * -1 });
Rectangle goodRect = table.getCellRect(model.getRowCount() - 1, 0, true);
table.scrollRectToVisible(goodRect);
}
JButton add = new JButton("Add");
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Rectangle goodRect = table.getCellRect(model.getRowCount() - 1, 0, true);
table.scrollRectToVisible(goodRect);
model.addRow(new Object[] { i, i * -1 });
i++;
}
});
add(add, BorderLayout.NORTH);
add(scrollPane, BorderLayout.CENTER);
}
}
The problem is, JTable and TableModel are both 0 indexed. That is, the last value is actually rowCount - 1.
So when you use table.getCellRect(table.getRowCount(), 0, true), it's actually returning a Rectangle of the right position, just with a 0 height, because the row doesn't actually exist.
Instead you want to use table.getCellRect(table.getRowCount() - 1, 0, true)
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
public class ScrollTable {
public static void main(String[] args) {
new ScrollTable();
}
public ScrollTable() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
final DefaultTableModel model = new DefaultTableModel(new Object[]{"Test"}, 0);
for (int index = 0; index < 100; index++) {
model.addRow(new Object[]{index});
}
final JTable table = new JTable(model);
JButton add = new JButton("Add");
add.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
model.addRow(new Object[]{model.getRowCount()});
Rectangle badRect = table.getCellRect(model.getRowCount(), 0, true);
Rectangle goodRect = table.getCellRect(model.getRowCount() - 1, 0, true);
System.out.println("bad = " + badRect);
System.out.println("goodRect = " + goodRect);
table.scrollRectToVisible(goodRect);
}
});
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(table));
frame.add(add, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
I would avoid using .setBounds where possible. It does not take into consideration the variety of differences that exist between different computers and OS's and while it might look perfectly fine when you are developing, when you move it another system, it could produce undeseriable results - IMHO