0

I have used a JTable only for the alignement of the values I want to display. So far I my table looks like this: enter image description here

What I want is to delete the margin of the table, so it can't be noticed that it is a table. Is there a way to make this possible?

My code:

displayNames = new ArrayList<String>();
displayClasses = new ArrayList<String>();
for (int i=0; i<displayName.size(); i++) {
    displayNames.add(displayName.get(i));
    displayClasses.add(classes.get(i));
    }
    Object rowData[][] = { displayNames.toArray(), displayClasses.toArray() };
    Object columnNames[] = displayNames.toArray();
    JTable table = new JTable(rowData, columnNames);
    table.setTableHeader(null);
    table.setShowGrid(false);
    table.setBackground(Color.LIGHT_GRAY);
    tablePane = new JScrollPane(table);
    tablePane.setPreferredSize(new Dimension(765,40));
    tablePane.setBackground(Color.LIGHT_GRAY);
    rightPanel.removeAll();
    rightPanel.updateUI();
    rightPanel.add(tablePane);

public void showGUI() {
        JFrame frame = new JFrame();
        frame.add(leftPanel,BorderLayout.EAST);
        frame.add(listScrollPane,BorderLayout.WEST);
        frame.add(rightPanel);
        frame.setSize(1000,500);
        frame.setLocation(200,100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
GeR
  • 135
  • 1
  • 7
  • *"I have used a JTable only for the alignement of the values"* Use a `GridBagLayout` instead. – Andrew Thompson Jun 11 '15 at 12:39
  • possible duplicate of [remove cells border in a jtable](http://stackoverflow.com/questions/3167112/remove-cells-border-in-a-jtable) – mustangDC Jun 11 '15 at 12:40
  • I'd be put JTable to container directly, without using JSrollPane, have to override getPrefferedSize from getPreferredScrollableViewportSize() – mKorbel Jun 11 '15 at 12:56

3 Answers3

0
tablepane.setBorder(BorderFactory.createEmptyBorder());
Jahid
  • 21,542
  • 10
  • 90
  • 108
Vaibhav G
  • 627
  • 5
  • 13
0

It sounds like you want to use setBorder on the JScrollPane

tablePane.setBorder(BorderFactory.createEmptyBorder());

Answered in more detail here.

Community
  • 1
  • 1
ewhoch
  • 341
  • 2
  • 7
0

The margin (and border) are showing because you place your table in a JScrollPane.

If you use rightPanel.add(**table**), the margin is gone.

If still want the border, you can manually add a border to the table using table.setBorder(...).

By not using the JScrollPane, you obviously will also lose the ability to scroll... I'm not sure if this is a problem for you.

I also agree with Andrew: you shouldn't use a table for this, use GridBagLayout instead.

Jahid
  • 21,542
  • 10
  • 90
  • 108