Removing jtable grid (cell border) completely

How can I completely remove the border around cells in a swinging JTable so that the cells do not have gaps between them? Here is what I tried:

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

and cell renderer entries and using:

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

for each individual cell.

But the result is to delete the rows between the cells, but 1 pixel gap remains between them.

+6
source share
3 answers

and cell renderer entries and using:

it all depends on what Renderer returns, but without Renderer it works for me

  • 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); } } 
+12
source

You can simply set the border color for the background color of the cell. Also, please review these examples,

http://www.java2s.com/Code/Java/Swing-Components/CellBorderTableExample.htm

http://www.esus.com/docs/GetQuestionPage.jsp?uid=1290

+2
source

You can use these methods in JTable to remove the grid if your column models are not generated automatically (without data binding).

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

But if you created column models using data binding, then you will have problems. This is even if you call these methods, they still color the white space at 1 pixel in each column field.

You can remove this column marker using the user defaultTableColumnModel method.

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

Then you can add your column table to the table.

  setColumnModel(new CustomDefaultTableColumnModel()); 
+1
source

Source: https://habr.com/ru/post/913897/


All Articles