Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: GUI Only Works Sometimes

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default GUI Only Works Sometimes

    I'm relatively new to programming, and I'm trying to make a gui with buttons that change panel visibility when clicked. I haven't really done anything yet, but I set one button so far (buttontoNA) to change panel visibility when clicked (make panelmain, the one the button is in, invisible and panelNA visible. The button works, except for some reason, most of the time the frame is empty when i run the gui. When the panels, etc do appear, the button that is set to change visibility does work, but the problem is that the panels don't appear most of the time when i run the code. This is completely random. What is wrong?

     
    //first class
    import javax.*;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
     
    public class guitest{
     
    	public static void main (String[] args){
     
     
    		countryinfoframe myframe = new countryinfoframe();		
    }
     
    public static void actionPerformed (){
     
    }
     
    //2nd class
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
     
    import javax.*;
    import java.awt.*;
    import java.awt.event.*;
    //import javax.swing.*;
    //import java.awt.event.ActionEvent;
    //import java.awt.event.ActionListener;
    //import java.awt.event.MouseEvent;
    //import java.awt.event.WindowAdapter;
    //import java.awt.event.WindowEvent;
     
    public class countryinfoframe implements MouseListener{
     
    	JPanel panelNA;
    	JPanel panelmain;
     
    	public countryinfoframe(){
    	JFrame framemain = new JFrame("Country Information");
    		framemain.setVisible(true);
    		framemain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		framemain.setSize(500,500);
     
    		panelmain = new JPanel();
    			framemain.add(panelmain);
    			panelmain.setVisible(true);
     
    			JButton buttontoNA = new JButton("North America");
    				panelmain.add(buttontoNA);
    			JButton buttontoSA = new JButton("South America");
    				panelmain.add(buttontoSA);
    			JButton buttontoE = new JButton("Europe");
    				panelmain.add(buttontoE);
    			JButton buttontoA = new JButton("Asia");
    				panelmain.add(buttontoA);
    			JButton buttontoAF = new JButton("Africa");
    				panelmain.add(buttontoAF);
    			JButton buttontoAU = new JButton("Australia");
    				panelmain.add(buttontoAU);
     
    				buttontoNA.addMouseListener(this);
     
    			panelNA = new JPanel();
    			framemain.add(panelNA);
    			panelNA.setVisible(false);
     
    			JLabel labelNA = new JLabel("North America");
    				panelNA.add(labelNA);
    			JButton buttontoNAC1 = new JButton("NAC1");
    				panelNA.add(buttontoNAC1);
    			JButton buttontoNAC2 = new JButton("NAC2");
    				panelNA.add(buttontoNAC2);
    			JButton buttontoNAC3 = new JButton("NAC3");
    				panelNA.add(buttontoNAC3);
    			JButton buttonfromNAtomain = new JButton("Back");
    				panelNA.add(buttonfromNAtomain);
     
     
    	}//end constructor
     
    	@Override
    	public void mouseClicked(MouseEvent e) {
    		panelmain.setVisible(false);
    		panelNA.setVisible(true);
    		//System.out.println(e);
    	}
     
    	@Override
    	public void mouseEntered(MouseEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void mouseExited(MouseEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void mousePressed(MouseEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void mouseReleased(MouseEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
     
     
    }	
     
     
     
     
    }

    BTW I do realize that there is a lot that is unnecessary.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,963
    Thanks
    56
    Thanked 2,355 Times in 2,326 Posts

    Default Re: GUI Only Works Sometimes

    Couple of comments.
    build the GUI first and then display it. Add full panels to the frame vs empty ones.
    What layout manager are you using?
    What location do you want the panels to appear on the frame?

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: GUI Only Works Sometimes

    Quote Originally Posted by Norm View Post
    Couple of comments.
    build the GUI first and then display it. Add full panels to the frame vs empty ones.
    What layout manager are you using?
    What location do you want the panels to appear on the frame?
    I am using Eclipse, want the panels topleft except I only want one visible at a time (which is what im trying to do with the buttons), and I dont know what you mean by empty vs full panels.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,963
    Thanks
    56
    Thanked 2,355 Times in 2,326 Posts

    Default Re: GUI Only Works Sometimes

    empty vs full panels
    Panel pnl = new panel(); // create a panel. It is now empty
    pnl.add(someComp1);
    pnl.add(someComp2);
    ...
    frame. add(pnl); // add the panel after it has been filled with some components

    What layout manager are you using? Where do you expect the components to be positioned when they are added to the frame? The default positions can be the new one on top of the previous.

  5. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: GUI Only Works Sometimes

    Quote Originally Posted by Norm View Post
    Panel pnl = new panel(); // create a panel. It is now empty
    pnl.add(someComp1);
    pnl.add(someComp2);
    ...
    frame. add(pnl); // add the panel after it has been filled with some components

    What layout manager are you using? Where do you expect the components to be positioned when they are added to the frame? The default positions can be the new one on top of the previous.
    The only program I am using is Eclipse, should I be using something else too? Also, I thought that I put buttons in both of the panels.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,963
    Thanks
    56
    Thanked 2,355 Times in 2,326 Posts

    Default Re: GUI Only Works Sometimes

    Eclipse is an IDE. A tool to help write code.
    What I'm talking about is what goes in the code, not the tools used to create it.
    If you don't know what a layout manager, you should read up on them.
    Go to this site and Search for layout for much info:
    The Really Big Index

  7. #7
    Junior Member
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: GUI Only Works Sometimes

    Quote Originally Posted by Norm View Post
    Eclipse is an IDE. A tool to help write code.
    What I'm talking about is what goes in the code, not the tools used to create it.
    If you don't know what a layout manager, you should read up on them.
    Go to this site and Search for layout for much info:
    The Really Big Index
    I was going to use gridbag layout but I hadnt gotten that far yet. I was just wondering why sometimes the panels show in the frame and sometimes they dont.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,963
    Thanks
    56
    Thanked 2,355 Times in 2,326 Posts

    Default Re: GUI Only Works Sometimes

    Where are the components being displayed when you add them to the frame?
    Can they overlap/hide one another? Do something to each panel so you can see if it is the one being shown.

    Set the back ground color for all the panels and the frame and content pane to different colors to see which one is showing when.
    Last edited by Norm; July 12th, 2011 at 09:14 AM.

Similar Threads

  1. jar file works on XP but not on Linux
    By cl2606 in forum Java Theory & Questions
    Replies: 1
    Last Post: June 10th, 2011, 10:19 AM
  2. This program works but Want to improve
    By SHStudent21 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 8th, 2011, 05:53 PM
  3. [SOLVED] Can someone verify if this code for deleting a BST works?
    By scottb80 in forum Java Theory & Questions
    Replies: 2
    Last Post: November 2nd, 2010, 11:19 AM
  4. How overloaded paint() works?
    By maikeru in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 21st, 2009, 06:13 PM
  5. How database connection pooling works in a application
    By JayVirk in forum JDBC & Databases
    Replies: 0
    Last Post: October 10th, 2009, 08:14 AM