-1

I can't see my DefaultTableModel inside my frame please, help .. Where I'm doing wrong?

here is my codes:

package phonebook;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;    
import java.awt.Font;    
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class MyProgram {

    private JFrame frame;
    private JTable table;
    private DefaultTableModel tableModel;

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

    public MyProgram() {
        initialize();
    }

    private void initialize() {

        tableModel=new DefaultTableModel();
        tableModel.addColumn("Name");
        tableModel.addColumn("Phone no.");
        tableModel.addColumn("City");
        tableModel.addColumn("Country");

        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JMenuBar menuBar = new JMenuBar();
        menuBar.setBounds(0, 0, 434, 21);
        frame.getContentPane().add(menuBar);

        JMenu mnFile = new JMenu("File");
        menuBar.add(mnFile);

        JMenuItem mntmNew = new JMenuItem("New ");
        mnFile.add(mntmNew);

        JMenuItem mntmExit = new JMenuItem("Exit");
        mnFile.add(mntmExit);

        JMenu mnAbout = new JMenu("About");
        menuBar.add(mnAbout);

        JMenuItem mntmAbout = new JMenuItem("About");
        mnAbout.add(mntmAbout);

        JLabel lblNewLabel = new JLabel("Welcome to Phonebook diary by Rajendra arora");
        lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
        lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel.setBounds(0, 21, 434, 50);

        frame.getContentPane().add(lblNewLabel);
        frame.add(new JScrollPane(table), BorderLayout.CENTER);

        table = new JTable(tableModel);
        table.setBounds(10, 260, 414, -189);
        frame.getContentPane().add(table);
    }
}
ThomasEdwin
  • 2,035
  • 1
  • 24
  • 36
Rajendra Arora
  • 43
  • 1
  • 1
  • 6
  • That's maybe because you haven't added it to the frame :) – Petr Mensik Jul 24 '14 at 09:18
  • No, see my code i added it :) – Rajendra Arora Jul 24 '14 at 09:19
  • @RajendraArora, its about time you started "accepting" answer when you are given answers to you questions. You got valid answers here and http://stackoverflow.com/questions/24913954/how-to-insert-all-database-values-into-a-column-table-list and http://stackoverflow.com/questions/24908535/switch-from-one-input-dialog-box-to-another-input-dialog-box and yet you have not accepted a single answer. – camickr Jul 24 '14 at 14:54

1 Answers1

4
  1. setLayout(null); null layouts will always come back to bite you
  2. frame.add(new JScrollPane(table), BorderLayout.CENTER); this is going to cause problems when using a null layout, as components have a default size of 0x0. Also, at the time this is called, table is null
  3. table.setBounds(10, 260, 414, -189) components can't have a negative size
  4. Don't add menu bars to your frame, use setJMenuBar instead

As previously recommend, use an appropriate layout managers.

If you can't find a single layout manager to meet your needs, don't be afraid to use multiple layouts across multiple containers to build the effect you want

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366