Results 1 to 6 of 6
  1. #1
    djokovic is offline Member
    Join Date
    May 2012
    Posts
    17
    Rep Power
    0

    Default Problem with GUI.

    Hello, I wanted to create program with JFrame window. In that window I want to add Button, and when people press the button, it will paint the shape on the screen.

    First I made abstract superclass of all shapes.
    Java Code:
    import javax.swing.JPanel;
    import javax.swing.JButton;
    
    abstract public class Draw extends JPanel{
    	 JButton button;
    	 String buttonTitle;
    	 
    	abstract public JButton getButton();
    }
    Then I created one subclass, DrawRect
    Java Code:
    import javax.swing.JButton;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class DrawRect extends Draw{
    	
    	public JButton getButton() {
    		buttonTitle = "Draw Rect";
    		button = new JButton(buttonTitle);
    		return button;
    	}
    	
    	public void paintComponent(Graphics g){
    		g.drawRect(50, 50, 120, 120);
    	}
    
    }
    And this is my main class

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JFrame;
    
    
    public class test extends JFrame implements ActionListener{
    	
       Draw d = new DrawRect();
       
       public static void main(String[] args){
    	   test t = new test();
    	   t.run(); 
       }
       
       public void run(){
    	   
    	   setSize(300,300);
    	   setVisible(true);
    	   getContentPane().add(BorderLayout.NORTH,d.getButton());
    	   
    	   d.getButton().addActionListener(this);
       }
    
    
       public void actionPerformed(ActionEvent e) {
    	   getContentPane().add(BorderLayout.CENTER,d);
    	   
    	   
       }
            
    }
    So, the problem is, that it doesn't work, when I press the button, but If I would copy this line:
    Java Code:
    getContentPane().add(BorderLayout.CENTER,d);
    and add in run() method this pop up when I start program:
    Problem with GUI.-draw.jpg

    But as I said before, if I would remove this line from run, and add same line in actionPerformed method, it doesn't work, even if i click the button. Can you tell me what's wrong here?

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

    Default Re: Problem with GUI.

    After adding the new component to the contentPane, you may want to next call revalidate() and then repaint() on the contentPane. The revalidate tells the contentPane to re-lay-out the components it holds, and the repaint tells the contentPane to repaint itself and its children. Now since contentPane is declared as a Container, the compiler won't like you calling revalidate() since this is a method of JComponent, but since we all know that the contentPane is really a JPanel, we can cast it as such and then be able to call the method:

    Java Code:
    JPanel myContentPane = (JPanel) getContentPane();
    myContentPane.add(someComponent, BorderLayout.CENTER);
    myContentPane.revalidate();
    myContentPane.repaint(); // not always needed

  3. #3
    djokovic is offline Member
    Join Date
    May 2012
    Posts
    17
    Rep Power
    0

    Default Re: Problem with GUI.

    Quote Originally Posted by Fubarable View Post
    After adding the new component to the contentPane, you may want to next call revalidate() and then repaint() on the contentPane. The revalidate tells the contentPane to re-lay-out the components it holds, and the repaint tells the contentPane to repaint itself and its children. Now since contentPane is declared as a Container, the compiler won't like you calling revalidate() since this is a method of JComponent, but since we all know that the contentPane is really a JPanel, we can cast it as such and then be able to call the method:

    Java Code:
    
    JPanel myContentPane = (JPanel) getContentPane();
    myContentPane.add(someComponent, BorderLayout.CENTER);
    myContentPane.revalidate();
    myContentPane.repaint(); // not always needed
    So I need to add your code to actionPerformed method and change someComponent to "d"? I'm asking because it doesn't work also.
    Last edited by djokovic; 06-05-2012 at 02:09 PM.

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

    Default Re: Problem with GUI.

    You are creating a new and different JButton each time you call getButton(). Thus the JButton that is being displayed is not the same as the one with the ActionListener (which you'd discover if you placed System.out.println(...) debug statements in the actionPerformed method). If you're going to do lazy initialization of the button, check first if button is null before creating a new one.

  5. #5
    djokovic is offline Member
    Join Date
    May 2012
    Posts
    17
    Rep Power
    0

    Default Re: Problem with GUI.

    Quote Originally Posted by Fubarable View Post
    You are creating a new and different JButton each time you call getButton(). Thus the JButton that is being displayed is not the same as the one with the ActionListener (which you'd discover if you placed System.out.println(...) debug statements in the actionPerformed method). If you're going to do lazy initialization of the button, check first if button is null before creating a new one.
    So, I need to add actionlistener in the same class where button was created? Thank you, but if I would have ten buttons, it is ok to do everything in one class? I don't remember now, but in one Java book, I read, that I need to split work, and don't do everything in one place.

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

    Default Re: Problem with GUI.

    Quote Originally Posted by djokovic View Post
    So, I need to add actionlistener in the same class where button was created?
    I did not say anything like that, and if you thought I did, please show me where. I said if you are going to have lazy initialization or creation of your JButton, you'd better first check if its null. If it's not null, then return the actual JButton instance held by the button variable and don't create a new one. You don't want to create a bunch of buttons when all you need is one.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •