MrSSS16
MrSSS16

Reputation: 473

JTable header not showing still

I have looked and found that many people weren't putting their table into a scrollPane. Even though I nest my table into a scrollPane then into the frame it still fails to show the header. Is there something else I'm missing ? Thanks

public class Gui extends JFrame {

AbstractTableModel model;
JTable table; 



public void start(AbstractTableModel model) { 
    this.model = model;
    table=new JTable(model){
        @Override 
        public boolean isCellEditable(int arg0, int arg1) { 
            return false; 
        }

    }; 


    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    TableColumn column = null;
        for (int i = 0; i < model.getColumnCount(); i++) {
            column = table.getColumnModel().getColumn(i);
            column.setPreferredWidth(120);
            column.setMaxWidth(300);
            column.setMinWidth(50);
        }    

        JScrollPane pane = new JScrollPane(table);  
        pane.setPreferredSize(new Dimension(900,900));

        add(pane); 
        setLayout(new FlowLayout()); 
        setVisible(true); 
        setDefaultCloseOperation(EXIT_ON_CLOSE); 
        pack();
}

Upvotes: 2

Views: 1383

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347334

Based on your code and then having to add the missing functionality, you code works...

enter image description here

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class Gui extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                Gui frame = new Gui();
                frame.start(new DefaultTableModel(new Object[]{"A", "B", "C"}, 10));
            }
        });
    }

    AbstractTableModel model;
    JTable table;

    public void start(AbstractTableModel model) {
        this.model = model;
        table = new JTable(model) {
            @Override
            public boolean isCellEditable(int arg0, int arg1) {
                return false;
            }

        };

        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        TableColumn column = null;
        for (int i = 0; i < model.getColumnCount(); i++) {
            column = table.getColumnModel().getColumn(i);
            column.setPreferredWidth(120);
            column.setMaxWidth(300);
            column.setMinWidth(50);
        }

        JScrollPane pane = new JScrollPane(table);
        pane.setPreferredSize(new Dimension(900, 900));

        add(pane);
        setLayout(new FlowLayout());
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();

    }
}

This then leads to two broad assumptions...

  1. You're TableModel doesn't have any columns
  2. You're TableModel doesn't have any column names...

For example, a TableModel with no column names...

No column names

Personally...

  • I wouldn't use FlowLayout for this, BorderLayout will give better results
  • pack the frame before you make it visible
  • Set the layout before you add components as sometimes, things can get messed up...

For example...

setLayout(new FlowLayout()); // But I'd use a `BorderLayout`
add(pane);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);

Upvotes: 3

Related Questions