2012-04-23 71 views
6

如何完全除去JTable中的單元格周圍的單元格,以便單元格之間沒有間隙? 這是我曾嘗試:完全除去jtable網格(單元格邊框)

table.setShowGrid(false); 
table.setShowVerticalLines(false); 
table.setShowHorizontalLines(false); 

並寫入單元格渲染器,並使用:爲每個單獨的電池

setBorder(BorderFactory.createEmptyBorder(0,0,0,0)); 

但結果是細胞之間的線被刪除,但他們之間留下1像素的差距。

回答

12

並寫入單元格渲染器,並使用:

一切都取決於什麼渲染器返回,但沒有渲染工程我

  • setIntercellSpacing()

  • setShowGrid()

enter image description here

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 jTable3 = new TableBackroundPaint0(data1, headers); 
     //final JTable jTable3 = new JTable(data1, headers); 
     jTable3.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
     jTable3.setModel(model1); 
     final JScrollPane sp3 = new JScrollPane(); 
     sp3.setViewportView(jTable3); 
     jTable3.setIntercellSpacing(new Dimension(0, 0)); 
     jTable3.setShowGrid(false); 
     //jTable3.setPreferredScrollableViewportSize(jTable3.getPreferredSize()); 
     JFrame frame = new JFrame("tableSelection"); 
     frame.add(sp3); 
     frame.setSize(new Dimension(600, 200)); 
     //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); 
    } 
} 
+2

注意,你必須調用'setIntercellSpacing()'方法_after_設置柱形模型(如果你選擇這樣做),因爲它基本上調用'setColumnMargin()'的'_current_ TableColumnModel'和'JTable'本身的'setRowMargin()'。 – hendrik 2013-10-28 22:30:26

1

您可以使用JTable的這些方法,以便如果列模式並不是自動生成除去格柵(沒有數據綁定)。

setShowGrid(false); 
setRowMargin(0); 
setShowVerticalLines(false); 
setShowHorizontalLines(false); 

但是,如果你使用數據綁定創建列模型,那麼你有麻煩。即使你調用這些方法,它們仍然在每個列邊界上畫白色1個像素的間隙。

您可以使用自定義DefaultTableColumnModel刪除該列邊距。

private class CustomDefaultTableColumnModel extends DefaultTableColumnModel{ 

@Override 
public void setColumnMargin(int newMargin) { 
    //Always set ColumnMargin to zero. 
    //Because after the column data binding its internally set one as ColumnMargin. 
    //That course to paint white color grid. 
    //To stop we override the setColumnMargin and pass zero to ColumnMargin. 
    super.setColumnMargin(0); 
    } 
} 

然後,您可以將自定義表格列模型添加到表格中。

setColumnModel(new CustomDefaultTableColumnModel()); 
+0

這是避免在所選行中出現令人討厭的白色1像素網格的最佳解決方案。也許我會建議覆蓋getColumnMargin總是返回0,而不是重寫setColumnMargin方法,但它非常接近。 – Ansharja 2017-09-20 13:00:58