2

I need to create an application with single window and without any popups. Everything works using same window and only changing content. I am going to create application using swing.

I have read Java: single frame vs multiple frames but I don't think that would suite my requirements because when application will grow it get's more difficult to maintain.

I would like to have MVC design. I am thinking about creating some sort of stack of controllers where controller will load view and model. So when I'll need to go back simply pop current controller and use previous one. I can be wrong about my design, if anyone have any suggestions how to implement such application feel free to comment.

Thank's for your time.

I have tried creating prototype class which would hold stack of containers to switch between.

public class WindowManager { protected Stack<Container> frames;// contains frames stack to navigate protected JFrame wnd; // frame to show public WindowManager() { this.frames = new Stack<>(); this.wnd = new JFrame(); wnd.setSize(640, 480); //wnd.setVisible(true); } public void addFrame(Container c) { this.frames.push(this.wnd.getContentPane()); this.wnd.setContentPane(c); } public WindowManager removeFrame() { Container c = this.frames.pop(); wnd.setContentPane(c); return this; } public void showWindow() { wnd.setVisible(true); } 

}

Instead of containers stack there would be Controllers stack which would load view and model.

2
  • Can you show us what have you tried? Commented Feb 17, 2013 at 17:13
  • Some aspects of your top-level design may reflect your containment hierarchy, but MVC is orthogonal to this. See also this Q&A. Commented Feb 17, 2013 at 17:19

1 Answer 1

3

You should probably create several JPanel objects and put them into CardLayout, so only one page is visible at a time. See the following example:

import java.awt.BorderLayout; import java.awt.CardLayout; import java.awt.event.ActionEvent; import javax.swing.AbstractAction; import javax.swing.Box; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class CardLayoutExample extends JFrame { public CardLayoutExample () { setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); getContentPane ().setLayout (new CardLayout ()); getContentPane ().add (new Fruits (), "Fruits"); getContentPane ().add (new Seasons (), "Seasons"); getContentPane ().add (new Colors (), "Colors"); getContentPane ().add (new Towns (), "Towns"); } public void switchTo (String page) { ((CardLayout)getContentPane ().getLayout ()).show (getContentPane (), page); } public static void main (String [] args) { SwingUtilities.invokeLater (new Runnable() { @Override public void run () { CardLayoutExample example = new CardLayoutExample (); example.pack (); example.setVisible (true); } }); } private class Colors extends JPanel { public Colors () { Box box = Box.createHorizontalBox (); box.add (new JButton (new AbstractAction("Red") { @Override public void actionPerformed (ActionEvent e) { switchTo ("Fruits"); } })); box.add (Box.createHorizontalStrut (8)); box.add (new JButton (new AbstractAction("Green") { @Override public void actionPerformed (ActionEvent e) { switchTo ("Seasons"); } })); box.add (Box.createHorizontalStrut (8)); box.add (new JButton (new AbstractAction("Blue") { @Override public void actionPerformed (ActionEvent e) { switchTo ("Towns"); } })); setLayout (new BorderLayout ()); add (box, BorderLayout.CENTER); } } private class Fruits extends JPanel { public Fruits () { Box box = Box.createVerticalBox (); box.add (new JButton (new AbstractAction("Apple") { @Override public void actionPerformed (ActionEvent e) { switchTo ("Colors"); } })); box.add (Box.createVerticalStrut (8)); box.add (new JButton (new AbstractAction("Orange") { @Override public void actionPerformed (ActionEvent e) { switchTo ("Seasons"); } })); box.add (Box.createVerticalStrut (8)); box.add (new JButton (new AbstractAction("Pear") { @Override public void actionPerformed (ActionEvent e) { switchTo ("Towns"); } })); setLayout (new BorderLayout ()); add (box, BorderLayout.CENTER); } } private class Seasons extends JPanel { public Seasons () { Box box = Box.createHorizontalBox (); box.add (new JButton (new AbstractAction("Winter") { @Override public void actionPerformed (ActionEvent e) { switchTo ("Colors"); } })); box.add (Box.createHorizontalStrut (8)); box.add (new JButton (new AbstractAction("Spring") { @Override public void actionPerformed (ActionEvent e) { switchTo ("Fruits"); } })); box.add (Box.createHorizontalStrut (8)); box.add (new JButton (new AbstractAction("Summer") { @Override public void actionPerformed (ActionEvent e) { switchTo ("Towns"); } })); setLayout (new BorderLayout ()); add (box, BorderLayout.CENTER); } } private class Towns extends JPanel { public Towns () { Box box = Box.createVerticalBox (); box.add (new JButton (new AbstractAction("Moscow") { @Override public void actionPerformed (ActionEvent e) { switchTo ("Colors"); } })); box.add (Box.createVerticalStrut (8)); box.add (new JButton (new AbstractAction("London") { @Override public void actionPerformed (ActionEvent e) { switchTo ("Fruits"); } })); box.add (Box.createVerticalStrut (8)); box.add (new JButton (new AbstractAction("Tokyo") { @Override public void actionPerformed (ActionEvent e) { switchTo ("Seasons"); } })); setLayout (new BorderLayout ()); add (box, BorderLayout.CENTER); } } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.