1

I am trying to fetch data from SQlite DB and show it in JTable - but header not appear.

This my code:

JPanel panel =new JPanel();
panel.setLayout(null);
panel.setSize(5,5);

JTable tabel_1=new JTable();
tabel_1.setSelectionBackground(new java.awt.Color(0, 153, 51));

JTableHeader header=tabel_1.getTableHeader();
header.setBackground(Color.BLACK);

DefaultTableModel tableModel =(DefaultTableModel) DbUtils.resultSetToTableModel(rs);

tableModel.setColumnIdentifiers(headers);

tabel_1.setModel(tableModel);
frame.add(panel);

frame.add(tabel_1);

and this is result:

enter image description here

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    Add the table in a JScrollPane – StanislavL Aug 09 '17 at 14:28
  • `panel.setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Aug 09 '17 at 15:14

1 Answers1

1

Try to add JScrollPane in this part of code to your code like this:

  JTable tabel_1 =new JTable();
  tabel_1.setSelectionBackground(new java.awt.Color(0, 153, 51));
  JScrollPane tableScroll = new JScrollPane(tabel_1);

  frame.add(tableScroll);
Abdelhak
  • 8,299
  • 4
  • 22
  • 36