Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Card Layout button glitch

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

    Default Card Layout button glitch

    Hi,
    I reacently tried using card layout, and i got it to work ,but when i try to add buttons i get these errors;
    Java Code:
    
    public class CardLayoutExample {
    	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 CardLayoutExample();
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    			}
    		});
    
    	}
    
    	public CardLayoutExample() throws IOException {
    		guiFrame = new JFrame();
    
    		// make sure the program exits when the frame closes
    		guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		guiFrame.setTitle("CardLayout Example");
    		guiFrame.setSize(800, 600);
    
    		// This will center the JFrame in the middle of the screen
    		guiFrame.setLocationRelativeTo(null);
    		guiFrame.setLayout(new BorderLayout());
    
    		// creating a border to highlight the JPanel areas
    		Border outline = BorderFactory.createLineBorder(Color.black);
    
    		JPanel tabsPanel = new JPanel();
    		tabsPanel.setBorder(outline);
    		JButton switchCards = new JButton("Switch Card");
    		switchCards.setActionCommand("Switch Card");
    		switchCards.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent event) {
    				cards.next(cardPanel);
    				
    			}
    		});
    		tabsPanel.add(switchCards);
    
    		guiFrame.add(tabsPanel, BorderLayout.NORTH);// add tabs panel at top
    
    		cards = new CardLayout();
    		cardPanel = new JPanel();
    		cardPanel.setLayout(cards);
    		cards.show(cardPanel, "Fruits");
    		final BufferedImage img = ImageIO.read(new File(
    				"C:/dq5-orc-king.jpg"));
    		final BufferedImage img2 = ImageIO.read(new File(
    				"C:/drak slime.png"));
    		
    		final BufferedImage img3 = ImageIO.read(new File(
    				"C:/king slime.jpg"));
    		JPanel firstCard = null;
    		JButton button = new JButton();
    		//firstCard.add(button);
    		//ImageIcon slimeIcon = new ImageIcon("C:/Users/Denys/workspace/HelloWorld/Dragon quest  game/slime.png");
    		//button.setIcon();
    		
    		firstCard = new JPanel() {
    				
    			@Override
    			public void paint(Graphics g) {
    				 Graphics2D g2d = (Graphics2D) g;
    				    int x = (this.getWidth() - img2.getWidth(null)) / 2;// get width of image and then utalize window frame 
    				    int y = (this.getHeight() - img2.getHeight(null)) / 2;// get height
    				g.drawImage(img2, x, y, null);
    				
    				 repaint();
    				 
    			}
    		};
    		JPanel thirdCard = null;
    		thirdCard = new JPanel() {
    
    			@Override
    			public void paint(Graphics g) {
    				 Graphics2D g2d = (Graphics2D) g;
    				    int x = (this.getWidth() - img3.getWidth(null)) / 2;
    				    int y = (this.getHeight() - img3.getHeight(null)) / 2;
    				g.drawImage(img3, x, y, null);
    				 repaint();
    				 
    			}
    		};
    		
    		JPanel secondCard = null;
    		secondCard = new JPanel() {
    
    			@Override
    			public void paint(Graphics g) {
    				g.drawImage(img, 0, 0, null);
    				 Graphics2D g2d = (Graphics2D) g;
    				    int x = (this.getWidth() - img.getWidth(null)) / 2;
    				    int y = (this.getHeight() - img.getHeight(null)) / 2;
    				
    				repaint();
    
    			}
    
    		};
    
    		cardPanel.add(firstCard);
    		cardPanel.add(secondCard);
    		cardPanel.add(thirdCard);
    
    		guiFrame.add(tabsPanel, BorderLayout.NORTH);//at top
    		guiFrame.add(cardPanel, BorderLayout.CENTER);// add to frame in center
    		guiFrame.setVisible(true);
    	}
    
    	// All the buttons are following the same pattern
    	// so create them all in one place.
    
    }
    Whats wrong with the buttons?

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

    Default Re: Card Layout button glitch

    Quote Originally Posted by dd2308 View Post
    Hi,
    I reacently tried using card layout, and i got it to work ,but when i try to add buttons i get these errors;
    ...
    What errors are you getting?

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

    Default Re: Card Layout button glitch

    Also, your JPanel's should draw in a paintComponent method override, not a paint method override, and more importantly never ever call repaint() from within a paint or paintComponent method.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,413
    Rep Power
    20

    Default Re: Card Layout button glitch

    Quote Originally Posted by dd2308 View Post
    Hi,
    I reacently tried using card layout, and i got it to work ,but when i try to add buttons i get these errors;
    What errors?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: Card Layout button glitch

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at CardLayoutExample.<init>(CardLayoutExample.java:87 )
    at CardLayoutExample$1.run(CardLayoutExample.java:34)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

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

    Default Re: Card Layout button glitch

    Quote Originally Posted by Fubarable View Post
    ...more importantly never ever call repaint() from within a paint or paintComponent method.
    How would i call it then

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

    Default Re: Card Layout button glitch

    Quote Originally Posted by dd2308 View Post
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at CardLayoutExample.<init>(CardLayoutExample.java:87 )
    at CardLayoutExample$1.run(CardLayoutExample.java:34)
    Please show us which lines are 87 and 34.


    How would I call it then
    It depends on what you're trying to do. But calling it from inside of these methods risk a bad recursion.

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

    Default Re: Card Layout button glitch

    Let me guess, one of the lines is this one:
    Java Code:
    cards.show(cardPanel, "Fruits");
    You seem to be calling this before you've added any components to the cardPanel. Not only that, you don't appear to be using String literals much less one refering to fruits" when you do in fact get around to adding components to the cardPanel.
    dd2308 likes this.

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

    Default Re: Card Layout button glitch

    My errors are gone now that i put the show command at the end. Thank you all.

Similar Threads

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