Results 1 to 14 of 14

Thread: Empty Form?

  1. #1
    Vorfin's Avatar
    Vorfin is offline Member
    Join Date
    Jul 2010
    Location
    Near London.
    Posts
    7
    Rep Power
    0

    Default Empty Form?

    I'm used to developing in NetBeans IDE, with their Swing GUI Builder. However due to a resent computer failure, I'm stuck using Eclipse (As NetBeans Will Not Run Well On My Older Laptop)

    After trying out some GUI Building Plugins, and finding them all relatively poor, I've decided to try my hand at doing the GUIs manually. However I have a problem.

    About 1/10 times I run the program that has a GUI, either in Eclipse, or as a .jar. I get nothing but a Form with the right settings, but no controls on it. Although this is probably bad, it was little more than an annoyance.

    However, as I tried more things on the GUI, the problem seemed to escalate exponentially. When using an Icon on the Form, or a JTextField, the Form never, ever loads. It just comes up, 300, 300, Resizing Disabled, the correct title, all as set. But the controls do not appear.

    Here is an my code:

    Works 9/10 Times:
    Java Code:
    package HelloWorld;
    
    import java.awt.Color;
    import javax.swing.*;
    
    
    public class GUI {	
    	
    	public static void main(String[] args) {			
    		GUI p = new GUI();
    		p.Create();		
    	}			
    	
    	public void Create() {
    		
    		JFrame Frame = new JFrame();
    		Frame.setVisible(true);
    		Frame.setSize(300, 300);
    		Frame.setResizable(false);
    		Frame.setTitle("Hello GUI!");	
    		Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	
    		JPanel Background = new JPanel(null);
    		Background.setBackground(Color.CYAN);
    		
    		JButton Button1 = new JButton();
    		Button1.setText("Click Me!");
    		Button1.setLocation(10,110);
    		Button1.setSize(Button1.getPreferredSize());
    		Background.add(Button1);
    		
    		JButton Button2 = new JButton();
    		Button2.setText("Me Too!");
    		Button2.setLocation(10, 150);
    		Button2.setSize(Button1.getPreferredSize());
    		Background.add(Button2);									
    		
    		//ImageIcon Icon = new ImageIcon(getClass().getResource("/Images/Java.png"));		
    		JLabel Label1 = new JLabel();			
    		Label1.setText("Hello");
    		//Label1.setIcon(Icon);
    		Label1.setHorizontalAlignment(SwingConstants.CENTER);		
    		Label1.setLocation(1, 1);
    		Label1.setSize(300, 100);
    		Background.add(Label1);
    				
    		Frame.add(Background);					
    	}
    }
    Doesn't Work:
    Java Code:
    package HelloWorld;
    
    import java.awt.Color;
    import javax.swing.*;
    
    
    public class GUI {	
    	
    	public static void main(String[] args) {			
    		GUI p = new GUI();
    		p.Create();		
    	}			
    	
    	public void Create() {
    		
    		JFrame Frame = new JFrame();
    		Frame.setVisible(true);
    		Frame.setSize(300, 300);
    		Frame.setResizable(false);
    		Frame.setTitle("Hello GUI!");	
    		Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	
    		JPanel Background = new JPanel(null);
    		Background.setBackground(Color.CYAN);
    		
    		JButton Button1 = new JButton();
    		Button1.setText("Click Me!");
    		Button1.setLocation(10,110);
    		Button1.setSize(Button1.getPreferredSize());
    		Background.add(Button1);
    		
    		JButton Button2 = new JButton();
    		Button2.setText("Me Too!");
    		Button2.setLocation(10, 150);
    		Button2.setSize(Button1.getPreferredSize());
    		Background.add(Button2);									
    		
    		ImageIcon Icon = new ImageIcon(getClass().getResource("/Images/Java.png"));		
    		JLabel Label1 = new JLabel();			
    		//Label1.setText("Hello");
    		Label1.setIcon(Icon);
    		Label1.setHorizontalAlignment(SwingConstants.CENTER);		
    		Label1.setLocation(1, 1);
    		Label1.setSize(300, 100);
    		Background.add(Label1);
    				
    		Frame.add(Background);					
    	}
    }

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

    Default

    One issue: call setVisible(true) after adding all your components and packing your JFrame.
    Issue 2: don't use null layout and absolute positioning but instead read the tutorials on how to use the layout managers.

  3. #3
    Vorfin's Avatar
    Vorfin is offline Member
    Join Date
    Jul 2010
    Location
    Near London.
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    One issue: call setVisible(true) after adding all your components and packing your JFrame.
    Issue 2: don't use null layout and absolute positioning but instead read the tutorials on how to use the layout managers.
    1) Thanks for the tip.
    2) I know how to use Layout Managers, in this scenario I was just playing with ".setLocation(x,y);"

    Edit: setVisable after adding them all worked amazingly. Thanks.

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

    Default

    But setLocation won't do squat in this situation since JPanels use FlowLayout by default and will ignore all setLocation calls.

  5. #5
    Vorfin's Avatar
    Vorfin is offline Member
    Join Date
    Jul 2010
    Location
    Near London.
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    But setLocation won't do squat in this situation since JPanels use FlowLayout by default and will ignore all setLocation calls.
    It seems to be working exactly how I want it. Button2 is right bellow Button1, and if I change the X/Y Values I can move them anywhere on the Form/Panel.
    Last edited by Vorfin; 07-04-2010 at 08:28 PM.

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    It seems to be working exactly how I want it.
    "Seems to work" is the key phrase. Yes you see components randomly positioned on the GUI but the layout is terrible. When you look at real GUI's the components are all placed with proper "white space" between components. Also the size of the frame is calculated based on the placement of components on the frame. Layout Managers to this for you automatically. Whereas your frame is way too large and the space between components is random.

    Also, while you are at it, learn to use proper Java naming conventions. Any text book or tutorial I've ever seen follows these conventions so don't make up your own. For example variable names should NOT start with an upper cased character.

  7. #7
    Vorfin's Avatar
    Vorfin is offline Member
    Join Date
    Jul 2010
    Location
    Near London.
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    proper "white space" between components.
    Has always been something I am not fond of, by taking the Layout Manager out, I can position things however I like, in a more eccentric and original fashion.

    As for the naming conventions, I'm an entirely self taught, hobby programmer who does it for fun, not work nor school. I mealy type how I feel comfortable, and TBH upper case variables look a lot neater, at least from my perspective. Imagine how boring the world would be if everyone did the exact same thing.
    Last edited by Vorfin; 07-04-2010 at 09:12 PM.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    Imagine how boring the world would be if everyone did the exact same thing.
    Especially driving in heavy traffic.

  9. #9
    Vorfin's Avatar
    Vorfin is offline Member
    Join Date
    Jul 2010
    Location
    Near London.
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Especially driving in heavy traffic.
    Exactly, can you imagine the news if there was never any pile ups? Who would watch it?

  10. #10
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Good luck with future questions. If you are not interested in learning proper coding then you are on your own. Standards and conventions exist for a reason. It helps prevent silly mistakes and makes reading, developing and maintaing code easier.

    Just like this silly mistake you made here. I've never seen any examples coded this way in any tutorial or text book. Because you did not follow conventions and tried to invent your own you wasted not only your time, but all the others who attempted to help as well.

  11. #11
    Vorfin's Avatar
    Vorfin is offline Member
    Join Date
    Jul 2010
    Location
    Near London.
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    Good luck with future questions. If you are not interested in learning proper coding then you are on your own. Standards and conventions exist for a reason. It helps prevent silly mistakes and makes reading, developing and maintaing code easier.

    Just like this silly mistake you made here. I've never seen any examples coded this way in any tutorial or text book. Because you did not follow conventions and tried to invent your own you wasted not only your time, but all the others who attempted to help as well.
    How was this error caused here caused by my typing style? If fact, I followed a tutorial that used correct typing styles to learn how Swing worked. It doesn't matter if I used frame or Frame, that was not the problem.

    The fact is, the tutorial I followed said to use .setVisible(true); in with the rest of the main Form code, not at the end. Fubarable answered my question in one single post, for which I am thankful. You however seem to attack me not not conforming to "standards"? Which only came around because one day back in the 70s some guy said "Hey guys, lets all use lower case variables =D".

    I suggest you vent your anger at the author of the tutorial I followed, which gave me the wrong information. I'm sure you will find it fun. :D

    YouTube - [HD] SWING/GUI in Java TUTORIAL/HELP NO visual editor- Create JFrame + JPanel + objects

  12. #12
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    It doesn't matter if I used frame or Frame, that was not the problem.
    It didn't say it was the problem in this case. I said it might cause problems/confusions in the future. For example do you know what a "static" method is? Do you know how to invoke a static method. Well, we do, and when we attempt to read your code it is confusing because it looks like your whole program is using static methods.

    I suggest you vent your anger at the author of the tutorial
    My frustration is at your apparent unwillingness to listen to simple suggestions like "using layout managers" or "using proper naming conventions". You may not understand the reasons now but will in the future.

    which gave me the wrong information.
    I agree, the tutorial is wong in many cases. One more suggestion (which I'm sure you will ignore) is do dump the tutorial and learn from Trail: Creating a GUI With JFC/Swing: Table of Contents (The Java™ Tutorials).

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    Another example where standardization is important.
    Have you ever tried working on a car or motorcycle where there are metric, American and English standard bolts?
    Standardization in programming means I can look at something and immediately recognize what it is and how it will be used. That can save lots of time looking back and forth trying to understand what and how from the code.

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

    Default

    Quote Originally Posted by Vorfin View Post
    How was this error caused here caused by my typing style? If fact, I followed a tutorial that used correct typing styles to learn how Swing worked. It doesn't matter if I used frame or Frame, that was not the problem.
    The problem is when you come here or to other forums asking for free help. If you do so, it is in your best interest to make it as easy as possible for those helping you to be able to do so, and by coding within the conventions you do this. Otherwise we have to struggle to understand your code which will either delay your getting help or cause you to get no answers or the wrong answers.

    If you're paying for help though, by all means, use whatever standard you like. Up to you of course.

Similar Threads

  1. Replies: 1
    Last Post: 05-27-2010, 09:48 PM
  2. Replies: 1
    Last Post: 03-22-2010, 09:03 AM
  3. Replies: 0
    Last Post: 10-31-2009, 09:24 AM
  4. Replies: 10
    Last Post: 08-12-2009, 04:02 PM
  5. Replies: 6
    Last Post: 11-28-2008, 06:55 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
  •