We help IT Professionals succeed at work.

JTable - Gridline

dshrenik
dshrenik asked
on
Please let me know how I can set the grid lines' width in my jtable to 0.

If possible, please provide some sample code.

Thanks!
Comment
Watch Question

Awarded 2011
Awarded 2011

Commented:
you mean you want to remove the grid lines?
table.setShowGrid(false);



http://www.roseindia.net/java/example/java/swing/GridLine.shtml
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;

public class GridLine{
  JTable table;
  public static void main(String[] args) {
  new GridLine();
  }

  public GridLine(){
  JFrame frame = new JFrame("Grid Line in JTable Component!");
  JPanel panel = new JPanel();
  String data[][] = {{"Vinod","Computer","3"},
   {"Rahul","History","2"},
   {"Manoj","Biology","4"},
   {"Sanjay","PSD","5"}};
  String col [] = {"Name","Course","Year"};
  DefaultTableModel model = new DefaultTableModel(data,col);
  table = new JTable(model);
  //Setting the grid line 
  table.setShowGrid(true);
  //Removing the grid line from the table
//  table.setShowGrid(false);
  //Show vertical grid lines
  table.setShowVerticalLines(true);
  //setting grid color
  table.setGridColor(Color.red);
  JTableHeader header = table.getTableHeader();
  header.setBackground(Color.yellow);
  JScrollPane pane = new JScrollPane(table);
  panel.add(pane);
  frame.add(panel);
  frame.setSize(500,150);
  frame.setUndecorated(true);
  frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setVisible(true);
  }
}

Open in new window

Author

Commented:
Its not about just turning them off. I want to be able to change their width.
Awarded 2011
Awarded 2011

Commented:
But you can change tyheir width the way - wwe did it yeaterday with this MatteBorder - only it is simpler - you don't care
which is the column or row - always return the same values - eiteher all ones, or all two's or all three's ,etc

Author

Commented:
The reason I am asking is because when I use Compund Borders and turn off the grid lines, I get empty space where the gridlines are present.
Awarded 2011
Awarded 2011

Commented:
well in adidtion turn them not to show if you don't want them - as they show in the first posting in this trail.

they have some default  (proberbly of thicknes one) which appears anyway if you don't
specify any particular border and don't worry about cell renderer
But yesterday when I was playing with them I definitely had everything just plain white without any
grid. You can also set their color equal to background - then you'll not see them in that MultiCoolorBorder
which I was trying yetserday - it works for sure. But if you want to change their thickness then this
MatteBorder gives you such option.

Author

Commented:
Can you tell me how I can set their color equal to the background color?
Awarded 2011
Awarded 2011
Commented:
I'm sure there is a way
you can in the Cellrendere say
Color color = this.getbackGround();

and then say
jTable.setGridColor(color);

and you amy do it say only when your row and column are bothe zero's

 but if your cells are of different color that may be a provbblem - try this
table.setShowGrid(false);
and you'll not have them at all

Yesterday I had alwqys white background I jaust was drawing this multcolor borded in WHITE color
- so there are plenty of way with that - if yyyou really need iot theat would not be a problems

Author

Commented:
Another option is to set the intercell spacing.