Results 1 to 7 of 7
- 01-26-2012, 04:47 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 76
- Rep Power
- 0
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
- 01-26-2012, 04:58 PM #2
Member
- Join Date
- Jul 2011
- Posts
- 76
- Rep Power
- 0
- 01-26-2012, 05:02 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 01-26-2012, 05:07 PM #4
Member
- Join Date
- Jul 2011
- Posts
- 76
- Rep Power
- 0
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.
- 01-26-2012, 05:18 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 01-26-2012, 06:52 PM #6
Re: some swing components are not displaying
It's not, but I'd also speculate that mouseovers triggered a repaint of the components that are visible.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.
Moving to the correct section of the forums.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 01-27-2012, 09:26 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Similar Threads
-
Components not displaying on JFrame
By mDennis10 in forum New To JavaReplies: 5Last Post: 07-27-2011, 07:48 PM -
JLayeredPane not displaying components
By abc in forum AWT / SwingReplies: 3Last Post: 01-06-2011, 03:40 PM -
Getting and Displaying Swing Components From DataBase
By vivekkumar in forum AWT / SwingReplies: 1Last Post: 07-27-2010, 03:32 PM -
Displaying Chinese characters on Swing components
By vaskarbasak in forum AWT / SwingReplies: 3Last Post: 06-26-2008, 08:27 AM -
HTML on Swing Components
By Java Tip in forum Java TipReplies: 0Last Post: 11-27-2007, 09:51 AM


LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks