mugiwaradz
mugiwaradz

Reputation: 393

JTable not shown columns titles

I have a little problem in this code. I want to display a JTable with Column Headers but the problem is that the column's titles are not being displayed, as the Screenshot shows below:

private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
private static final String DB_CONNECTION = "jdbc:oracle:thin:@localhost:1521:DB";
private static final String DB_USER = "*******";
private static final String DB_PASSWORD = "********";
Connection conn = null;
Statement stmt = null;
static Vector<Vector<String>> dataJ, dataC, data;
Vector<String> columnJ,

public void getCleubJoueurData() {
    data = new Vector<Vector<String>>();
    colum = new Vector<String>();
    colum.add("nom");
    colum.add("nom_cleub");
    colum.add("numero_maillot");
    colum.add("nationalite");

    String query = "SELECT joueur.NOM,CLEUB.NOM_CLEUB," +
                    "JOUEUR_CLEUB.NUMERO_MAILLOT, joueur.NATIONALITE " +
                    "FROM joueur JOIN JOUEUR_CLEUB ON joueur.ID_J=" + 
                    "JOUEUR_CLEUB.ID_J JOIN CLEUB ON JOUEUR_CLEUB.ID_C=CLEUB.ID_C";

    try {
        try {
            Class.forName(DB_DRIVER);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        conn = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
        stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        while (rs.next()) {
            Vector<String> vstring = new Vector<String>();
            vstring.add(rs.getString("nom"));
            vstring.add(rs.getString("nom_cleub"));
            vstring.add(rs.getString("numero_maillot"));
            vstring.add(rs.getString("nationalite"));

            data.add(vstring);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException ex) {
            }
        }
    }
}

public App() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 882, 477);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    tabbedPane.setBackground(Color.WHITE);
    tabbedPane.setBounds(40, 43, 812, 390);
    contentPane.add(tabbedPane);
    JPanel panel = new JPanel();
    tabbedPane.addTab("joueurs", null, panel, null);
    panel.setLayout(null);

    getJoueurData();
    DefaultTableModel mode = new DefaultTableModel(data, column);

    table = new JTable(model);
    table.setToolTipText("");
    table.setBounds(0, 94, 819, 269);
    panel.add(table);

}

Upvotes: 1

Views: 89

Answers (2)

andyroberts
andyroberts

Reputation: 3518

The JTable documentation states that you'd typically want to use JTable in conjunction with a JScrollPane. If not:

JTables are typically placed inside of a JScrollPane. [...] 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.

I'd recommend wrapping in a JScrollPane myself:

...
JScrollPane scrollPane = new JScrollPane(table);
panel.add(scrollPane);
...

Upvotes: 1

Paul Samsotha
Paul Samsotha

Reputation: 209122

  1. Don't use null layouts. (And don't set the bounds)

  2. Wrap your table in a JScrollPane. The JScrollPane will show the headers. If you don't want to use a JScrollPane, then you can get the JTableHeader of the table and add it separately

Former: (with JScrollPane)

JPanel container = new JPanel();  // default FlowLayout
JScrollPane scrollPane = new JScrollPane(table);
//container.add(table);  // don't add the table, just the JScrollPane
container.add(scrollPane);

Latter: (without JScrollPane)

JPanel container = new JPanel(new BorderLayout());
container.add(table, BorderLayout.CENTER);
container.add(table.getTableHeader(), BorderLayout.PAGE_START);

Upvotes: 1

Related Questions