user3744388
user3744388

Reputation: 11

Adding a JTable to JFrame

I want to add test2 to my DFrametest class, so that the table of test2 shows up in my window. I get an empty window and can't find the mistake.

public class test2 extends JPanel {

    JTable tbl;
    DefaultTableModel dt;

    public test2(){
        JLabel label = new JLabel("Course Lookup GUI");
        this.add( label );
        tbl = new JTable();
        dt = new DefaultTableModel();
        dt.addColumn("ID");
        dt.addColumn("Name");
        tbl.setModel(dt);

        try{
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/lagerverwaltungwin", "root", "");
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("SELECT ArtNr, Beschreibung FROM artikel");  
        }catch(Exception e){
            e.printStackTrace();
        }

        JScrollPane jp = new JScrollPane();
        jp.getViewport().add(tbl);
        add(jp);
    }

}

This is my Frameclass which should have the table from test2:

public class DFrametest extends JFrame {

    private JPanel contentPane;

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

    public DFrametest() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        test2 t = new test2();
        this.add(t);
        this.setVisible(true);
    }

}

Upvotes: 1

Views: 93

Answers (2)

Arijit
Arijit

Reputation: 1674

Never Use null Layout. Also donot use setBounds(100, 100, 450, 300); use pack();

Change your DFrametest class like this

        contentPane.setLayout(new BorderLayout());
        contentPane.add(t, BorderLayout.CENTER);

Full Code of DFrametest class:

public class DFrametest extends JFrame {

    private JPanel contentPane;

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

    public DFrametest() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        test2 t = new test2();
        contentPane.setLayout(new BorderLayout());
        contentPane.add(t, BorderLayout.CENTER);

        pack();
    }

}

Upvotes: 2

user3505725
user3505725

Reputation: 295

Try to use setBounds() method since you haven't mentioned any layout (setLayout is null). Add setBounds method before adding the jpanel to the jframe

setBounds(x-axis,y-axis,width,height);

EX:-

   setBounds(10,20,200,400);

Note:

Upvotes: -1

Related Questions