tubbecool
tubbecool

Reputation: 1

JScrollPane for JTable

public class ERPFrame extends JFrame {
    private JPanel contentPane;
    private JTable table;
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;
    private JTextField textField_3;
    Controller controller;
    DefaultTableModel dfm;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ERPFrame frame = new ERPFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public ERPFrame() {

        controller = new Controller();
        dfm = new DefaultTableModel();

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 1123, 730);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);


        table = new JTable(dfm);
        JScrollPane scrollPane = new JScrollPane(table);


        table.setBounds(343, 30, 734, 638);

        contentPane.add(table, scrollPane);
    } //end of constructor
}

I'm trying to get a JScrollPane for my JTable, but without success. Anyone got any idea of what I'm doing wrong? I have tried like everything, and searched for the problem for like 3 hours. How could it be so hard to add a scroll pane?

Upvotes: 0

Views: 383

Answers (1)

Reimeus
Reimeus

Reputation: 159874

The JTable is already the ViewPortView component of the JScrollPane. Replace

contentPane.add(table, scrollPane);

with

contentPane.add(scrollPane);

Also, don't use absolute positioning (null layout) - Use a layout manager instead. In the absence of a layout manager, the default size of the JScrollPane, along with every other component is 0 x 0 and will not appear. This forces you to specify the size and position of every component on the container.

Upvotes: 1

Related Questions