Results 1 to 7 of 7
  1. #1
    Reskaillev is offline Member
    Join Date
    Jul 2011
    Posts
    53
    Rep Power
    0

    Default Notifying a cardlayout manager from within a custom Panel

    So the problem goes as follows:

    I want to make a JApplet with multiple panels using a cardlayout manager. The JPanel that uses the Cardlayout is called "cards".

    The first panel (that is inserted in "cards") called "mainMenu" has 4 JButtons implemented (using Boxlayout).

    Now the Panel "mainMenu" is a member of the custom made class "MainPanel" that extends JPanel.
    The 4 JButtons are thus initiated in the constructor of this custom class "MainPanel".

    Now I add ActionListeners to the JButtons (Im still in the custom class "MainPanel")

    The Question: How to use the CardLayout JPanel "cards" to swap to the appropriate JPanel? (within the MainPanel class)

    (Within the Japplet swapping is easy using
    Java Code:
    	public void itemStateChanged(ItemEvent evt) {
    		// TODO Auto-generated method stub
    		CardLayout cl = (CardLayout)(cards.getLayout());
            cl.show(cards, (String)evt.getItem());
    	}
    by adding the appropriate listener to a component
    )

    Or could it be that I'm using CardLayout wrong?

    Thnx in advance
    Last edited by Reskaillev; 08-23-2011 at 12:28 AM.

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

    Default

    I'm not quite sure how your program is set up or just what your problem is exactly based on your description above.

  3. #3
    Reskaillev is offline Member
    Join Date
    Jul 2011
    Posts
    53
    Rep Power
    0

    Default

    Pardon me,

    Let me restart with better examples

    So I have a Japplet:

    Java Code:
    public class Marauders extends JApplet {
    	
    	JPanel cards;
    	final static String buttonPanel = "Card with JButtons";
    	final static String infoPanel = "Card with Information";
    	
    	public void init()
    	{
    		
    	        JPanel main = new MainPanel();
    	        
    	        JPanel controls = new JPanel();
    	        controls.add(new JTextField("TextField", 20));
    	        
    	        cards = new JPanel(new CardLayout());
    	        cards.add(main, BUTTONPANEL);
    	        cards.add(controls, TEXTPANEL);
    	        
    	        this.setSize(650, 400);
    	        this.add(cards, BorderLayout.CENTER);
    	}
    }
    Then I go into my MainPanel class:

    Java Code:
    public class MainPanel extends JPanel implements ActionListener {
    
    	public MainPanel()
    	{
    		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    		AddAButton("New", this);
                    AddAButton("Load", this);
                    AddAButton("Objective", this);
                    AddAButton("Controls", this);
    	}
    	
        private void AddAButton(String text, Container container) {
            JButton button = new CustomButton(text);
            button.setAlignmentX(Component.CENTER_ALIGNMENT);
            button.setMaximumSize(new Dimension(100, 25));
            button.setActionCommand(text);
            button.addActionListener(this);
            container.add(button);
        }
    	
        @Override
        public void paintComponent(Graphics g) {
        	g.setColor(Color.BLACK);
        	g.fillRect(0, 0, getWidth(), getHeight());
        }
    
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    }
    Now I would like the actionPerformed method of the MainPanel to swap the cards (the JPanel in the JApplet). I want to swap to the "controls" JApplet

    the question again: Is this possible? Am I using CardLayout wrongly? What should I use instead?

    Thanks in advance and sorry for my unclearness, (hope I made the Japplet and plane short enough)

    Resk
    Last edited by Reskaillev; 08-23-2011 at 12:52 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I recommend that you allow Marauders to control its own components but also give it public methods so other classes can ask it to do things. For instance, have Marauders control the swapping of JPanels but giving it a public method say swapCard(...) that allows other classes to get it to swap its views, and I've implemented the method as follows:

    Java Code:
       public void swapCards(String key) {
          cardsLayout.show(cards, key);
       }
    Note that I've made the CardLayout object a private class field so it is more easily accessable. I've also made the CardLayout key Strings public constants so that outside classes can use them.

    Now for an outside class to be able to call this method, it will need a reference to the current Marauder object which is easy to do by passing the object into the other via either a constructor or method parameter. I've used a method parameter so we don't have to worry about which class gets constructed first:

    Java Code:
    class MainPanel extends JPanel implements ActionListener {
       //...
       private Marauders marauders = null;
    
       public MainPanel() {
          //...
       }
    
       public void setMauraders(Marauders marauders) {
          this.marauders = marauders;
       }
    And then have MainPanel call this method like so:
    Java Code:
       @Override
       public void actionPerformed(ActionEvent e) {
          String actionCommand = e.getActionCommand();
          if (actionCommand.equals(CONTROLS)) {
             marauders.swapCards(Marauders.TEXT_PANEL); // uses constant from Marauders.
          }
       }

    The whole thing looks like this:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class Marauders extends JApplet {
    
       public static final String BUTTON_PANEL = "Card with JButtons";
       public static final String INFO_PANEL = "Card with Information";
       public static final String TEXT_PANEL = "text panel";
    
       private CardLayout cardsLayout = new CardLayout();
       private JPanel cards = new JPanel(cardsLayout);
       
    
       @Override
       public void init() {
          MainPanel main = new MainPanel();
          JPanel controls = new JPanel();
          controls.add(new JTextField("TextField", 20));
          cards.add(main, BUTTON_PANEL);
          cards.add(controls, TEXT_PANEL);
    
          main.setMauraders(this);
          
          this.setSize(650, 400);
          this.add(cards, BorderLayout.CENTER);
       }
       
       public void swapCards(String key) {
          cardsLayout.show(cards, key);
       }
    }
    
    class MainPanel extends JPanel implements ActionListener {
       private static final String CONTROLS = "Controls";
       private Marauders marauders = null;
    
       public MainPanel() {
          this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
          AddAButton("New", this);
          AddAButton("Load", this);
          AddAButton("Objective", this);
          AddAButton(CONTROLS, this); // using constants decreases the chances of a spelling or capitalization error
       }
    
       public void setMauraders(Marauders marauders) {
          this.marauders = marauders;
       }
    
       private void AddAButton(String text, Container container) {
          JButton button = new CustomButton(text);
          button.setAlignmentX(Component.CENTER_ALIGNMENT);
          button.setMaximumSize(new Dimension(100, 25));
          button.setActionCommand(text);
          button.addActionListener(this);
          container.add(button);
       }
    
       @Override
       public void paintComponent(Graphics g) {
          g.setColor(Color.BLACK);
          g.fillRect(0, 0, getWidth(), getHeight());
       }
    
       @Override
       public void actionPerformed(ActionEvent e) {
          String actionCommand = e.getActionCommand();
          if (actionCommand.equals(CONTROLS)) {  // using constants decreases the chances of a spelling or capitalization error
             marauders.swapCards(Marauders.TEXT_PANEL);
          }
       }
    
    }
    
    // added so your classes compile. You should simplify your posted code 
    // so we don't have to do this
    class CustomButton extends JButton {
       public CustomButton(String text) {
          super(text);
       }
    }
    Addendum:
    This is really nothing more than basic object oriented programming, and one way to think of OOPs is as actors in a play, and throughout your program each actor gets other actors to do things by talking to them or in other words by sending them messages (calling a method). So here your MainPanel object has to send a message to change the card in your cards panel to the Marauders object.
    Last edited by Fubarable; 08-23-2011 at 01:30 AM.

  5. #5
    Reskaillev is offline Member
    Join Date
    Jul 2011
    Posts
    53
    Rep Power
    0

    Default

    Clever and actually very simple!

    By implementing the marauders object into the other it is indeed easy to get the swapping to work.

    One of the main problems of beginning java prpgrammers is finding the right objects to solve
    the problems, Seeing that they have no idea what the full extend of java is.

    So is this the right method to implement that kind of structure?

    One of my primary problems with this program was that I could not draw an image on my JPanel from within the JApplet. That's why I made a new JApplet Child. Bad Idea/Good Idea?

    Anyways, thanks a lot for the information (still have the feeling that this method (not your part, but mine) can be simplified a lot by using other methods)

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Reskaillev View Post
    So is this the right method to implement that kind of structure?
    I'm not sure without more information.


    One of my primary problems with this program was that I could not draw an image on my JPanel from within the JApplet. That's why I made a new JApplet Child. Bad Idea/Good Idea?
    I don't know what you mean by "I could not draw an image on my JPanel from with the JApplet" or what you mean by "new JApplet Child". Could you elaborate?

  7. #7
    Reskaillev is offline Member
    Join Date
    Jul 2011
    Posts
    53
    Rep Power
    0

    Default

    let me elaborate on that: (I also made a mistake by saying JApplet and not JPanel like I intended)

    To get the graphics I wanted I had to make a custom class "MainPanel" and that gave me the aformentioned problem.

    Now if I could have just created the entire "main" JPanel IN the Japplet (thus without creating a new custom class of JApplet) I wouldn't have the entire problem

    but that seemed not wise to me and I created the custom class...

    (By child of JPanel I ment to say that my MainPanel is a child of the JPanel class)
    Last edited by Reskaillev; 08-23-2011 at 12:18 PM.

Similar Threads

  1. CardLayout Help
    By David M. in forum New To Java
    Replies: 4
    Last Post: 08-06-2011, 03:57 AM
  2. JTextPane/Custom JPanel problem
    By adwart in forum AWT / Swing
    Replies: 7
    Last Post: 08-01-2011, 03:59 AM
  3. Custom JPanel
    By morris4019 in forum AWT / Swing
    Replies: 4
    Last Post: 04-08-2011, 10:40 PM
  4. Help with CardLayout
    By Kyle227 in forum New To Java
    Replies: 4
    Last Post: 05-28-2010, 02:03 AM
  5. Regarding CardLayout
    By adeeb in forum AWT / Swing
    Replies: 1
    Last Post: 06-07-2008, 08:52 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
  •