Page 1 of 2 12 LastLast
Results 1 to 20 of 21
Like Tree3Likes

Thread: Java GUI Problem

  1. #1
    mbschultz97 is offline Super OP Noob
    Join Date
    May 2014
    Location
    Virginia
    Posts
    65
    Rep Power
    0

    Default Java GUI Problem

    When i run the program it doesn't show all the text fields and labels and stuff but when i click btnTest_1 and go back to btnTest everything appears...
    Java Code:
    import javax.swing.*;
    
    
    public class Frame extends JFrame{
    	private JTextField textField;
    	private JTextField textField_1;
    	
    	public Frame(){
    		setTitle("Multifunctual Calculator");
    		setVisible(true);
    		setSize(600,400);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		
    		getContentPane().setLayout(null);
    		
    		JButton btnTest = new JButton("Button 1");
    		btnTest.setBounds(10, 11, 89, 23);
    		getContentPane().add(btnTest);
    		
    		final JPanel panel = new JPanel();
    		panel.setBorder(new LineBorder(new Color(0, 0, 0)));
    		panel.setBounds(10, 41, 564, 298);
    		getContentPane().add(panel);
    		panel.setLayout(null);
    		
    		final JLabel lblTest = new JLabel("test");
    		lblTest.setBounds(196, 9, 89, 14);
    		panel.add(lblTest);
    		
    		textField = new JTextField();
    		textField.setBounds(74, 34, 161, 20);
    		panel.add(textField);
    		textField.setColumns(10);
    		
    		JLabel lblAmount = new JLabel("Amount");
    		lblAmount.setBounds(10, 37, 46, 14);
    		panel.add(lblAmount);
    		
    		textField_1 = new JTextField();
    		textField_1.setBounds(74, 65, 161, 20);
    		panel.add(textField_1);
    		textField_1.setColumns(10);
    		
    		JLabel lblLength = new JLabel("Length");
    		lblLength.setBounds(10, 68, 46, 14);
    		panel.add(lblLength);
    		
    		JButton btnCalculate = new JButton("Calculate");
    		btnCalculate.setBounds(106, 96, 89, 23);
    		panel.add(btnCalculate);
    		
    		JButton btnTest_1 = new JButton("Button 2");
    		btnTest_1.setBounds(109, 11, 89, 23);
    		getContentPane().add(btnTest_1);
    		
    		final JPanel panel_1 = new JPanel();
    		panel_1.setBorder(new LineBorder(new Color(0, 0, 0)));
    		panel_1.setBounds(10, 41, 564, 298);
    		getContentPane().add(panel_1);
    		
    		JLabel lblTest_1 = new JLabel("test2");
    		panel_1.add(lblTest_1);
    		
    		JLabel lblMadeByMatthew = new JLabel("Made by: Matthew Schultz");
    		lblMadeByMatthew.setBounds(402, 347, 196, 14);
    		getContentPane().add(lblMadeByMatthew);
    		
    		btnTest.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    				panel_1.setVisible(false);
    				panel.setVisible(true);
    			}
    		});
    		
    		btnTest_1.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    				panel.setVisible(false);
    				panel_1.setVisible(true);
    			}
    		});
    		
    		btnCalculate.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    				int x = Integer.parseInt(textField.getText());
    				int y = Integer.parseInt(textField_1.getText());
    				int value = x * y;
    				lblTest.setText("it is " + value);
    			}
    		});
    	}
    	
    	public static void main(String args[]){
    		Frame blank = new Frame();
    	}
    }
    thanks for the help!

  2. #2
    mbschultz97 is offline Super OP Noob
    Join Date
    May 2014
    Location
    Virginia
    Posts
    65
    Rep Power
    0

    Default Re: Java GUI Problem

    btw i used a try catch thingy for the btnCalculate function...

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,960
    Rep Power
    8

    Default Re: Java GUI Problem

    Why are you using a null layout?

    Why are you adding components to a JFrame after it's already visible? Usually you should only add components *before* the JFrame is visible, otherwise you get the problem you're encountering.

    If you really need to add components to a JFrame that's already visible, then you have to revalidate and repaint your JFrame after you add them.
    gimbal2 likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Java GUI Problem

    When you add the panel / panel_1, you do not set the Visibility of those. If you want to see one panel after the form is created, then add a setVisible(true) for the panel you want to see.

    Konrad

    Edit: Just ignore my reply. The full truth is of course in the reply of Kevin. Calling revalidate and repaint should be done. The panels will be visible without setting them visible. (But maybe you want to make one of them not visible.)
    Last edited by kneitzel; 05-19-2014 at 04:51 PM.

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Java GUI Problem

    Quote Originally Posted by KevinWorkman View Post
    Why are you using a null layout?.
    Good question. Or in other words, why are you (the OP) using absolute positioning? (i.e. using setBounds()).

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    mbschultz97 is offline Super OP Noob
    Join Date
    May 2014
    Location
    Virginia
    Posts
    65
    Rep Power
    0

    Default Re: Java GUI Problem

    I'm using a plugin in eclipse to make the GUI

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,960
    Rep Power
    8

    Default Re: Java GUI Problem

    For the love of Gosling you should ditch that GUI builder and just write the code yourself. You aren't going to learn a darn thing if you rely on GUI builders.

    (That's just my two cents, but the fact that you don't understand what this code is doing sorta proves my point.)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,001
    Rep Power
    6

    Default Re: Java GUI Problem

    GUI builders are evil. They make you think building GUIs is as simple as dragging and dropping stuff. Yet everyone who uses them ends up asking here and being yelled at for using a GUI builder.

    The only people qualified to use a GUI builder are the ones who know and understand the code which the GUI builder will generate intimately - because at least then you know how to resolve inevitable problems you'll have and be able to manually intervene where the dumb GUI builder messes up. And such people will likely choose to do the programming themselves anyway, because who wants to have generated code in their project?
    KevinWorkman likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java GUI Problem

    Quote Originally Posted by gimbal2 View Post
    who wants to have generated code in their project?
    I, the founding father of the S.A.T.E.E.F.L.A.T.S.A.B.C. (Society Against Those Evil Enhanced For Loops And That Silly Auto Boxing Crapola) couldn't agree more!

    kindest regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,001
    Rep Power
    6

    Default Re: Java GUI Problem

    I'll bite. Yes autoboxing is evil, but what the heck is wrong with enhanced for loops? I was getting -so- tired of having to manually use an Iterator even for the cases where I don't need its very useful remove() method.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java GUI Problem

    Quote Originally Posted by gimbal2 View Post
    I'll bite. Yes autoboxing is evil, but what the heck is wrong with enhanced for loops? I was getting -so- tired of having to manually use an Iterator even for the cases where I don't need its very useful remove() method.
    Those 'enhanced' for loops also generate code behind the scene for you (the Iterator stuff); b.t.w. you should see what switching on a String does to your code ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,001
    Rep Power
    6

    Default Re: Java GUI Problem

    We've been over that, I don't use switch in any case ;)

    So the loop is only evil because it generates the repetitive boilerplate code? For one second I was going to think you were going to drop the word "performance" there.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java GUI Problem

    Quote Originally Posted by gimbal2 View Post
    We've been over that, I don't use switch in any case ;)

    So the loop is only evil because it generates the repetitive boilerplate code? For one second I was going to think you were going to drop the word "performance" there.
    You were complaining about code being generated behind your back, not me; the compiler bridges the gap between generating byte code and generating Java source code at several places; I only think about performance when I have to implement something on silly little processors (single cores, not much RAM and clock that resembles a calendar), otherwise I let the JIT compiler do the dirty work ;-)

    kind regards,

    Jos (<--- switches are for ints and ints only (all the rest is syntactic make up))
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,001
    Rep Power
    6

    Default Re: Java GUI Problem

    Lies! I was only complaining about generated code as emitted by a GUI builder (which is visible for everyone to see); YOU were the one that made the jump to what the compiler generates "behind your back" mister founding father of the S.A.T.E.E.F.L.A.T.S.A.B.C.

    Shame!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  15. #15
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Java GUI Problem

    I am just wondering:

    Are you creating a wikipedia entry for S.A.T.E.E.F.L.A.T.S.A.B.C.? :)

    Konrad

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java GUI Problem

    Quote Originally Posted by kneitzel View Post
    I am just wondering:

    Are you creating a wikipedia entry for S.A.T.E.E.F.L.A.T.S.A.B.C.? :)
    No, it's a secret society; I said too much already ...

    kind regards,

    Jos (<-- lips sealed)

    Konrad[/QUOTE]
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Java GUI Problem

    Really clever plan!

    You just want me to travel to your location, take to a bar and spend you a lot of beer to unseal your lips ...
    A great plan but it failed ...

    (But if you come to Frankfurt, Germany by accident your plan might be successful in case you tell me in advance :)

    With kind regards,

    Konrad

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java GUI Problem

    Quote Originally Posted by kneitzel View Post
    Really clever plan!

    You just want me to travel to your location, take to a bar and spend you a lot of beer to unseal your lips ...
    A great plan but it failed ...
    I never mentioned bars ... (I read a lot about them, but I've never been to one ... ;-)

    (But if you come to Frankfurt, Germany by accident your plan might be successful in case you tell me in advance :)
    If I ever go to the Mainz university again, I might take a detour and stop halfway (by surprise! ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  19. #19
    mbschultz97 is offline Super OP Noob
    Join Date
    May 2014
    Location
    Virginia
    Posts
    65
    Rep Power
    0

    Default Re: Java GUI Problem

    I knew when I made this thread that there was something wrong with the order that I add the stuff to the panels... I've been trying to move around the code so all of the labels and stuff show when I run it but I'm still missing a couple things... When I run it I don't see btnTest, btnTest_1, lblMadeByMatthewSchultz, textField, and textField_1... If I wave my mouse around where the buttons should be they will appear and if I click btnTest_1 and then btnTest everything will appear but the lblMadeByMatthewSchultz... thanks for the help!

  20. #20
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Java GUI Problem

    Hi,

    the problem exists, because you add items to a frame that is already visible. So what is happening?
    - The frame is visible. So it is painted.
    - When you add another control, no requirement is seen to redraw the frame. (This is something that I didn't see with Java 8 on my linux system. So maybe that is changed now? Or was I just lucky?)

    So there are 2 possible solutions:
    a) Move the setVisible(true); call to the end of the constructor
    b) add
    Java Code:
       revalidate();
       repaint();
    at the end of your constructor.

    That should solve your problem.

    With kind regards,

    Konrad
    mbschultz97 likes this.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 04-19-2014, 02:50 PM
  2. Replies: 3
    Last Post: 09-01-2012, 08:58 PM
  3. Replies: 4
    Last Post: 03-16-2012, 11:29 AM
  4. Small problem with problem with Java, C++ parse program.
    By dragstang86 in forum New To Java
    Replies: 4
    Last Post: 10-30-2011, 03:43 AM
  5. Problem Display Jmenubar Java Se6 u23 versus Java SE6 u22
    By Ravanelly in forum Advanced Java
    Replies: 0
    Last Post: 01-07-2011, 09:36 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
  •