Page 2 of 2 FirstFirst 12
Results 21 to 32 of 32
Like Tree1Likes

Thread: Problem understanding basic Simple Enumerators

  1. #21
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default

    Can you create a small simple program to demonstrate the problem. Your posted code is incomplete and does not compile.

  2. #22
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by eLancaster View Post
    ...I want to add an About Menu to the menubar. I thought of many simple ways of doing this (except for pop-up messages, which i hate). In the end I decided that i'd put two panels: an aboutPanel and a gamePanel. When the user clicks on the about menu, gamePanel.setVisible(false) & aboutPanel.setVisible(true) and when the user clicks on gameMenu, gamePanel.setVisible(true) and aboutPanel.setVisible(false).

    When I click on About, nothing happens - the menu button just takes on a clicked appearance. What am i doing wrong?
    If there's a better way of doing this, please do suggets - thanks!

    It sounds as if you want to use a CardLayout to swap JPanels, but I'm not sure since I can't compile or run your code. I agree with Norm that you might want to create and post a small compilable program that demonstrates your problem. Also, since this is a new problem, next time you'll want to start a new thread as you'll have better chances of getting help.

  3. #23
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    Sorry about the thread thing. Here's a tiny code that demonstrates my problem.
    I'm perfectly aware that this is the dumbest way to implement what i'm trying to do - but what I want is that when the user clicks on about, the gamePanel (with the TicTacToe game) should disappear and be replaced with the aboutPanel which has information about versions and stuff.

    Thanks for your help

    Whatever.java
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JPanel;
    
    
    public class Whatever {
    	
    	JPanel gamePanel;
    	JPanel aboutPanel;
    	
    	public Whatever()
    	{
    		JFrame frame = new JFrame("Example");
    		
    		JMenuBar menubar = new JMenuBar();
    		JMenu gameMenu = new JMenu("Game");
    		JMenu aboutMenu = new JMenu("About");
    		
    		gamePanel = new JPanel();
    		gamePanel.add(new JButton("I'm a button"));
    		
    		aboutPanel = new JPanel();
    		aboutPanel.add(new JLabel("Some stuff"));
    		aboutPanel.setVisible(false);
    		
    		gameMenu.addActionListener(
    			new ActionListener()
    			{
    				public void actionPerformed(ActionEvent event)
    				{
    					gamePanel.setVisible(true);
    					aboutPanel.setVisible(false);
    				}
    			}
    		);
    		
    		aboutMenu.addActionListener(
    				new ActionListener()
    				{
    					public void actionPerformed(ActionEvent event)
    					{
    						aboutPanel.setVisible(true);
    						gamePanel.setVisible(false);
    					}
    				}
    		);
    		
    		menubar.add(gameMenu);
    		menubar.add(aboutMenu);
    		frame.setJMenuBar(menubar);
    		frame.getContentPane().add(BorderLayout.CENTER, gamePanel);
    		frame.getContentPane().add(BorderLayout.NORTH, aboutPanel);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(400,400);
    		frame.setVisible(true);
    	}
    	
    	public static void main(String args[])
    	{
    		new Whatever();
    	}
    }

  4. #24
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    Oh and I read up on CardLayout - definitely not the kind of thing I want.

  5. #25
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by eLancaster View Post
    Oh and I read up on CardLayout - definitely not the kind of thing I want.
    Why do you say that?

  6. #26
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    Well CardLayout, according to the Tutorials, either use a drop-down menu or tabs. I don't want either of those. I'm using a menubar so that when the user clicks on the gameMenu, he sees a menu with options like one player, two player e.t.c - you can't really do that with Tabs or Drop-downs.

    The other way that i could do this is, when the user clicks on the about menu, remove everything from the gamePanel and add the aboutLabel - but that seems a lot of work and i'm quite certain there is a more proper way of doing it.

  7. #27
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default

    Do the actionListeners in the code you posted ever get called?

  8. #28
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by eLancaster View Post
    Well CardLayout, according to the Tutorials, either use a drop-down menu or tabs
    Where do the tutorials state that these are needed for CardLayout? Please show a link because you're not describing a CardLayout tutorial that I've ever seen and you're supposed limitations to CardLayout simply don't exist. I think that you're grossly misinterpreting whatever it is you're reading since it would likely help you if you re-review the tutorials and try to use them correctly.

  9. #29
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    Here's the link.
    I may have misinterpreted this quote:

    The CardLayout class manages two or more components (usually JPanel instances) that share the same display space. When using the CardLayout class, let the user choose between the components by using a combo box. The CardLayoutDemo application is an example to illustrate this feature.

    Another way to accomplish the same task is to use a tabbed pane.

  10. #30
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    That's one demo. Does it state that one is required to use combo box or tabbed pane? Again, please re-read the whole tutorial as you are grossly misinterpreting it.

  11. #31
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    @Fubarable : pardon my laziness :$

    OK, I've read up on why the actionListener didn't get called, and on CardLayout and both problems are solved. Thanks guys.

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.CardLayout;
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JPanel;
    import javax.swing.event.MenuEvent;
    import javax.swing.event.MenuListener;
    
    
    public class Whatever {
    	
    	CardLayout deckOfPanels;
    	JFrame frame;
    	JPanel panel;
    	JPanel gamePanel;
    	JPanel aboutPanel;
    	
    	public Whatever()
    	{
    		frame = new JFrame("Example");
    		
    		gamePanel = new JPanel();
    		gamePanel.add(new JButton("I'm a button"));
    		
    		aboutPanel = new JPanel();
    		aboutPanel.add(new JLabel("Some stuff"));
    		
    		deckOfPanels = new CardLayout();
    		
    		panel = new JPanel(deckOfPanels);
    		panel.setBackground(Color.WHITE);
    		panel.add(gamePanel, "Game");
    		panel.add(aboutPanel, "About");
    		
    		JMenuBar menubar = new JMenuBar();
    		
    		JMenu buttonMenu = new JMenu("Button");
    		buttonMenu.addMenuListener(new MenuListener()
    		{
    			public void menuCanceled(MenuEvent arg0) {}
    
    			@Override
    			public void menuDeselected(MenuEvent arg0) {}
    
    			public void menuSelected(MenuEvent arg0) {
    				deckOfPanels.show(panel, "Game");
    			}
    				
    		});
    		menubar.add(buttonMenu);
    		
    		JMenu aboutMenu = new JMenu("About");
    		aboutMenu.addMenuListener(new MenuListener()
    		{
    			public void menuCanceled(MenuEvent arg0) {}
    
    			public void menuDeselected(MenuEvent arg0) {}
    
    
    			public void menuSelected(MenuEvent arg0) {
    				deckOfPanels.show(panel, "About");
    			}
    				
    		});
    		menubar.add(aboutMenu);
    		
    		frame.setJMenuBar(menubar);
    		frame.getContentPane().add(BorderLayout.CENTER, panel);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(400,400);
    		frame.setVisible(true);
    	}
    	
    	public static void main(String args[])
    	{
    		new Whatever();
    	}
    }

  12. #32
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    The original program works too, didn't paste that cuz it's rather large.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Very basic java problem
    By mrparker2 in forum New To Java
    Replies: 6
    Last Post: 05-26-2011, 08:50 PM
  2. Replies: 1
    Last Post: 05-10-2011, 08:10 AM
  3. Replies: 9
    Last Post: 09-21-2010, 04:15 PM
  4. Triangles java problem (basic help)
    By adz06 in forum New To Java
    Replies: 5
    Last Post: 10-31-2009, 06:58 PM
  5. What am I doing wrong in this basic problem
    By Blaedel in forum New To Java
    Replies: 10
    Last Post: 10-11-2009, 07:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •