Results 1 to 7 of 7
  1. #1
    mDennis10 is offline Member
    Join Date
    Jul 2011
    Posts
    76
    Rep Power
    0

    Default some swing components are not displaying

    I have been having this problem for months now, whenever i write by programs and run them some of the components displays while others doesn't. if i close the program and run them again then everything displays or doesn't and i have to do this over and over again everytime i want the program to display properly. I normally break up different parts of the GUI into functions and then call these functions back in the class constructor however for certain functions all the components display while others none does until i close and run over and over again until they does e.g

    Java Code:
    public class LogIn extends JFrame implements ActionListener{
        
        private JTextField userNameTf;
        private JPasswordField passwordTf;
        private JButton logInBtn;
      
        //class constructor
        public LogIn ()
        {
            setTitle ("Log In");
            setSize (430,350);
            setVisible (true);
            setResizable (false);
            setLocation (200,150);
            setLayout (new FlowLayout ());
            setDefaultCloseOperation (EXIT_ON_CLOSE);
            
            setMenuBar ();
            LogInPanel ();
            setLogo ();
        }//end constructor
        
        
        //adds menu bar 
        final public void setMenuBar ()
        {
            JMenuBar mBar = new JMenuBar ();
            JMenu file = new JMenu ("File");
           JMenu help = new JMenu ("help");
            JMenuItem exit = new JMenuItem ("Exit");
            
            mBar.add(file);
            mBar.add(help);
            file.add(exit);
            setJMenuBar (mBar);
            
            //add ActionListeners
            exit.addActionListener (this);
        }//end setMenuBar
        
        //creates JPanel that contain login components
       final public void LogInPanel ()
        {
            //initialize instance variables
            userNameTf = new JTextField (15);
            passwordTf = new JPasswordField (15);
            logInBtn =  new JButton ("Log In");
            
            //create JLabels 
            JLabel userNameLb = new JLabel ("User Name: ");
            JLabel passwordLb = new JLabel ("Password: ");
            
            
            //create JPanel and set properties
            JPanel panel = new JPanel (new GridBagLayout ());
            panel.setPreferredSize (new Dimension (270,180));
            panel.setBorder (BorderFactory.createTitledBorder ("Log In"));
            
            GridBagConstraints c = new GridBagConstraints ();
            c.insets = new Insets (5,5,5,5);
            
            //userNameLb constraints
            c.gridx = 0;
            c.gridy = 0;
            panel.add(userNameLb,c);
            
            
            //userNameTf constraints
            c.gridx = 1;
            c.gridy = 0;
            panel.add(userNameTf,c);
            
            
            //passwordLb constraints
            c.gridx = 0;
            c.gridy = 1;
            panel.add(passwordLb,c);
            
            
            //passwordTf constraints
            c.gridx = 1;
            c.gridy = 1;
            panel.add(passwordTf,c);
            
            
            //logInBtn constraints
            c.gridx = 1;
            c.gridy = 2;
            panel.add(logInBtn,c);
            
            
            add(panel);
            
            //add ActionListeners
            userNameTf.addActionListener (this);
            passwordTf.addActionListener (this);
            logInBtn.addActionListener (this);
           
        }//end logInPanel method
       
       
       //sets application logo
        final public void setLogo ()
       {
           ImageIcon icon = new ImageIcon ("ncu_logo_2.png");
           JLabel lb = new JLabel (icon);
           add(lb);
       }//end setLogo method
        
        
        //Handles action Events
        public void actionPerformed (ActionEvent e)
        {
            //handles exit menuItem click event
            if  (e.getActionCommand ().equals ("Exit"))
            {
                dispose ();
            }//end exit menu item click event
            
        }//end actionPerformed method
        
    }//end LogIn class

  2. #2
    mDennis10 is offline Member
    Join Date
    Jul 2011
    Posts
    76
    Rep Power
    0

    Default Re: some swing components are not displaying

    Sometimes it displays this
    some swing components are not displaying-untitled1.jpg

    Other times it displays this
    some swing components are not displaying-untitled.jpg

    and I haven't changed anything in the code
    Last edited by mDennis10; 01-26-2012 at 05:01 PM.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: some swing components are not displaying

    You're setting the JFrame visible before adding everything to it, and I don't see a pack() call either.

  4. #4
    mDennis10 is offline Member
    Join Date
    Jul 2011
    Posts
    76
    Rep Power
    0

    Default Re: some swing components are not displaying

    when i called setVisible method after i call the setLogo method everything works correctly but why didn't it work the same way when i did it the first way.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: some swing components are not displaying

    Because the JFrame does not redraw whenever you add a component.
    It redraws when it's asked to (and setVisible is one of those 'asks').
    If you don't pack() before the setVisible you'll probably start to have layout problems. That's the method that sorts all the sizing out.

    I am curious why it appears to have randomly picked elements to make visible. I'd have thought none would have been, but it's possible setVisible is being done on a different thread. Ah! Of course, I bet the above code is not being run on the EDT.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default Re: some swing components are not displaying

    I am curious why it appears to have randomly picked elements to make visible. I'd have thought none would have been, but it's possible setVisible is being done on a different thread. Ah! Of course, I bet the above code is not being run on the EDT.
    It's not, but I'd also speculate that mouseovers triggered a repaint of the components that are visible.

    Moving to the correct section of the forums.

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

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: some swing components are not displaying

    Oh right.
    That probably makes more sense...:)

Similar Threads

  1. Components not displaying on JFrame
    By mDennis10 in forum New To Java
    Replies: 5
    Last Post: 07-27-2011, 07:48 PM
  2. JLayeredPane not displaying components
    By abc in forum AWT / Swing
    Replies: 3
    Last Post: 01-06-2011, 03:40 PM
  3. Getting and Displaying Swing Components From DataBase
    By vivekkumar in forum AWT / Swing
    Replies: 1
    Last Post: 07-27-2010, 03:32 PM
  4. Displaying Chinese characters on Swing components
    By vaskarbasak in forum AWT / Swing
    Replies: 3
    Last Post: 06-26-2008, 08:27 AM
  5. HTML on Swing Components
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-27-2007, 09:51 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
  •