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

Thread: Java Swing Application

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation Java Swing Application

    I made a simple java application for fun which I was planning to add on to and make alot better. I ran into an error with an action listener problem. The main point of the program was for me to click the button and it would show a label. I tried and tried but it never worked. So I decided to test whenever I clicked the button if it could output to the console window instead of displaying something on the gui. Of course it worked and now Im really puzzled on how and why one part is working and one is not.

    Heres my code:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
     
    public class Frame extends JFrame{
     
     
    	static JLabel label;
    	static JButton button;
    	static JFrame frame;
     
    	public static void main(String[] args){
    		frame = new JFrame("Hello World!");
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(500, 400);
    		frame.setLayout(new FlowLayout());
     
    		button = new JButton("Click Me!");
    		frame.add(button);
     
    		label = new JLabel("THIS BETTER WORK!");
     
    		button.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e) {
    				frame.add(label);
    				System.out.println("BUTTON IS PRESSED!");
    			}
    		});
     
     
     
     
    	}
     
    }


  2. #2
    Junior Member
    Join Date
    Nov 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation Java Swing Application

    I made a simple java application for fun which I was planning to add on to and make alot better. I ran into an error with an action listener problem. The main point of the program was for me to click the button and it would show a label. I tried and tried but it never worked. So I decided to test whenever I clicked the button if it could output to the console window instead of displaying something on the gui. Of course it worked and now Im really puzzled on how and why one part is working and one is not.

    Heres my code:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
     
    public class Frame extends JFrame{
     
     
    	static JLabel label;
    	static JButton button;
    	static JFrame frame;
     
    	public static void main(String[] args){
    		frame = new JFrame("Hello World!");
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(500, 400);
    		frame.setLayout(new FlowLayout());
     
    		button = new JButton("Click Me!");
    		frame.add(button);
     
    		label = new JLabel("THIS BETTER WORK!");
     
    		button.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e) {
    				frame.add(label);
    				System.out.println("BUTTON IS PRESSED!");
    			}
    		});
     
     
     
     
    	}
     
    }

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Swing Application

    Some comments on the code:
    You should not use classnames that are used by java SE: Frame
    Why does your class extend JFrame?

    When you change the contents of a container you need to tell the container to do another layout.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Nov 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Swing Application

    Ok what should my class be extending then? And then I would need to put flowlayout in the actionPerformed method?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Swing Application

    The class does not need to extend anything.

    Look at the Container class for some methods that could help.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Nov 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Swing Application

    Ok so I looked at the class, I see alot of methods but im not sure exactly what I should be looking for? Could u give an example of what the code should look like to work?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Swing Application

    You need to call a method that will tell the container to do a new layout of the components it contains.

    Try some of the methods to see if they do it. That's what I had to do to find one that worked.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Nov 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Swing Application

    Ok so the weirdest thing happened. I went ahead and did what you said and found a method that should work:

    frame.add(label, new FlowLayout());

    but it didnt so I went back to this:

    frame.add(label);

    As I was fiddling around with the screen I saw that once I clicked the button and edited the frames dimensions by dragging the screen smaller or bigger, the text showed! Im not sure how or why its doing this though. If I don't click the button and change the screens size it doesn't do anything. Its only if I click the button and drag the screen smaller or bigger that It will show the text! Im not sure the problem now, but I hope you can help. Thanks!

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Swing Application

    Interesting. I've never tried changing the layout manager from a listener. I don't see in the API doc what that version of the add() method is supposed to do when given an instance of a layout manager.

    There are methods that ask the current layout manager to redo the layout of the container. Try some of those.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Nov 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Swing Application

    Well how do most people do it then? I suspect that theres people that when a button is clicked something from the layout is changed right? Theres got to be a way.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Swing Application

    One way is to call a method in the Container class that tells the layout manager in the container to redo the layout of the components it controls.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Nov 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Swing Application

    This is really weird I have a text field that I made and whenever I start the program it doesnt show until I drag the screen bigger or smaller, Its not even being changed inside a listener. I'm just gonna show you the whole code. Right now im just trying everything and experimenting because this is really weird, Im not sure why its happening.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
     
    public class Fun{
     
    	static JLabel label;
    	static JButton button;
    	static JFrame frame;
    	static JPanel panel;
    	static JTextField textField;
     
    	public static void main(String[] args){
    		frame = new JFrame("Hello World!");
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(500, 400);
    		frame.setResizable(true);
    		frame.setLayout(new FlowLayout());
    		frame.setLocationRelativeTo(null);
    		frame.getContentPane().setBackground(Color.CYAN);
     
    		textField = new JTextField("Type Here");
    		frame.add(textField);
     
    		/*button.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e) {
    				frame.add(label);
    				System.out.println("BUTTON IS PRESSED!");
    			}
    		});
    		*/
    	}
     
    }

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Swing Application

    It's better to display (setVisible) the frame AFTER everything has been added to it. If the frame displays its contents before anything is added, when will it redisplay the frame after something has been added?
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    Nov 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Swing Application

    Ok so norm im kind of not sure what your trying to get at here. Im sure your correct in how your thinking it but in my mind im not understanding what your trying to say. In my mind how can the frame display something before something is added to the frame? If you could possibly re-word what you were saying. Thanks.

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Swing Application

    If the frame immediately shows what has been already added to it when setVisible(true) is called, what would it have to display? In the code there is Nothing added before the setVisible call.

    If components are added to the frame after the call to setVisible(true) when will the frame show them?
    If the code is adding many components, it would be nice if the code told the frame it was done adding the components it wants to be shown so the frame could repaint itself to show all the components that were added. If the frame is not told to repaint itself, the components added after the setVisible(true) might not be shown until some event causes the frame to repaint.
    If you don't understand my answer, don't ignore it, ask a question.

  16. The Following User Says Thank You to Norm For This Useful Post:

    vandy22 (November 18th, 2013)

  17. #16
    Junior Member
    Join Date
    Nov 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Swing Application

    Ok yeah I get it now, and It worked. Thanks alot!

    --- Update ---

    Great news! I figured out how to edit a layout in a listener. What I did was called the repaint function in the listener then made it visible to reset the components. I just went ahead and made it so when I clicked a button it deleted the text field. So heres the code and I underlined and bolded the main parts that show what I added and how it helped.

    __________________________________________________ ____________________________________________
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;


    public class Fun{

    static JLabel label;
    static JButton button;
    static JFrame frame;
    static JPanel panel;
    static JTextField textField;

    public static void main(String[] args){

    frame = new JFrame("Hello World!");

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setSize(500, 400);
    frame.setResizable(true);
    frame.setLayout(new FlowLayout());
    frame.setLocationRelativeTo(null);
    frame.getContentPane().setBackground(Color.CYAN);

    textField = new JTextField("Type Here");
    frame.add(textField);
    button = new JButton("ClickME!");
    frame.add(button);
    frame.setVisible(true);

    button.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {

    frame.remove(textField);
    System.out.println("BUTTON IS PRESSED!");
    frame.repaint();
    frame.setVisible(true);

    }
    });

    }

    }
    __________________________________________________ ____________________________________________
    So in other words instead of using one method you said was located in the containers class I just repainted and setVisible to true and It worked perfectly! Thanks for walking me through It! You earned a Thanks!

  18. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Swing Application

    Glad you found a way to get what you wanted.
    Another way was to call the Container class's validate() method to have its layout manager redo the layout.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. java swing application to java webapplication
    By larbie in forum Java Theory & Questions
    Replies: 2
    Last Post: May 30th, 2013, 03:00 PM
  2. Thread dysfunctioning in swing application
    By rpathak123 in forum Threads
    Replies: 0
    Last Post: April 4th, 2013, 04:19 AM
  3. A date picker in my swing application.
    By Petsoft in forum AWT / Java Swing
    Replies: 1
    Last Post: November 7th, 2012, 09:39 AM
  4. Java program to Add a JMenu toolbar to a Java Swing application
    By JavaPF in forum Java Swing Tutorials
    Replies: 6
    Last Post: March 6th, 2012, 12:25 PM
  5. Java program to Add a JMenu toolbar to a Java Swing application
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 3
    Last Post: April 30th, 2010, 09:00 AM