Swing - JTable JScrollPane 删除底部边框或添加 header 边框
Swing - JTable JScrollPane remove bottom border or add header border
我有一个动态创建的带有 jscrollpane 的 jtable。我正在努力解决边界问题。如果我指定一个边界,我会在没有 table 数据的区域周围得到一个边界。如果我在滚动窗格中创建空边框,我会得到所需的边框,但是标题周围的边框不存在。我将附上两张照片,以便您查看问题。
左边和底部的边框不应该在那里。
这是正确的,但标题上的边框丢失了。
我会包含相关代码。
一个class
private void createPanels(JButton close, JButton mapButton){
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
add(mainPanel);
setModal(true);
JPanel topPanel = new JPanel(new BorderLayout(0, 0));
topPanel.setPreferredSize(new Dimension(400, 30));
JLabel title = new JLabel("Mapping Flags");
title.setFont(new Font("SansSerif", Font.PLAIN, 17));
JPanel panelForTitle = new JPanel(new FlowLayout(FlowLayout.LEFT, 270, 30));
panelForTitle.add(title);
mainPanel.add(panelForTitle);
mainPanel.add(topPanel);
JPanel tablePanel = new JPanel(new BorderLayout());
tablePanel.setBorder(BorderFactory.createEmptyBorder(15, 25, 15, 25));
tablePanel.add(spTable);
mainPanel.add(tablePanel);
JPanel bottom = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 30));
close.setPreferredSize(new Dimension(90,22));
mapButton.setPreferredSize(new Dimension(90,22));
bottom.add(close);
bottom.add(mapButton);
mainPanel.add(bottom);
bottom.setMaximumSize(new Dimension(600, 0));
setTitle("Mapping Flags");
setSize(new Dimension(650, 380));
setResizable(false);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
另一个class
public void setMappingsTable(){
dtm = new DefaultTableModel(new String[]{"Style ID", "Style", "Exact Match", "Carry Over", "OEM Temp"}, 0);
mappingsTable.setModel(dtm);
mappingsTable.setRowHeight(20);
mappingsTable.getColumnModel().getColumn(0).setPreferredWidth(75);
mappingsTable.getColumnModel().getColumn(1).setPreferredWidth(410);
mappingsTable.getColumnModel().getColumn(2).setPreferredWidth(130);
mappingsTable.getColumnModel().getColumn(3).setPreferredWidth(130);
mappingsTable.getColumnModel().getColumn(4).setPreferredWidth(130);
mappingsTable.setFont(new Font("SansSerif", Font.PLAIN, 12));
mappingsTable.setBackground(Color.WHITE);
Color color = mappingsTable.getGridColor();
// mappingsTable.setBorder(new MatteBorder(0, 0, 0, 0, color));
mappingsTable.setBorder(BorderFactory.createLineBorder(Color.RED,1));
addDataToTable();
}
public JScrollPane setScrollPane(JTable mappingsTable){
spTable = new JScrollPane(mappingsTable);
spTable.setBounds(45, 230, 700, 300);
// spTable.setBorder(BorderFactory.createEmptyBorder());
return spTable;
}
如何添加 header 边框或删除不属于 jscrollpane 数据的底部、右侧、左侧部分?谢谢你的帮助。似乎无论我做什么都不能完美地满足我的需要。
What can i do to either add in a header border
您可以将 MatteBorder 添加到 table header。只需指定组件的 3 个边的边框。
您可以从 table
中获取 table header
另一种选择可能是覆盖 JTable 的 getPreferredScrollableViewportSize()
方法。然后就可以控制scrollpane的大小了。
JTableHeader header = yourTable.getTableHeader();
header.setBorder(new LineBorder(Color.decode("#006699"),2));
我有一个动态创建的带有 jscrollpane 的 jtable。我正在努力解决边界问题。如果我指定一个边界,我会在没有 table 数据的区域周围得到一个边界。如果我在滚动窗格中创建空边框,我会得到所需的边框,但是标题周围的边框不存在。我将附上两张照片,以便您查看问题。
左边和底部的边框不应该在那里。
这是正确的,但标题上的边框丢失了。
我会包含相关代码。
一个class
private void createPanels(JButton close, JButton mapButton){
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
add(mainPanel);
setModal(true);
JPanel topPanel = new JPanel(new BorderLayout(0, 0));
topPanel.setPreferredSize(new Dimension(400, 30));
JLabel title = new JLabel("Mapping Flags");
title.setFont(new Font("SansSerif", Font.PLAIN, 17));
JPanel panelForTitle = new JPanel(new FlowLayout(FlowLayout.LEFT, 270, 30));
panelForTitle.add(title);
mainPanel.add(panelForTitle);
mainPanel.add(topPanel);
JPanel tablePanel = new JPanel(new BorderLayout());
tablePanel.setBorder(BorderFactory.createEmptyBorder(15, 25, 15, 25));
tablePanel.add(spTable);
mainPanel.add(tablePanel);
JPanel bottom = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 30));
close.setPreferredSize(new Dimension(90,22));
mapButton.setPreferredSize(new Dimension(90,22));
bottom.add(close);
bottom.add(mapButton);
mainPanel.add(bottom);
bottom.setMaximumSize(new Dimension(600, 0));
setTitle("Mapping Flags");
setSize(new Dimension(650, 380));
setResizable(false);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
另一个class
public void setMappingsTable(){
dtm = new DefaultTableModel(new String[]{"Style ID", "Style", "Exact Match", "Carry Over", "OEM Temp"}, 0);
mappingsTable.setModel(dtm);
mappingsTable.setRowHeight(20);
mappingsTable.getColumnModel().getColumn(0).setPreferredWidth(75);
mappingsTable.getColumnModel().getColumn(1).setPreferredWidth(410);
mappingsTable.getColumnModel().getColumn(2).setPreferredWidth(130);
mappingsTable.getColumnModel().getColumn(3).setPreferredWidth(130);
mappingsTable.getColumnModel().getColumn(4).setPreferredWidth(130);
mappingsTable.setFont(new Font("SansSerif", Font.PLAIN, 12));
mappingsTable.setBackground(Color.WHITE);
Color color = mappingsTable.getGridColor();
// mappingsTable.setBorder(new MatteBorder(0, 0, 0, 0, color));
mappingsTable.setBorder(BorderFactory.createLineBorder(Color.RED,1));
addDataToTable();
}
public JScrollPane setScrollPane(JTable mappingsTable){
spTable = new JScrollPane(mappingsTable);
spTable.setBounds(45, 230, 700, 300);
// spTable.setBorder(BorderFactory.createEmptyBorder());
return spTable;
}
如何添加 header 边框或删除不属于 jscrollpane 数据的底部、右侧、左侧部分?谢谢你的帮助。似乎无论我做什么都不能完美地满足我的需要。
What can i do to either add in a header border
您可以将 MatteBorder 添加到 table header。只需指定组件的 3 个边的边框。
您可以从 table
中获取 table header另一种选择可能是覆盖 JTable 的 getPreferredScrollableViewportSize()
方法。然后就可以控制scrollpane的大小了。
JTableHeader header = yourTable.getTableHeader();
header.setBorder(new LineBorder(Color.decode("#006699"),2));