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

Thread: More GUI help

  1. #1
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default More GUI help

    Hello again,

    I am making another test for my GUI and this one is supposed to have a couple of buttons that react and repaint the string that is displayed. Here is the code so far:

    package Tests;
     
    import java.awt.*;
    import java.awt.Event.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.*;
     
    public class NumbersTest {
     
    	private static class Display extends JPanel  {
     
    		public String currentMessage = "Click a button and see what happens!";
     
    		public void paintComponent(Graphics g)  {
     
    			super.paintComponent(g);
    			g.drawString(currentMessage, 50, 215);
     
     
    		}
     
            private static class buttonHandler1 implements ActionListener  {
     
    			String currentMessage;
     
    			public void actionPerformed(ActionEvent e) {
    				currentMessage = "You clicked Button #!";
     
    			}
     
    		}
     
            private static class buttonHandler2 implements ActionListener  {
     
    			String currentMessage;
     
    			public void actionPerformed(ActionEvent e) {
    				currentMessage = "You clicked Button #2!";
    			}
     
    		}
     
    		private static class buttonHandler3 implements ActionListener  {
     
    			String currentMessage;
     
    			public void actionPerformed(ActionEvent e) {
    				currentMessage = "You clicked Button #3!";
    			}
     
    		}
     
    		private static class closeButtonHandler implements ActionListener {
     
    			String currentMessage;
     
    			public void actionPerformed(ActionEvent e)  {
     
    				System.exit(0);
     
    			}
     
    		}
     
    		public static void main(String[] arg)  {
     
    			String currentMessage;
     
    			Display displayPanel = new Display();
    			JButton button1 = new JButton("Button #1");
    			JButton button2 = new JButton("Button #2");
    			JButton button3 = new JButton("Button #3");
    			JButton closeButton = new JButton("Close");
    			buttonHandler1 listenerb1 = new buttonHandler1();
    			buttonHandler2 listenerb2 = new buttonHandler2();
    			buttonHandler3 listenerb3 = new buttonHandler3();
    		    closeButtonHandler closelistener = new closeButtonHandler();
     
    		    button1.addActionListener(listenerb1);
    		    button2.addActionListener(listenerb2);
    		    button3.addActionListener(listenerb3);
    		    closeButton.addActionListener(closelistener);
     
    		    JPanel content = new JPanel();
    		    content.setLayout(new BorderLayout());
    		    content.add(displayPanel, BorderLayout.CENTER);
    		    content.add(button1, BorderLayout.NORTH);
    		    content.add(button2, BorderLayout.WEST);
    		    content.add(button3, BorderLayout.EAST);
    		    content.add(closeButton, BorderLayout.SOUTH);
    		    content.setVisible(true);
     
    		    JFrame window = new JFrame("Numbers and Buttons");
    			window.setContentPane(content);
    	        window.setSize(500, 500);
    			window.setLocation(100, 100);
    			window.setVisible(true);
     
     
     
    		}
     
    	}
     
    }

    How do I make it so that when the buttons are clicked, it repaints the string displayed to whatever I want it to? I was thinking repaint(); but I'm not sure.

    Help?

    -Silent


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: More GUI help

    Calling repaint() will cause the java program to call the paintComponent method very soon afterwards.
    You will have to tell the paintComponent method what String you want it to draw.

  3. #3
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: More GUI help

    OK, so how do I call repaint()?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: More GUI help

    objectReference.repaint();

  5. #5
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: More GUI help

    So, in this case, what is my object reference that would work in this context?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: More GUI help

    What class is the paintComponent method in?

    Also you need to set String that is to be shown. The handlers are only setting a local variable.
    Last edited by Norm; March 13th, 2012 at 09:22 PM.

  7. #7
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: More GUI help

    Well, the paintComponent method is in the Display routine (or class).

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: More GUI help

    what is my object reference that would work
    paintComponent method is in the Display routine (or class).
    You would need a reference to the instance of the Display class.

    Your nested classes will be a problem. An inner class would be able to see the variables of the class that it is in.
    The static nested classes will need a reference to any class whose variables it wants to access.

  9. #9
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: More GUI help

    Uhh... I think I get what your saying. Would you mind quick throwing together some code so I can see what you really mean?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: More GUI help

    You can read here about nested and inner classes:
    Nested Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    You need to decide if you want either of those or if you want all you classes external to each other.
    If you use inner classes, then the inner classes will have access to the class variables that they are inside, like the currentMessage variable
    You should remove all the local definitions for that variable and only have one in the class with the paintComponent method.
    Last edited by Norm; March 14th, 2012 at 08:32 PM.

  11. #11
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: More GUI help

    Alright, thanks. I will look at the tut.

  12. #12
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: More GUI help

    Ok, so I read up on the nested classes thing and I edited the code, and the repaint() is not underlined, so that's a good sign. Here is the code:

    package Tests;
     
    import java.awt.*;
    import java.awt.Event.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.*;
     
    public class NumbersTest {
     
    	private class Display extends JPanel  {
     
    		public String currentMessage = "Click a button and see what happens!";
     
    		public void paintComponent(Graphics g)  {
     
    			super.paintComponent(g);
    			g.drawString(currentMessage, 50, 215);
     
     
    		}
     
            private class buttonHandler1 implements ActionListener  {
     
    			String currentMessage;
     
    			public void actionPerformed(ActionEvent e) {
    				currentMessage = "You clicked Button #!";
    				repaint(currentMessage);
     
    			}
     
    		}
     
            private class buttonHandler2 implements ActionListener  {
     
    			String currentMessage;
     
    			public void actionPerformed(ActionEvent e) {
    				currentMessage = "You clicked Button #2!";
    				repaint(currentMessage);
    			}
     
    		}
     
    		private class buttonHandler3 implements ActionListener  {
     
    			String currentMessage;
     
    			public void actionPerformed(ActionEvent e) {
    				currentMessage = "You clicked Button #3!";
    				repaint(currentMessage);
    			}
     
    		}
     
    		private class closeButtonHandler implements ActionListener {
     
    			String currentMessage;
     
    			public void actionPerformed(ActionEvent e)  {
     
    				System.exit(0);
     
    			}
     
    		public static void main(String[] arg)  {
     
    			String currentMessage;
     
    			Display displayPanel = new Display();
    			JButton button1 = new JButton("Button #1");
    			JButton button2 = new JButton("Button #2");
    			JButton button3 = new JButton("Button #3");
    			JButton closeButton = new JButton("Close");
    			buttonHandler1 listenerb1 = new buttonHandler1();
    			buttonHandler2 listenerb2 = new buttonHandler2();
    			buttonHandler3 listenerb3 = new buttonHandler3();
    		    closeButtonHandler closelistener = new closeButtonHandler();
     
    		    button1.addActionListener(listenerb1);
    		    button2.addActionListener(listenerb2);
    		    button3.addActionListener(listenerb3);
    		    closeButton.addActionListener(closelistener);
     
    		    JPanel content = new JPanel();
    		    content.setLayout(new BorderLayout());
    		    content.add(displayPanel, BorderLayout.CENTER);
    		    content.add(button1, BorderLayout.NORTH);
    		    content.add(button2, BorderLayout.WEST);
    		    content.add(button3, BorderLayout.EAST);
    		    content.add(closeButton, BorderLayout.SOUTH);
    		    content.setVisible(true);
     
    		    JFrame window = new JFrame("Numbers and Buttons");
    			window.setContentPane(content);
    	        window.setSize(500, 500);
    			window.setLocation(100, 100);
    			window.setVisible(true);
     
     
     
    		}
     
    	}
     
    		public void repaint(String currentMessage2) {
     
     
    		}
     
    	}
     
    }

    But, the public static void main(String[] arg) apparently has an error, and it says:

    The method main cannot be declared static; static methods can only be declared in a static or top level type

    Help?

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: More GUI help

    static methods can only be declared in a static or top level type
    That says what the problem is. A static method can only be in a static or top level class.
    Move the main method or make the class it is in static.

    Why do you have the NumbersTest class?

    Why do you have so many variables with the same name: currentMessage?

  14. #14
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: More GUI help

    Oh, woops. I can delete those.

    The first question, not sure what to say.

    And, I'm supposed to move the main() method to.... where?

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: More GUI help

    Out of an inner, non static class.

    Why do you have the NumbersTest class?

  16. #16
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: More GUI help

    Because I want to test my abilities with GUI?

  17. #17
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: More GUI help

    Alright, I am very annoyed at myself. I make SOOOO many typos.

    It was just a typo. Thanks

  18. #18
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: More GUI help

    Ok, so now, when I fix the typo, something in the main() routine apparently has an error:

    package Tests;
     
    import java.awt.*;
    import java.awt.Event.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.*;
     
    public class NumbersTest {
     
    	private class Display extends JPanel  {
     
    		public String currentMessage = "Click a button and see what happens!";
     
    		public void paintComponent(Graphics g)  {
     
    			super.paintComponent(g);
    			g.drawString(currentMessage, 50, 215);
     
     
    		}
     
    	}
     
            private class buttonHandler1 implements ActionListener  {
     
    			String currentMessage;
     
    			public void actionPerformed(ActionEvent e) {
    				currentMessage = "You clicked Button #!";
    				repaint(currentMessage);
     
    			}
     
    		}
     
            private class buttonHandler2 implements ActionListener  {
     
    			String currentMessage;
     
    			public void actionPerformed(ActionEvent e) {
    				currentMessage = "You clicked Button #2!";
    				repaint(currentMessage);
    			}
     
    		}
     
    		private class buttonHandler3 implements ActionListener  {
     
    			String currentMessage;
     
    			public void actionPerformed(ActionEvent e) {
    				currentMessage = "You clicked Button #3!";
    				repaint(currentMessage);
    			}
     
    		}
     
    		private class closeButtonHandler implements ActionListener {
     
    			String currentMessage;
     
    			public void actionPerformed(ActionEvent e)  {
     
    				System.exit(0);
     
    			}
     
    		}
     
    	    public static void main(String[] args)  {
     
    			String currentMessage;
     
    			Display displayPanel = new Display();
    			JButton button1 = new JButton("Button #1");
    			JButton button2 = new JButton("Button #2");
    			JButton button3 = new JButton("Button #3");
    			JButton closeButton = new JButton("Close");
    			buttonHandler1 listenerb1 = new buttonHandler1();
    			buttonHandler2 listenerb2 = new buttonHandler2();
    			buttonHandler3 listenerb3 = new buttonHandler3();
    		    closeButtonHandler closelistener = new closeButtonHandler();
     
    		    button1.addActionListener(listenerb1);
    		    button2.addActionListener(listenerb2);
    		    button3.addActionListener(listenerb3);
    		    closeButton.addActionListener(closelistener);
     
    		    JPanel content = new JPanel();
    		    content.setLayout(new BorderLayout());
    		    content.add(displayPanel, BorderLayout.CENTER);
    		    content.add(button1, BorderLayout.NORTH);
    		    content.add(button2, BorderLayout.WEST);
    		    content.add(button3, BorderLayout.EAST);
    		    content.add(closeButton, BorderLayout.SOUTH);
    		    content.setVisible(true);
     
    		    JFrame window = new JFrame("Numbers and Buttons");
    			window.setContentPane(content);
    	        window.setSize(500, 500);
    			window.setLocation(100, 100);
    			window.setVisible(true);
     
     
     
    		}
     
     
    		public void repaint(String currentMessage2) {
     
     
    		}
     
    	}

    The new Display(); in Display displayPanel = new Display(); is underlined and it says

    No enclosing instance of type NumbersTest is accessible. Must qualify the allocation with an enclosing instance of type NumbersTest (e.g. x.new A() where x is an instance of NumbersTest).

    Honestly, I have absolutely no idea what this means.

    Anybody else know?

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: More GUI help

    I think the link I posted in #10 discusses this. The Display class is an inner class. You need an instance of the NumbersTest class to be able to create a Display class object.

    Why not get rid of the NumbersTest class? Or move the Display class out of the NumbersTestClass?
    Or move all of the code in the main() method into the NumbersTest class's constructor and have the main() method just create an instance of NumbersTest?

  20. #20
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: More GUI help

    Wait... What? Ok, I am so very confused. I'm just going to let this project drop.

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: More GUI help

    Get rid of the NumbersTest class and use the Display class.