Java – JTable does not display column headers
•
Java
This is my first time using GUI, so I'm not sure what caused the problem My uni mission The project aims to develop a "product management" plan for various purposes Except for GUI, the whole process has been completed. That's why I don't know why this JTable won't display column headers This is the code (by the way, using miglayout)
package sepm.s2012.e0727422.gui;
import sepm.s2012.e0727422.service.*;
import java.awt.*;
import javax.swing.*;
import net.miginfocom.swing.MigLayout;
public class MainFrame {
/** Start all services **/
// IProductService ps = new ProductService();
// IInvoiceService is = new InvoiceService();
// IOrderService os = new OrderService();
/** Frame **/
private JFrame frame;
private JTabbedPane tab;
public static void main(String[] args) {
new MainFrame();
}
public MainFrame() {
frame = new JFrame("Product control and management system");
frame.setVisible(true);
frame.setSize(800,600);
frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
/** TABBED PANE options and parameters **/
tab = new JTabbedPane();
ImageIcon icon = null; // TODO
tab.addTab("Products",icon,tabProducts(),"Add/Update/Delete products");
tab.addTab("Invoices",tabInvoices(),"Overview of invoices");
tab.addTab("Cart",tabCart(),"Order new products");
tab.setSelectedIndex(0);
frame.add(tab,BorderLayout.CENTER);
}
/**
* Products Panel tab
* @return panel
*/
public JPanel tabProducts() {
JPanel panel = new JPanel(new MigLayout("","20 [] 20","10 [] 10 [] 10 [] 10"));
JLabel label = new JLabel("List of all available products");
JButton add = new JButton("Add product");
JButton update = new JButton("Update product");
// Below is a test table,will be replace by products in DB
String[] tableTitle = new String[] {"ID","Name","Type","Price","In stock"};
String[][] tableData = new String[][] {{"1","Item 1","Type 1","0.00","0"},{"2","Item 2","Type 2",{"3","Item 3","Type 3",{"4","Item 4","Type 4","0"}};
JTable table = new JTable(tableData,tableTitle);
panel.add(label,"wrap,span");
panel.add(table,span");
panel.add(add);
panel.add(update);
return panel;
}
public JPanel tabInvoices() {
JPanel panel = new JPanel(new MigLayout());
return panel;
}
public JPanel tabCart() {
JPanel panel = new JPanel(new MigLayout());
return panel;
}
}
Solution
First, add the table to the JScrollPane:
JScrollPane scrollpane = new JScrollPane(table);
Then add the scroll pane to your layout:
panel.add(scrollpane,span");
Secondly, add all components to the framework and display them as follows at the end of the method:
//... frame.add(tab,BorderLayout.CENTER); frame.validate(); frame.setVisible(true);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
