Steller Steller - 29 days ago 18
Java Question

Swing - JTable JScrollPane remove bottom border or add header border

I have a dynamically created jtable with a jscrollpane. I am trying to resolve the border issue. If i specify a border I get a border around the area where theres no table data. If i create empty border in the scrollpane i get the desired border, however the border around the headings are not there. I will include two pics so you can see issue.

The left and bottom border shouldn't be there.

enter image description here

This is correct, but the border on the heading is missing.
enter image description here

I will include pertinent code.

One class

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

}


Another class

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;

}


What can i do to either add in a header border or remove the bottom, right, left sections that are not part of the jscrollpane data? Thanks for any help. It seems no matter what i do its never perfectly what i need.

Answer

What can i do to either add in a header border

You can add a MatteBorder to the table header. Just specify the border for 3 sides of the component.

You can get the table header from the table

Another option might be to override the getPreferredScrollableViewportSize() method of the JTable. Then you can control the size of the scrollpane.

Comments