Kyle Weise
Kyle Weise

Reputation: 43

Trouble with JTables in GUI

So I'm working on a GUI for a movie rental system in Java, and here is a method for displaying what the user should see.

public void displayUser() //displays user screen/functionality
{
    getContentPane().removeAll();
    adminCheck = 1;

    menuBar = new JMenuBar();
    menu = new JMenu("Members");
    menuBar.add(menu);
    menu2 = new JMenu("Releases");
    menuBar.add(menu2);


    retrieveMember = new JMenuItem("Retrieve Member");
    menu.add(retrieveMember);
    retrieveRelease = new JMenuItem("Retrieve Release");
    menu2.add(retrieveRelease);

    JScrollPane c  = new JScrollPane();
    JScrollPane b = new JScrollPane();

    JTextField field1 = new JTextField("");
    JTextField field2 = new JTextField("");
    JTextField field3 = new JTextField("");
    JTextField field4 = new JTextField("");
    JTextField field5 = new JTextField("");
    JTextField field6 = new JTextField("");
    field1.setEditable(false);
    field2.setEditable(false);
    field3.setEditable(false);
    field4.setEditable(false);
    field5.setEditable(false);
    field6.setEditable(false);

    JPanel infoPanel = new JPanel(new GridLayout(0, 1));
    infoPanel.add(new JLabel("Last Name:"));
    infoPanel.add(field1);
    infoPanel.add(new JLabel("First Name:"));
    infoPanel.add(field2);
    infoPanel.add(new JLabel("Social Security #:"));
    infoPanel.add(field3);
    infoPanel.add(new JLabel("Email:"));
    infoPanel.add(field4);
    infoPanel.add(new JLabel("Phone Number:"));
    infoPanel.add(field5);
    infoPanel.add(new JLabel("Address"));
    infoPanel.add(field6);
    add(infoPanel, BorderLayout.CENTER);

    TableModel memberModel = new MemberTableModel();
    TableModel itemModel = new ItemTableModel();
    memberTable = new JTable(memberModel);
    releaseTable = new JTable(itemModel);

    c.add(memberTable);
    b.add(releaseTable);

    String[] searchMemberChoices = {"Search Members By:", "Last Name", "First Name","Phone Number", "Member ID"};
    String[] searchReleaseChoices = {"Search Releases By:", "Title" , "Category", "Type", "Release ID" };
    searchMember = new JComboBox(searchMemberChoices);
    searchRelease = new JComboBox(searchReleaseChoices);

    add(c, BorderLayout.WEST);
    add(b, BorderLayout.EAST);
    add(searchMember, BorderLayout.WEST);
    add(searchRelease, BorderLayout.EAST);
    add(menuBar, BorderLayout.NORTH);
    add(infoPanel, BorderLayout.CENTER);


    setVisible(true);
    setResizable(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    repaint();

    insertMember.addActionListener(new MenuActionListener());
    deleteMember.addActionListener(new MenuActionListener());
    editMember.addActionListener(new MenuActionListener());
    retrieveMember.addActionListener(new MenuActionListener());
    insertRelease.addActionListener(new MenuActionListener());
    deleteRelease.addActionListener(new MenuActionListener());
    editRelease.addActionListener(new MenuActionListener());
    retrieveRelease.addActionListener(new MenuActionListener());
    searchMember.addActionListener(new ComboBoxActionListener());
    searchRelease.addActionListener(new ComboBoxActionListener());

}

I am implementing my own TableModel class(not shown), extended from AbstractTableModel.
The problem is that the JTables(memberTable and itemTable) are not showing up. I initialize the Tables with TableModel, declare two JScrollPanes,add one table per pane, and add the panes to the frame. I don't understand why they aren't visible. Can anyone provide some assistance?

Note: The tables are declared globally, outside the scope of this one method.

Upvotes: 0

Views: 61

Answers (2)

Kevin Workman
Kevin Workman

Reputation: 42176

Look at these lines:

add(c, BorderLayout.WEST);
add(b, BorderLayout.EAST);
add(searchMember, BorderLayout.WEST);
add(searchRelease, BorderLayout.EAST);

You're adding two components to WEST and EAST, which won't work. You can only have one component per BorderLayout section. Note that you can nest layouts by adding components to a JPanel, and then adding that JPanel to your BorderLayout.

Upvotes: 2

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

As per the JScrollPane API, you don't want to add components directly to JScrollPanes since that squashes their viewports, but rather you want to add components to the JScrollPane's viewport as the viewport's view.

Change:

c.add(memberTable);
b.add(releaseTable);

to

c.setViewportView(memberTable);
b.setViewportView(releaseTable);

Upvotes: 2

Related Questions