0

Hello I'm having a problem displaying my JTable inside a JScrollPane.

This is my code:

table = new JTable();
    table.setBounds(16, 203, 362, 16);
    //getContentPane().add(table);
    table.setShowHorizontalLines(true);
    table.setShowVerticalLines(true);
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    table.setModel(new DefaultTableModel(
        new Object[][] {
            {"2", "DN Korina", "200"},
        },
        new String[] {
            "QTY", "Item Code", "Amount"
        }
    ));
    table.getColumnModel().getColumn(0).setPreferredWidth(105);
    table.getColumnModel().getColumn(1).setPreferredWidth(244);
    table.getColumnModel().getColumn(2).setPreferredWidth(220);
    table.setBackground(Color.PINK);
    JTableHeader header = table.getTableHeader();
    header.setBackground(Color.yellow);
    JScrollPane scrollPane = new JScrollPane(table);
    scrollPane.setBounds(16, 480, 359, -240);
    getContentPane().add(scrollPane);

I'm using WindowBuilder Pro and I can't see the jScrollPane in the design view. If I removed JScrollPane and let JTable alone, JTable is visible. But I can't see the header. I've read that to view JTable's header, JTable must be placed inside JScrollPane. Any ideas why I can't see my JScrollPane and JTable? Thanks.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Dunkey
  • 1,900
  • 11
  • 42
  • 72
  • 1
    use a layout manager and your problem will disappear – Reimeus Jan 07 '14 at 03:05
  • 1
    @Reimeus I'm using AbsoluteLayout. Is there a problem with that? – Dunkey Jan 07 '14 at 03:06
  • Don't set the bounds. A layout _other_ than AbsoluteLayout – Paul Samsotha Jan 07 '14 at 03:06
  • @peeskillet in what view will I remove the bounds? JTable or JScrollPane? – Dunkey Jan 07 '14 at 03:07
  • *"I'm using AbsoluteLayout. Is there a problem with that?"* - Plenty. The Swing API was designed to be used with layout managers, so it's entire internal mechanism revolves around the ability to provide useful information to that part of the API to allow it to respond to various changes in the system. Layout managers resolve you of the wonderful issues surrounding fonts, font metrics and the variety of paint pipelines present in modern GUI based operating systems. Absolute positing is, generally, a fallacy, which when put up against simple user metrics and guidelines, becomes insignificant – MadProgrammer Jan 07 '14 at 03:45

1 Answers1

1

Finally, I got it to work!

I set the same bounds for JTable and JScrollPane.

table = new JTable();
table.setBounds(16, 203, 362, 16);


scrollPane.setBounds(16, 203, 362, 266);
Dunkey
  • 1,900
  • 11
  • 42
  • 72
  • 1
    Set the layout of the container to BorderLayout, have no more problems – MadProgrammer Jan 07 '14 at 03:13
  • @MadProgrammer for this app, I'm using AbsoluteLayout. – Dunkey Jan 07 '14 at 03:20
  • 1
    `LayoutManager`s are designed to overcome the issues presented when developing GUI applications. You have no control over the font or how those fonts are rendered on different systems, amongst the most obvious. While absolute layout managers "look" good to start with, be prepared to spend the rest of your life tweaking it... – MadProgrammer Jan 07 '14 at 03:42
  • At best, you must do a _lot_ more work; art worst, you get [this](http://stackoverflow.com/a/12532237/230513). – trashgod Jan 07 '14 at 08:43