Jtable Won't Show Column Headers

JTable won't show column headers

Put your JTable inside a JScrollPane. Try this:

add(new JScrollPane(scrTbl));

Header of JTable's column won't show

Here's a simple JTable GUI I created using your method.

JTable Example # 38593729

Here are the changes I made.

  1. I used a border layout on the JPanel that holds the JTable.

  2. I got rid of all null layouts and positioning statements. I did ask for a preferred size for the JPanel. After you actually add some data to the JTable, you can define the size of the JTable and remove the preferred size hint.

  3. I defined the JTable first, then the JScrollPane. Thanks to Andrew Thompson for his comment.

Here's the minimal, runnable example #38,593,729 of a JTable in a JPanel in a JFrame. I hope this example helps you, unlike the first 38,593,728 examples on the Internet.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

public class JTableSimpleExample implements Runnable {

private JFrame frame;

@Override
public void run() {
frame = new JFrame("JTable Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.add(panelTabel());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

private JPanel panelTabel() {
JPanel panelTabel = new JPanel();
panelTabel.setLayout(new BorderLayout());
panelTabel.setPreferredSize(new Dimension(400, 100));

Vector<String> headerTabel = new Vector<>(2);
headerTabel.addElement(new String("No."));
headerTabel.addElement(new String("Kode Barang"));
DefaultTableModel modelTabel = new DefaultTableModel(1, headerTabel.size());
modelTabel.setColumnIdentifiers(headerTabel);

JTable tabelBarang = new JTable(modelTabel);

JScrollPane scrollTabel = new JScrollPane(tabelBarang);

panelTabel.add(scrollTabel, BorderLayout.CENTER);
return panelTabel;
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new JTableSimpleExample());
}

}

JTable column header not visible

The API for JTable states:

"Note that if you wish to use a JTable in a standalone view (outside
of a JScrollPane) and want the header displayed, you can get it using
getTableHeader() and display it separately."

Or just add the table to a scrollpane, and add your scrollpane to the panel...

JTable: Can't see Column Names

You need to add scrollPane to panel instead of adding table to panel.

JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
panel.add(scrollPane);

JTable doesn't show columns names

You used the JScrollPane in a wrong way. To make it work fine, just do the following.

Pass the JTable instance to the JScrollPane in the constructor:

private JScrollPane scrollPane = new JScrollPane(contentTable);

Comment out the line where you used to add the JTable to the JScrollPane:

// scrollPane.add(contentTable);

When you put the component inside the constructor of a JScrollPane, you mention which is the view to which the scroll to be applied for.

On the other side, using the add method, you just add a component to a container, like adding it to a JPanel. This way, you don't specify the component to add the scroll bars to.



Related Topics



Leave a reply



Submit