0

I have three Java classes:

a JFrame: HomeView, a JPanel: TopicListView, another JPanel: ReplyListView.

In HomeView, I have a menu item that I can click to display TopicListView. In TopicListView, I want to have a button that I can click to display ReplyListView. When the button is clicked, it will invoke the openReplyListView() method. The method will create a new JPanel and replace the current JPanel with it. However, the code in openReplyListView() do not work.

Note: I am not using CardLayout.

Any help would be appreciated on how to get this working.

Thanks in advance.

HomeView class:

public class HomeView extends JFrame {

private JPanel contentPane;
private JMenuBar menuBar;
private JMenu mnForum;
private JMenuItem mntmReply;
private JMenuItem mntmTopic;

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

/**
 * Create the frame.
 */
public HomeView() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 1280, 720);
    setJMenuBar(getMenuBar_1());
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);
}

private JMenuBar getMenuBar_1() {
    if (menuBar == null) {
        menuBar = new JMenuBar();
        menuBar.add(getMnForum());
    }
    return menuBar;
}
private JMenu getMnForum() {
    if (mnForum == null) {
        mnForum = new JMenu("Forum");
        mnForum.add(getMntmTopic());
        mnForum.add(getMntmReply());
    }
    return mnForum;
}
private JMenuItem getMntmReply() {
    if (mntmReply == null) {
        mntmReply = new JMenuItem("Reply");
        mntmReply.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                JPanel panel = new ReplyView();
                getContentPane().removeAll();
                getContentPane().add(panel);
                getContentPane().validate();
                getContentPane().repaint();
            }
        });
    }
    return mntmReply;
}


private JMenuItem getMntmTopic() {
    if (mntmTopic == null) {
        mntmTopic = new JMenuItem("Topic");
        mntmTopic.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                JPanel panel = new TopicListView();
                getContentPane().removeAll();
                getContentPane().add(panel);
                getContentPane().validate();
                getContentPane().repaint();
            }
        });
    }
    return mntmTopic;
}
}

TopicListView class:

public class TopicListView extends JPanel {
private JTable table;
private JScrollPane scrollPane;
private JLabel lblTopics;


/**
 * Create the panel.
 */
public TopicListView() {
    setLayout(null);
    add(getTable());
    add(getScrollPane());
    add(getLblTopics());
}

**Code snippet for the table and the scrollpane:**


private void openReplyListView(){
    JPanel panel = new ReplyListView();
    getContentPane().removeAll();
    getContentPane().add(panel);
    getContentPane().validate();
    getContentPane().repaint();

}

ReplyListView class (In general it's the same as TopicListView)

public class ReplyListView extends JPanel {
private JTable table;
private JScrollPane scrollPane;
private JLabel lblTest;

/**
 * Create the panel.
 */
public ReplyListView() {
    setLayout(null);
    add(getTable());
    add(getScrollPane());
    add(getLblTest());

}
private JTable getTable() {
    if (table == null) {
        table = new JTable();
        table.setBounds(414, 114, 464, 354);
    }
    return table;
}
private JScrollPane getScrollPane() {
    if (scrollPane == null) {
        scrollPane = new JScrollPane(getTable());
        scrollPane.setBounds(414, 114, 464, 349);
    }
    return scrollPane;
}
private JLabel getLblTest() {
    if (lblTest == null) {
        lblTest = new JLabel("TEST");
        lblTest.setFont(new Font("Tahoma", Font.PLAIN, 30));
        lblTest.setBounds(593, 35, 81, 68);
    }
    return lblTest;
}
}
Joseph Farrugia
  • 327
  • 1
  • 3
  • 14
AmuletxHeart
  • 396
  • 4
  • 16
  • Use a `CardLayout` much easier. See [here](http://stackoverflow.com/a/14012757/1133011) for an example (scroll towards bottom of post). Also dont use null layout use an appropriate LayoutManager – David Kroukamp Jan 12 '13 at 14:00
  • How do I get this particular case to work? i.e. change the JPanel from within a JPanel. I do not wish to rework the whole program just for this one situation. Also if I don't use null layout, I can't freely position the components. Which layout allow me to freely position? – AmuletxHeart Jan 12 '13 at 14:43

1 Answers1

2

Your TopicListView have no LayoutManger (i.e. setLayout(null) in constructor).

It's usually a bad idea. (I'm pretty sure that's the cause of your problem)

Try something like this

private void openReplyListView(){
    JPanel panel = new ReplyListView();
    removeAll();
    setLayout(new BorderLayout());
    add(panel, BorderLayout.CENTER);
    validate();
    repaint();
}
ben75
  • 29,217
  • 10
  • 88
  • 134
  • 1
    your answer is correct, but please to add suggestion about CardLayout and JFrame.pack after switching betweens cards then will be nice answer – mKorbel Jan 12 '13 at 11:32
  • I am using absolute layout so I can place components wherever I like. The error i am getting is "The method getContentPane() is undefined for the type TopicListView". – AmuletxHeart Jan 12 '13 at 12:09
  • JFrame.get.........., but you extending JFrame, then have to create as local variable, same as the JPanels, then JFrame is accesible without any woodoo from all methods, classes inside whole / current JVM :-) – mKorbel Jan 12 '13 at 12:18
  • The file TopicListView is a JPanel file (extends JPanel) and does not contain a JFrame. – AmuletxHeart Jan 12 '13 at 13:21
  • @ben75 Why do I have to have a LayoutManger? If I want to freely position my components, which one allows me to do that? – AmuletxHeart Jan 12 '13 at 14:57
  • @AmuletxHeart setLayout(null) allows to do absolute positioning... but you have to do it by hand: I means call `setLocation()` and `setSize()` on every component (if you don't do that you will probably have a blank screen) – ben75 Jan 12 '13 at 15:02