KeyListeners in JPanels using a CardLayout


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: KeyListeners in JPanels using a CardLayout

  1. #1
    Join Date
    Apr 2005
    Posts
    18

    KeyListeners in JPanels using a CardLayout

    I'm making and testing a swing application using key listeners and CardLayout. I have two JPanels that can respond to key events; one counts and displays the number of key presses so far, the other displays what key was last pressed. I only want one of these panels visible at a time, and I want key presses to only affect the visible panel.

    I contain these two panels in a third JPanel that uses a CardLayout. This panel itself has a key listener to switch between the panels it's containing. I stick this panel in a JFrame for the final application.

    What I'm trying to do in general is make a JPanel that acts as a container for two other JPanels, only showing one at a time. If I press a certain key, like SPACE for example, the contained panels are switched so that the hidden one is visible and the visible one is hidden. If some other key is pressed, I want that key to affect and be processed by the visible panel, but not the hidden one.

    I sort of having it work the way I want right now, except I have to press TAB a lot. When I'm flipping between the two "inner" panels I have to press TAB before my key presses affect it. I have to press TAB again to be able to flip between my two inner panels again.

    If anyone can give me some tips I'd be appreciative.

    Thank you,

    Aaron J. M.

  2. #2
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Hmm... can you post some source code? (preferably where you are adding the key listener)

  3. #3
    Join Date
    Apr 2005
    Posts
    18

    Post

    O.K., here's the constructor for one of my inner panels (the other panel is the same idea):
    Code:
    public MyPanel1(int width, int height) {
    	
    	// ...
    	
    	setFocusable(true);
    	addKeyListener(new KeyAdapter() {
    		public void keyPressed(KeyEvent e) {
    			keyCode = e.getKeyCode();
    			repaint();
    			
    			System.out.println("Key press shower updated.");
    		}
    	});
    }
    Here's some code for my outer panel, which uses the CardLayout:
    Code:
    public CardPanel(int width, int height) {
    	
    	// ...
    			
    	setLayout(new CardLayout());
    		
    	setFocusable(true);
    	addKeyListener(new KeyAdapter() {
    		public void keyPressed(KeyEvent e) {
    			if (e.getKeyCode() == KeyEvent.VK_SPACE) {
    				showComponent();
    				System.out.println("Next component shown in CardPanel.");
    			}
    		}
    	});
    }
    
    public void addComponent(Component comp, String key) {
    	add(comp, key);
    }
    
    private void showComponent(/*String key*/) {
    	((CardLayout)getLayout()).next(this)/*.show(this, key)*/;
    }
    And here's part of the extended JFrame that I'm using to house my application:
    Code:
    public TestFrame(String title, int width, int height) {
    	
    	// ...
    	
    	Container contentPane = getContentPane();
    	contentPane.setLayout(new BorderLayout());
    	
    	CardPanel cards = new CardPanel(width, height);
    	MyPanel1 p1 = new MyPanel1(width, height);
    	MyPanel2 p2 = new MyPanel2(width, height);
    	
    	cards.addComponent(p1, P1);
    	cards.addComponent(p2, P2);
    	
    	contentPane.add(cards, BorderLayout.CENTER);
    	
    	// ...
    }
    Last edited by AaronJM; 12-25-2005 at 05:23 PM.

  4. #4
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    I sort of having it work the way I want right now, except I have to press TAB a lot. When I'm flipping between the two "inner" panels I have to press TAB before my key presses affect it. I have to press TAB again to be able to flip between my two inner panels again.
    I'm not really sure what you mean... so hitting the spacebar doesn't work until you hit tab? and you completely lost me on the second half of that quote.

  5. #5
    Join Date
    Mar 2004
    Posts
    635
    So you got a CardPanel containing 2 MyPanel's, but only want 1 panel visible at a time? Only add 1 panel to CardPanel then. When user presses "space", remove the current panel and add the 2nd one instead, and visa-versa.

    I think this might work with switching 2 panels.
    Code:
    public void switchComponent()
    {
        if(cardPanel.isAncestorOf(panel1))
        {
             cardPanel.remove(panel1);
             cardPanel.add(panel2);
        }
        else
        {
             cardPanel.remove(panel2);
             cardPanel.add(panel1);
        }
    }

  6. #6
    Join Date
    Apr 2005
    Posts
    18
    I'm not really sure what you mean... so hitting the spacebar doesn't work until you hit tab? and you completely lost me on the second half of that quote.
    I mean when I first start the application, switching the panels with the spacebar works fine. But when I press another key (other than tab), the panel currently visible isn't affected like it should be. It's only when I hit tab that key presses have an effect on the visible panel. But now I'm unable to switch panels with the spacebar anymore, until I hit tab again, where it goes back to what it's like when I first started the application. Sorry if this doesn't clear things up.


    So you got a CardPanel containing 2 MyPanel's, but only want 1 panel visible at a time? Only add 1 panel to CardPanel then. When user presses "space", remove the current panel and add the 2nd one instead, and visa-versa.
    If I'd just removing and inserting panels instead of just flipping between them, should my outer panel even be using a CardLayout? The whole reason I'm using a CardLayout is because I want to store multiple panels but only have one visible and active at a time.

  7. #7
    Join Date
    Mar 2004
    Posts
    635
    Well in Windows, TAB switches focus to the next component. My guess is that after switching, the panel no longer has the focus, and when you hit TAB, the focus is being set on the next component (in this case, your panel).

    You don't need a card layout for that.

Similar Threads

  1. JPanels in Java
    By Diane in forum Java
    Replies: 1
    Last Post: 02-24-2003, 08:25 AM
  2. cardLayout question
    By Robert in forum Java
    Replies: 1
    Last Post: 12-03-2000, 12:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center