Results 1 to 6 of 6
Thread: Problem with GUI.
- 06-05-2012, 01:11 PM #1
Member
- Join Date
- May 2012
- Posts
- 17
- Rep Power
- 0
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.
Then I created one subclass, DrawRectJava Code:import javax.swing.JPanel; import javax.swing.JButton; abstract public class Draw extends JPanel{ JButton button; String buttonTitle; abstract public JButton getButton(); }
And this is my main classJava 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); } }
So, the problem is, that it doesn't work, when I press the button, but If I would copy this line: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); } }
and add in run() method this pop up when I start program:Java Code:getContentPane().add(BorderLayout.CENTER,d);

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?
-
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
- 06-05-2012, 02:03 PM #3
Member
- Join Date
- May 2012
- Posts
- 17
- Rep Power
- 0
-
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.
- 06-05-2012, 06:58 PM #5
Member
- Join Date
- May 2012
- Posts
- 17
- Rep Power
- 0
Re: Problem with GUI.
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.
-
Re: Problem with GUI.
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.


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks