Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JTable header not showing

JTable header not showing...

My JTable header wont show even if add it into a container like JScrollPane...tell me why is it happen and how can i fix it or debug it.. I search through internet and all they saying is add container to your jtable, i did but still my header are not showing.


public void table(){

try{

        rs = stat.executeQuery("SELECT * FROM payments;");

        Vector<String> header = new Vector<String>();

        header.add("PAYMENT"); 
        header.add("AMOUNT");
        header.add("MODIFIER");
        header.add("DATE MODIFIED");

        Vector<Vector<Object>> data = new  Vector<Vector<Object>>();

        while(rs.next()) {

            Vector<Object> row = new Vector<Object>();

            row.add(rs.getString("description"));
            row.add(rs.getString("amount")); 
            row.add(rs.getString("remarks")); 
            row.add(rs.getString("date"));


            data.add(row);

        } // loop

        table = new JTable(data, header);

        JScrollPane scrollPane = new JScrollPane(table);

        panel.add(table);
        panel.add(table.getTableHeader());
        //panel.removeAll();
        //scroll.add(table);
        validate();

    }catch(Exception e){
        System.out.println("Error in table: "+e);
    }//try and catch

}
like image 712
PhantomKid Avatar asked Sep 28 '13 10:09

PhantomKid


People also ask

How to display the column headers of a JTable?

The column headers are displayed by a separate component, a JTableHeader. If the JTable is put inside a JScrollPane, it adds the header automatically as column header of the JScrollPane. If you don't put it into a JScrollPane, you have to add the header by hand (it's probably the best to use a BorderLayout).

What is a JTable in JTable?

A JTable is a subclass of JComponent class for displaying complex data structures. A JTable can follow the Model View Controller (MVC) design pattern for displaying the data in rows and columns.

How to display column headers in jscrollpane?

The column headers are displayed by a separate component, a JTableHeader. If the JTable is put inside a JScrollPane, it adds the header automatically as column header of the JScrollPane.


2 Answers

Start by removing

panel.add(table.getTableHeader());

The JTable is designed to add it's header to the JScrollPane. An instance of a component can only belong to a one parent/container, the above line is removing it from the scrollpane

Also, change this...

panel.add(table);

To

panel.add(scrollPane);
like image 195
MadProgrammer Avatar answered Sep 27 '22 22:09

MadProgrammer


Best JTable Example (Working smoothly just paste in editor and execute as Java application) with dynamic data :

import java.awt.EventQueue;

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

public class TableTest {

private JFrame frame;
private JTable table;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                TableTest window = new TableTest();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public TableTest() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    table = new JTable();

    DefaultTableModel daDefaultTableModel = new DefaultTableModel(0, 0);

    String[] columnNames = new String[] {"Column Header1", "Column Header2", "Column Header3"};

    daDefaultTableModel.setColumnIdentifiers(columnNames);

    //Dummy data for Table
    daDefaultTableModel.addRow(new Object[] {"Test1","Test2","Test3"});
    daDefaultTableModel.addRow(new Object[] {"Test2","Test2","Test3"});
    daDefaultTableModel.addRow(new Object[] {"Test3","Test2","Test3"});
    daDefaultTableModel.addRow(new Object[] {"Test4","Test2","Test3"});
    daDefaultTableModel.addRow(new Object[] {"Test5","Test2","Test3"});
    daDefaultTableModel.addRow(new Object[] {"Test6","Test2","Test3"});
    daDefaultTableModel.addRow(new Object[] {"Test7","Test2","Test3"});
    daDefaultTableModel.addRow(new Object[] {"Test8","Test2","Test3"});
    daDefaultTableModel.addRow(new Object[] {"Test9","Test2","Test3"});
    daDefaultTableModel.addRow(new Object[] {"Test10","Test2","Test3"});
    daDefaultTableModel.addRow(new Object[] {"Test11","Test2","Test3"});
    daDefaultTableModel.addRow(new Object[] {"Test12","Test2","Test3"});
    daDefaultTableModel.addRow(new Object[] {"Test13","Test2","Test3"});
    daDefaultTableModel.addRow(new Object[] {"Test14","Test2","Test3"});
    daDefaultTableModel.addRow(new Object[] {"Test15","Test2","Test3"});
    daDefaultTableModel.addRow(new Object[] {"Test16","Test2","Test3"});
    daDefaultTableModel.addRow(new Object[] {"Test17","Test2","Test3"});
    daDefaultTableModel.addRow(new Object[] {"Test18","Test2","Test3"});
    daDefaultTableModel.addRow(new Object[] {"Test19","Test2","Test3"});

    table.setModel(daDefaultTableModel);

    JScrollPane scrollPane = new JScrollPane(table);
    scrollPane.setBounds(10, 38, 414, 212);
    frame.getContentPane().add(scrollPane);



  }
}
like image 38
Vikash Singh Avatar answered Sep 27 '22 20:09

Vikash Singh