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.
Here are the changes I made.
I used a border layout on the JPanel that holds the JTable.
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.
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
Exception Thrown Inside Catch Block - Will It Be Caught Again
How to Get the Session Object If I Have the Entity-Manager
Charsequence VS String in Java
What Are the Differences Between "Generic" Types in C++ and Java
How to Call a Method in Another Class of the Same Package
Does the Sequence of the Values Matter in a JSON Object
What Is Java Pojo Class, Java Bean, Normal Class
How to Make a Jbutton in a Jtable Cell Click-Able
Java Sax Parser Split Calls to Characters()
Java String - See If a String Contains Only Numbers and Not Letters
General Strategy to Resolve Java Memory Leak
What Are the Best Use Cases for Akka Framework
Cannot Change Version of Project Facet Dynamic Web Module to 3.0
Gson Custom Seralizer for One Variable (Of Many) in an Object Using Typeadapter
Java, How to Implement a Shift Cipher (Caesar Cipher)