2

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.

roeygol
  • 4,908
  • 9
  • 51
  • 88
Steller
  • 308
  • 2
  • 8
  • 26

2 Answers2

5

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I would like to just add the border to the header, but im unsure how to do that. – Steller Aug 01 '16 at 15:11
  • @Steller, I just told you how to do that. There are two steps 1) get the table header from the table. 2) add the MatteBorder to the header. Which step don't you understand? – camickr Aug 01 '16 at 15:12
1
JTableHeader header = yourTable.getTableHeader();
header.setBorder(new LineBorder(Color.decode("#006699"),2));
TiyebM
  • 2,684
  • 3
  • 40
  • 66