I have a JTable
in Java Swing. I need to set the font size, color and style. How can I implement that in JTable
?
Asked
Active
Viewed 3.0k times
8

Andrew Thompson
- 168,117
- 40
- 217
- 433

Jishnu
- 125
- 1
- 2
- 8
-
3JTable already has "default" style. I guess you want to set new style. – KV Prajapati Aug 27 '11 at 02:23
-
2For individual cells or the whole table? – Ryan Amos Aug 27 '11 at 02:56
2 Answers
11
why set TableRenderer for basic setting in the JTable, more --> JTable#setWhatever
EDIT (ballast removed) code:
import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class TableExample {
public TableExample() {
Object[][] data1 = new Object[50][5];
for (int i = 0; i < data1.length; i++) {
data1[i][0] = "Company # " + (i + 1);
for (int j = 1; j < data1[i].length; j++) {
data1[i][j] = "" + (i + 1) + ", " + j;
}
}
String[] headers = {"Col 1", "Col 2", "Col 3", "Col 4", "Col 5"};
DefaultTableModel model1 = new DefaultTableModel(data1, headers);
final JTable jTable1 = new JTable(model1);
jTable1.setBackground(Color.orange);
jTable1.setForeground(Color.blue);
jTable1.setRowHeight(24);
jTable1.setFont(new Font("Arial", Font.BOLD, 12));
jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
final JScrollPane sp1 = new JScrollPane();
sp1.setPreferredSize(new Dimension(600, 200));
sp1.setViewportView(jTable1);
final JTable jTable2 = new JTable(model1);
jTable2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
final JScrollPane sp2 = new JScrollPane();
sp2.setPreferredSize(new Dimension(600, 200));
sp2.setViewportView(jTable2);
final JTable jTable3 = new TableBackroundPaint0(data1, headers);
jTable3.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jTable3.setModel(model1);
final JScrollPane sp3 = new JScrollPane();
sp3.setPreferredSize(new Dimension(600, 200));
sp3.setViewportView(jTable3);
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(3, 0, 10, 10));
panel1.add(sp1);
panel1.add(sp2);
panel1.add(sp3);
JFrame frame = new JFrame("tableSelection");
frame.add(panel1);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TableExample te = new TableExample();
}
});
}
}
class TableBackroundPaint0 extends JTable {
private static final long serialVersionUID = 1L;
TableBackroundPaint0(Object[][] data, Object[] head) {
super(data, head);
setOpaque(false);
((JComponent) getDefaultRenderer(Object.class)).setOpaque(false);
}
@Override
public void paintComponent(Graphics g) {
Color background = new Color(168, 210, 241);
Color controlColor = new Color(230, 240, 230);
int width = getWidth();
int height = getHeight();
Graphics2D g2 = (Graphics2D) g;
Paint oldPaint = g2.getPaint();
g2.setPaint(new GradientPaint(0, 0, background, width, 0, controlColor));
g2.fillRect(0, 0, width, height);
g2.setPaint(oldPaint);
for (int row : getSelectedRows()) {
Rectangle start = getCellRect(row, 0, true);
Rectangle end = getCellRect(row, getColumnCount() - 1, true);
g2.setPaint(new GradientPaint(start.x, 0, controlColor, (int) ((end.x + end.width - start.x) * 1.25), 0, Color.orange));
g2.fillRect(start.x, start.y, end.x + end.width - start.x, start.height);
}
super.paintComponent(g);
}
}

mKorbel
- 109,525
- 20
- 134
- 319
-
-1 for random code snippets recycled ;-) Or what have the parts about custom selection and background image to do with the question? As @camickr recently reminded you: code in answers should be as focused to the problem as code in questions (or sscce, to please Andrew :) – kleopatra Aug 27 '11 at 09:48
-
@kleopatra accepted, but then is contraproductive asking Andrew for SSCCE, his answer in all respects meet the standards for quality answers that there were clear your comment – mKorbel Aug 27 '11 at 10:25
-
1ahh .. that's a misunderstanding, I had no intention of asking @Andrew Thompson for example code, it's a joke between the two of us, me never remembering the exact letters of the acronym :-) If you accept your examples being overly verbose and not to the point: please feel free to edit your answer :-) – kleopatra Aug 27 '11 at 10:46
-
@kleopatra please see my edit, then now all de/constructive comments are welcome, why not ... – mKorbel Aug 27 '11 at 12:01
-
@kleopatra for example last his comment here http://stackoverflow.com/questions/7178347/how-to-import-an-image-in-menu-bar – mKorbel Aug 27 '11 at 12:10
-
1@Andrew Thompson thanks, but some your creatures are unforgettable, especially if you starts an answer with Dear Playboy ... (OTN) – mKorbel Aug 27 '11 at 18:08
-
6
Set a custom TableCellRenderer. See How to Use Tables - Concepts: Editors and Renderers in the Java Tutorial for more details.

Andrew Thompson
- 168,117
- 40
- 217
- 433