2016-08-01 95 views
2

我有一个jscrollpane动态创建jtable。我正在尝试解决边界问题。如果我指定一个边框,我会在没有表格数据的区域附近找到边框。如果我在滚动窗格中创建空边框,我会得到所需的边框,但标题周围的边框不存在。我将包括两个图片,以便您可以看到问题。Swing - JTable JScrollPane删除下边框或添加标题边框

左边框和底边框不应该在那里。

enter image description here

这是正确的,但在标题的边界丢失。 enter image description here

我将包含相关的代码。

一类

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); 

} 

另一类

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; 

} 

我能做些什么以添加在头边框或删除底部,左,右不在JScrollPane的数据的部分章节?谢谢你的帮助。看起来不管我做什么都不完美,我需要什么。

回答

4

我可以做些什么来无论是在头部添加边框

您可以将MatteBorder添加表头。只需指定组件3边的边框。

您可以从表

另一种选择可能是覆盖JTable的getPreferredScrollableViewportSize()方法得到的表头。然后你可以控制滚动窗格的大小。

+0

我想只是将边框添加到标题,但我不确定如何做到这一点。 – Steller

+0

@Steller,我刚刚告诉过你该怎么做。有两个步骤1)从表格中获取表格标题。 2)将MatteBorder添加到标题。你不明白哪一步? – camickr

+0

我明白了,谢谢! – Steller

0
JTableHeader header = yourTable.getTableHeader(); 
header.setBorder(new LineBorder(Color.decode("#006699"),2));