Jtable,使用自定义渲染器和系统 L&F 显示排序图标 TableHeader 的问题
Jtable, issues with displaying sorting icon TableHeader with custom renderer and system L&F
我为我的 JTabe 创建了一个自定义渲染器 header,但是在对 table 进行排序后,当我使用默认系统 L&F 时,排序图标没有出现(在我的例子中 windows 8) 但是当我使用 Java L&F.
时出现图标
使用 Java L&F:
使用系统 L&F:
这是我的 class 渲染器:
private class HeaderRenderer implements TableCellRenderer{
private final LineBorder lb = new LineBorder(new Color(0, 152, 206));
private final Dimension dim = new Dimension(150, 25);
private TableCellRenderer delegate;
public HeaderRenderer(TableCellRenderer delegate) {
this.delegate = delegate;
}
@Override
public Component getTableCellRendererComponent(JTable table,Object value, boolean isSelected, boolean hasFocused, int row, int column) {
Component comp = delegate.getTableCellRendererComponent(table, value, isSelected, hasFocused, row, column);
if (comp instanceof JLabel) {
JLabel label = (JLabel) comp;
label.setBackground(Color.WHITE);
label.setPreferredSize(dim);
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setBorder(lb);
}
return comp;
}
}
使用:
JTableHeader tableHeaderCompnoent = table.getTableHeader();
tableHeaderCompnoent.setDefaultRenderer(new HeaderRenderer(tableHeaderCompnoent.getDefaultRenderer()));
编辑:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
public class TableRenderingAndSorting extends JFrame {
private JPanel contentPane;
private JTable table_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TableRenderingAndSorting frame = new TableRenderingAndSorting();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TableRenderingAndSorting() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 497, 347);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
table_1 = new JTable();
table_1.setModel(new DefaultTableModel(
new Object[][] {
{"1", "Jack", "Developper"},
{"2", "Richard", "Developper"},
{"3", "Jassmine", "Developper"},
{"4", "Tom", "Project leader"},
{"5", "Anna", null},
},
new String[] {
"Col1", "Col2", "Col3"
}
));
table_1.setAutoCreateRowSorter(true);
contentPane.add(new JScrollPane(table_1), BorderLayout.CENTER);
JTableHeader tableHeaderCompnoent = table_1.getTableHeader();
tableHeaderCompnoent.setDefaultRenderer(new HeaderRenderer(tableHeaderCompnoent.getDefaultRenderer()));
table_1.getTableHeader().setDefaultRenderer(new HeaderRenderer(tableHeaderCompnoent.getDefaultRenderer()));
}
private class HeaderRenderer implements TableCellRenderer{
private final LineBorder lb = new LineBorder(new Color(0, 152, 206));
private final Dimension dim = new Dimension(150, 25);
private TableCellRenderer delegate;
public HeaderRenderer(TableCellRenderer delegate) {
this.delegate = delegate;
}
@Override
public Component getTableCellRendererComponent(JTable table,Object value, boolean isSelected, boolean hasFocused, int row, int column) {
Component comp = delegate.getTableCellRendererComponent(table, value, isSelected, hasFocused, row, column);
if (comp instanceof JLabel) {
JLabel label = (JLabel) comp;
label.setBackground(Color.WHITE);
label.setPreferredSize(dim);
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setBorder(lb);
}
return comp;
}
}
}
[...] the sorting icon does not appear when I'm using the default system L&F (in my case windows 8) but the icon appears when I use the Java L&F.
默认L&F(Metal)和系统L&F的区别在于,第一个在header单元格的中心(垂直对齐)绘制排序图标,而后者在顶部绘制。 header 单元格。
接下来要为 header 单元格绘制边框。边框从组件的边缘向内绘制,这意味着它绘制在组件的区域上。由于排序图标在系统L&F的边缘,所以被border覆盖了
您可以通过删除边框或将标签添加到将处理边框的 JPanel
来解决此问题:
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocused, int row, int column) {
Component comp = delegate.getTableCellRendererComponent(table, value, isSelected, hasFocused, row, column);
JPanel panel = new JPanel(new BorderLayout());
if (comp instanceof JLabel) {
JLabel label = (JLabel) comp;
label.setBackground(Color.WHITE);
label.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(label);
panel.setBorder(lb);
}
return panel;
}
如@mKorbel 所述,我删除了 setPreferredSize
,dim
字段也应随之删除。
此外,在行中
JTableHeader tableHeaderCompnoent = table_1.getTableHeader();
tableHeaderCompnoent.setDefaultRenderer(new HeaderRenderer(tableHeaderCompnoent.getDefaultRenderer()));
table_1.getTableHeader().setDefaultRenderer(new HeaderRenderer(tableHeaderCompnoent.getDefaultRenderer()));
第二个和第三个是一样的,你可以去掉第三个
我为我的 JTabe 创建了一个自定义渲染器 header,但是在对 table 进行排序后,当我使用默认系统 L&F 时,排序图标没有出现(在我的例子中 windows 8) 但是当我使用 Java L&F.
时出现图标
使用 Java L&F:
使用系统 L&F:
这是我的 class 渲染器:
private class HeaderRenderer implements TableCellRenderer{
private final LineBorder lb = new LineBorder(new Color(0, 152, 206));
private final Dimension dim = new Dimension(150, 25);
private TableCellRenderer delegate;
public HeaderRenderer(TableCellRenderer delegate) {
this.delegate = delegate;
}
@Override
public Component getTableCellRendererComponent(JTable table,Object value, boolean isSelected, boolean hasFocused, int row, int column) {
Component comp = delegate.getTableCellRendererComponent(table, value, isSelected, hasFocused, row, column);
if (comp instanceof JLabel) {
JLabel label = (JLabel) comp;
label.setBackground(Color.WHITE);
label.setPreferredSize(dim);
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setBorder(lb);
}
return comp;
}
}
使用:
JTableHeader tableHeaderCompnoent = table.getTableHeader();
tableHeaderCompnoent.setDefaultRenderer(new HeaderRenderer(tableHeaderCompnoent.getDefaultRenderer()));
编辑:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
public class TableRenderingAndSorting extends JFrame {
private JPanel contentPane;
private JTable table_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TableRenderingAndSorting frame = new TableRenderingAndSorting();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TableRenderingAndSorting() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 497, 347);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
table_1 = new JTable();
table_1.setModel(new DefaultTableModel(
new Object[][] {
{"1", "Jack", "Developper"},
{"2", "Richard", "Developper"},
{"3", "Jassmine", "Developper"},
{"4", "Tom", "Project leader"},
{"5", "Anna", null},
},
new String[] {
"Col1", "Col2", "Col3"
}
));
table_1.setAutoCreateRowSorter(true);
contentPane.add(new JScrollPane(table_1), BorderLayout.CENTER);
JTableHeader tableHeaderCompnoent = table_1.getTableHeader();
tableHeaderCompnoent.setDefaultRenderer(new HeaderRenderer(tableHeaderCompnoent.getDefaultRenderer()));
table_1.getTableHeader().setDefaultRenderer(new HeaderRenderer(tableHeaderCompnoent.getDefaultRenderer()));
}
private class HeaderRenderer implements TableCellRenderer{
private final LineBorder lb = new LineBorder(new Color(0, 152, 206));
private final Dimension dim = new Dimension(150, 25);
private TableCellRenderer delegate;
public HeaderRenderer(TableCellRenderer delegate) {
this.delegate = delegate;
}
@Override
public Component getTableCellRendererComponent(JTable table,Object value, boolean isSelected, boolean hasFocused, int row, int column) {
Component comp = delegate.getTableCellRendererComponent(table, value, isSelected, hasFocused, row, column);
if (comp instanceof JLabel) {
JLabel label = (JLabel) comp;
label.setBackground(Color.WHITE);
label.setPreferredSize(dim);
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setBorder(lb);
}
return comp;
}
}
}
[...] the sorting icon does not appear when I'm using the default system L&F (in my case windows 8) but the icon appears when I use the Java L&F.
默认L&F(Metal)和系统L&F的区别在于,第一个在header单元格的中心(垂直对齐)绘制排序图标,而后者在顶部绘制。 header 单元格。
接下来要为 header 单元格绘制边框。边框从组件的边缘向内绘制,这意味着它绘制在组件的区域上。由于排序图标在系统L&F的边缘,所以被border覆盖了
您可以通过删除边框或将标签添加到将处理边框的 JPanel
来解决此问题:
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocused, int row, int column) {
Component comp = delegate.getTableCellRendererComponent(table, value, isSelected, hasFocused, row, column);
JPanel panel = new JPanel(new BorderLayout());
if (comp instanceof JLabel) {
JLabel label = (JLabel) comp;
label.setBackground(Color.WHITE);
label.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(label);
panel.setBorder(lb);
}
return panel;
}
如@mKorbel 所述,我删除了 setPreferredSize
,dim
字段也应随之删除。
此外,在行中
JTableHeader tableHeaderCompnoent = table_1.getTableHeader();
tableHeaderCompnoent.setDefaultRenderer(new HeaderRenderer(tableHeaderCompnoent.getDefaultRenderer()));
table_1.getTableHeader().setDefaultRenderer(new HeaderRenderer(tableHeaderCompnoent.getDefaultRenderer()));
第二个和第三个是一样的,你可以去掉第三个