2016-08-01 185 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));