Results 1 to 7 of 7
  1. #1
    dd2308 is offline Member
    Join Date
    Sep 2012
    Posts
    28
    Rep Power
    0

    Default Card Layout show

    Hi,
    I am trying to implement the show method, of cardlayout. However i cannot get it to work.
    Java Code:
    public class Main {
    
    		JFrame guiFrame;
    		CardLayout cards;
    		JPanel cardPanel;
    public static void main(String[] args) {
    
    			// Use the event dispatch thread for Swing components
    			EventQueue.invokeLater(new Runnable() {
    
    				@Override
    				public void run() {
    
    					try {
    						new Main();
    					} catch (IOException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    				}
    			});
    
    		}
    public Main() throws IOException {
    			guiFrame = new JFrame();
    
               guiFrame.setSize(800, 600);
                     cards = new CardLayout();
    		cardPanel = new JPanel();
    		cardPanel.setLayout(cards);
                   
                    final JPanel[][] JP = new JPanel[27][2];
    		       for (int i = 0; i < JP.length; i++) {
    			
    			JP[i][1] = new JPanel();
    
    
    			JP[i][1].setBackground(Color.white);
    			cardPanel.add(JP[i][1]);
    
    		}
    
             JButton [][] Category = new JButton[5][2];
    		for (int i = 0; i < Category.length; i++) {
    			JButton bRec = new JButton();
    			Category[i][1] = bRec;
    			JP[1][1].add(bRec);
    			bRec.setFont(FirstFont);
    			bRec.setBorder(BorderFactory.createLineBorder(Color.black));
    			bRec.setBackground(BackGround);
    			bRec.setForeground(Color.white);
    
                        ButQuestions500[0][1].addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent event) {
    				cards.show(cardPanel, "JP5");
    				
    			}
    		});
                    guiFrame.add(cardPanel, BorderLayout.CENTER);// add to frame in center
    		guiFrame.add(backTab, BorderLayout.SOUTH);
    		guiFrame.setVisible(true);
                        }// Showing only problematic area
    Whether i use JP[5][1].getName()
    or the current method it will not flip to that card.

    Thanks

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Card Layout show

    I think you are trying to use (call) the show() method of CardLayout, rather than implement it.

    Note, from its API docs, that it wants as argumets both the container with the layout, and the name of the thing to show.

    Java Code:
    cards.show(cardPanel, "JP5");
    As far as I can tell you just made up the name "JP5": it isn't the actual name of any of the panels. So nothing will happen.

    Whether i use JP[5][1].getName()
    or the current method...
    I guess you mean you tried

    Java Code:
    cards.show(cardPanel, JP[5][1].getName());
    but, again, you never gave JP[5][1] a name, so it doesn't have one to return with getName().

    Try giving the panel a name. Do this at the time you add the panel to the container. There's an example of this on the How to Use CardLayout page of Oracle's Tutorial.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Card Layout show

    ... and I meant to add: follow standard Java coding conventions and begin variables with a lowercase letter. Eg category. And make the variables descriptive of what they are.

  4. #4
    dd2308 is offline Member
    Join Date
    Sep 2012
    Posts
    28
    Rep Power
    0

    Default Re: Card Layout show

    I tried setting the name of a Panel and then retrieving it, but still nothing.
    Java Code:
    JP[5][1].setName("Hello");
    ...
    cards.show(cardPanel, "Hello");
    or
    Java Code:
    JP[5][1].setName("Hello");
    ...
    cards.show(cardPanel, JP[5][1].getName());

  5. #5
    dd2308 is offline Member
    Join Date
    Sep 2012
    Posts
    28
    Rep Power
    0

    Default Re: Card Layout show

    I understand now, when adding to the cardPanel i have to add a name
    cardPanel.add (JP[i][1],"JP"+i)

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Card Layout show

    Setting the name as part of adding the panel to the container, then showing the panel using the same name (as illustrated on that tutorial page) works for me. But that involved changing your code somewhat as ButQuestions500 is not declared anywhere.

    Fwiw, I added the button listener to bRec. Also I put some text into each of the bRec buttons, and added some content to each of the panels added to the JP array so I could see what gui changes were taking place. But none of that is of much use to you: better, I think if you post compilable, runnable code. You can remove the stuff that has no bearing on the problem (fonts, backgrounds etc), but the resulting code should be runnable if others are to see the problem.

    Given what you tried: did you read the tutorial example? and see how the components were added to the container? Eg the String BUTTONPANEL was used both for adding and showing. (For showing the string was obtained via the combobox.) You don't have to use static final strings, but you should use strings that the container (and its layout manager) know about in order to successfully switch to a given panel by name.

    ---

    It is worth changing the variable names for legibility. It becomes a habit after a while and, hence, effortless.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Card Layout show

    Quote Originally Posted by dd2308 View Post
    I understand now, when adding to the cardPanel i have to add a name
    cardPanel.add (JP[i][1],"JP"+i)
    Yes, exactly! Well done.

    Post back if that change isn't enough.

Similar Threads

  1. Card Layout with Buttons
    By jazob in forum AWT / Swing
    Replies: 5
    Last Post: 01-13-2012, 02:04 AM
  2. Card Layout with Buttons
    By jazob in forum New To Java
    Replies: 1
    Last Post: 01-12-2012, 11:12 PM
  3. Image in Card Layout
    By poorbrain in forum Java Applets
    Replies: 4
    Last Post: 03-08-2011, 06:15 PM
  4. Question Card Layout, Card Management
    By lrichil in forum AWT / Swing
    Replies: 1
    Last Post: 04-22-2010, 10:11 AM
  5. Card Layout
    By Gilbee in forum NetBeans
    Replies: 3
    Last Post: 03-03-2009, 09:37 PM

Tags for this Thread

Posting Permissions

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