0

I am trying to add a table to my GUI but when I change the layout of the frame from GridLayout to NULL it goes missing from the GUI?

Code below for creation of the GUI:

    public SalariedEmployeeGUI() {

    setLayout(null);
    getContentPane().setBackground(Color.WHITE);


    JPanel salariedEmpTablePanel = new JPanel();
    salariedEmpTablePanel.setLayout(null);
    salariedEmpTablePanel.setLocation(60,110);
    salariedEmpTablePanel.setSize(550,390);
    salariedEmpTablePanel.setBorder(BorderFactory.createTitledBorder(
            BorderFactory.createEtchedBorder(EtchedBorder.RAISED, 
                    Color.BLUE, Color.DARK_GRAY), "Salaried Employee Table"));
    salariedEmpTablePanel.setBackground(Color.WHITE);

    sEmpTable = new JTable();
    sEmpTable.setPreferredScrollableViewportSize(new Dimension(550, 390));
    JScrollPane scrollPane = new JScrollPane(sEmpTable);
    salariedEmpTablePanel.add(sEmpTable);


    add(salariedEmpTablePanel);
    setupTable();
    loadTable();

The setupTable and loadTable methods are below.. but I think they work fine:

    private void setupTable() {
    payrollQueries = new PayrollQueries();
    tableModel = new DefaultTableModel();
    tableModel.setColumnCount(5);
    tableModel.setColumnIdentifiers(new String[]{"First Name",
        "Last Name", "Type", "SSN", "Weekly Salary"});
    sEmpTable.setModel(tableModel);
}

private void loadTable() {
    entries = payrollQueries.getSalariedEmployee();
    int tableRow = 0;
    tableModel.setNumRows(entries.size());

    System.out.println("number of salaries employees: " + entries.size());

    for (SalariedEmployee se : entries) {
        tableModel.setValueAt(se.getFirstName(), tableRow, 0);
        tableModel.setValueAt(se.getLastName(), tableRow, 1);
        tableModel.setValueAt(se.getSocialSecurityNumber(), tableRow, 2);
        tableModel.setValueAt(se.geteType(), tableRow, 3);
        tableModel.setValueAt(se.getWeeklySalary(), tableRow, 4);
        tableRow++;
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 5
    Welcome to Stack Overflow! Don't use `null` layouts. Pixel perfect layouts are an illusion in modern UI design, you have no control over fonts, DPI, rendering pipelines or other factors that will change the way that you components will be rendered on the screen. Swing was designed to work with layout managers to overcome these issues. If you insist on ignoring these features and work against the API design, be prepared for a lot of headaches and never ending hard work... – MadProgrammer Jun 03 '14 at 02:07

1 Answers1

4

The short answer is that it's because when using null layout, you are responsible for specifying the exact position and size of all components added.

The correct answer is: This is one more reason why you should not use null layout. Period.

Use of null layout makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain. Instead you will want to study and learn the layout managers and then nest JPanels, each using its own layout manager to create pleasing and complex GUI's that look good on all OS's.

The layout manager tutorial can be found here: Layout Manager Tutorial


Edit
Also note that you are adding your JTable to two containers, a JPanel and a JScrollPane. Do not do this. Only add it to the JScrollPane and then add the scrollPane to wherever you wish.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373