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 4 of 4

Thread: Why am I getting an empty window here

  1. #1
    Junior Member zlloyd1's Avatar
    Join Date
    Nov 2012
    Location
    Norfolk, Virginia
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Why am I getting an empty window here

    I am creating a program in Java to calculate various retail figures having to do with sale prices in different departments, and for some reason when I run my program I am getting an empty window that opens with nothing in it?? I know I have added panels, buttons, text fields, and a number of other elements that should be appearing inside of my window, but all I am getting is nothing, just a blank, empty window??
    Anyway, here is my code, which compiles and runs just fine, with no errors:
    import java.awt.*;
    import javax.swing.*;
    public class RetailCalculator extends JFrame {
         public JLabel itemMessageLabel; //to display item message
          public JPanel panel;
          public JTextField itemTextField;
          public JLabel departmentMessageLabel;
          public JLabel itemOriginalLabel;
          public JTextField itemOriginal;
          public JLabel percentOffLabel;
          public JTextField percentOff;
          public ButtonGroup bg; 
         final int WINDOW_WIDTH = 1200, //WINDOW WIDTH IN PIXELS
                      WINDOW_HEIGHT = 1000;     
           /*Constructor*/      
         public RetailCalculator()
         {
            super("Retail Calculator");            
       //CREATE window  
              //create window with title
              JFrame window = new JFrame ("Retail Calculator");         
              //set size of window
               window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);          
                  //what happens when x or close is clicked
               window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
               //display the window
               buildPanel();
               pack();
               window.setVisible(true);
               panel.setVisible(true);}
       //create label text field and buttons
        private void buildPanel()           
        { //create custom panels
                  JPanel panel = new JPanel();
           panel.setLayout(new BorderLayout());            
               itemMessageLabel = new JLabel ("Enter your first item number: ");
         //text field for item name
               itemTextField = new JTextField(8);
        //select department from menu (combo, radio or menu)       
        //display message to choose department
            departmentMessageLabel = new JLabel ("Choose your department: ");
        //create 5 departments with radio buttons
            JRadioButton department1 = new JRadioButton("Women's");
            JRadioButton department2 = new JRadioButton("Men's");
            JRadioButton department3 = new JRadioButton("Children's");
            JRadioButton department4 = new JRadioButton("Shoes");
            JRadioButton department5 = new JRadioButton("Toys");
         bg = new ButtonGroup();
         bg.add(department1);
         bg.add(department2);
         bg.add(department3);
         bg.add(department4);
         bg.add(department5);           
       //text field for original price
            itemOriginalLabel = new JLabel ("Enter the items original price: ");
            itemOriginal = new JTextField(); 
        //text field for percent discount
            percentOffLabel = new JLabel ("Enter the percent off for sale price: ");
            percentOff = new JTextField(2);
        //add item button
            JButton button1 = new JButton ("Add Item");  
            //calculate button computes and displays      
            JButton button2 = new JButton ("Compute Sale Price");   
       //create and add objects to panel        
               //add the item data to the panel
              panel.add(itemMessageLabel);
              panel.add(itemTextField);         
            //add the departments to the panel       
            //add original info to panel
            panel.add(itemOriginalLabel);
            panel.add(itemOriginal);       
            panel.add(percentOffLabel);
            panel.add(percentOff);       
            //add buttons
            panel.add(button1);
            panel.add(button2);}  
           /* private inner class to handle event of buttons and radio fields */
     
    public static void main(String[] args)
    { 
    new RetailCalculator();}}
    but when I run this it opens a window with nothing in it, and I cannot understand this....
    I did panel.setVisible(true);, and I thought that would cause the panel with all of the buttons to appear when run, but I am getting nothing here, Please Help!!


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Why am I getting an empty window here

    Your code is very confused and confusing including use of more than one JFrame (the window variable and the RetailCalculator current object itself) -- which JFrame is the one that is supposed to be displayed? You also add a lot of components to a JPanel in your buildPanel() method but never add that JPanel to a displayed JFrame. Calling setVisible(true) on a component will not display the component if it hasn't been added to a top-level window, such as a JFrame, that is visualized.

    I suggest that you 1) don't have your class extend JFrame but rather use the window variable and build your GUI with that JFrame, 2) start small with a small simple program, that you test and try to display and know that it will display before adding more components, 3) test the program frequently to know that it is still displaying what you think it should display 4) not add new components until the current code tests well.

  3. #3
    Junior Member zlloyd1's Avatar
    Join Date
    Nov 2012
    Location
    Norfolk, Virginia
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Re: Why am I getting an empty window here

    Quote Originally Posted by curmudgeon View Post
    Your code is very confused and confusing including use of more than one JFrame (the window variable and the RetailCalculator current object itself) -- which JFrame is the one that is supposed to be displayed? You also add a lot of components to a JPanel in your buildPanel() method but never add that JPanel to a displayed JFrame. Calling setVisible(true) on a component will not display the component if it hasn't been added to a top-level window, such as a JFrame, that is visualized.
    Out of curiosity, although I can hear what you are saying here, could you answer a different question for me, that just came to me after reading your response here.
    How could I add the JPanel to a displayed JFrame??
    Also, I thought that the line,
    window.setVisible(true);
    inside of public RetailCalculator, would have created a displayed JFrame.... Please explain why it does not, if possible!!

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Why am I getting an empty window here

    Quote Originally Posted by zlloyd1 View Post
    Out of curiosity, although I can hear what you are saying here, could you answer a different question for me, that just came to me after reading your response here.
    How could I add the JPanel to a displayed JFrame??
    You must add your components to some component hierarchy that is eventually held by a JFrame or other visualized top-level window. Simple as that. You're creating JPanels adding components to them, and either adding them to a non-visualized JFrame (the "this" in your code above) or not adding them to anything at all.

    Also, I thought that the line,
    window.setVisible(true);
    inside of public RetailCalculator, would have created a displayed JFrame.... Please explain why it does not, if possible!!
    It should create a visualized JFrame but I don't see where you've added anything to the JFrame that is referenced by the window variable. I've not run your code, but on inspection it appears that this would do nothing but display an empty JFrame.

    Again, please realize that you have two JFrames that you're working with here, and you're displaying one of them, but haven't added any components to this visualized JFrame.

    Have you gone through some of the Swing tutorials yet on this? If not, I highly recommend them as they will help you immensely.

Similar Threads

  1. My spinner is empty
    By VagosM in forum Android Development
    Replies: 0
    Last Post: November 28th, 2012, 05:28 PM
  2. Replies: 2
    Last Post: August 27th, 2012, 09:19 PM
  3. Empty If Statement
    By lightOfDay in forum Loops & Control Statements
    Replies: 5
    Last Post: June 14th, 2012, 08:41 AM
  4. Replies: 1
    Last Post: December 17th, 2011, 03:32 AM
  5. Craating non active window ,and inputing to the non active window.
    By java-beginner in forum Java Theory & Questions
    Replies: 0
    Last Post: February 25th, 2011, 10:13 PM

Tags for this Thread