Home » Archived » Visual Editor (VE) » Swing: using factory methods to build GUI
Swing: using factory methods to build GUI [message #93376] |
Fri, 10 June 2005 14:19  |
Eclipse User |
|
|
|
Originally posted by: user.domain.invalid
Hello,
Using VE version 1.0.1.1 (the latest stable), I tried building a JPanel
subclass with many JButtons. I want all of the buttons have the same
properties - except for the text - so I thought of this approach:
// class MyPanel extends JPanel
private JButton getRedButton(String text) {
JButton button=new JButton(text);
button.setBackground(java.awt.Color.red);
button.set...(..);
return button;
}
Then, every button's getter method looks like this:
private JButton getFirst() {
if (first==null) {
first=getRedButton("First");
first.setSomeUniqueProperty(...);
}
return first;
}
Of-course, MyPanel.initialize() adds get*() to the panel.
This approach works, but... The Visual Editor doesn't show these buttons
on the panel. It doesn't even show them in the Java Beans tree. This
behavior makes it very hard to design complex UIs. In fact, it also
makes the VE useless for this case.
What am I doing wrong? I know, I can do everything in the "regular" way
- set all properties in the getters. But that means a lot of code
duplication.
Please suggest ways to fix the problem.
Thanks,
Noam.
|
|
| | | |
Re: Swing: using factory methods to build GUI [message #93480 is a reply to message #93466] |
Fri, 10 June 2005 15:36   |
Eclipse User |
|
|
|
Rich Kulp wrote:
> Instead of a factory method you would need to create a specialized
> subclass of JButton that when created initializes itself the way you
> want.
I tried that approach. Actually, the problem I posted here is a
simplification of the real thing: I already use an extended JButton (I
needed to add a field anyway). Let's call that subclass MyButton.
One of the properties shared among all of the buttons is an
ActionListener, implemented by MyPanel. So I added a MyButton
constructor that takes a String and an ActionListener.
This makes the VE give the following error (in the JavaBeans tree):
java.lang.NoSuchMethodException(MyButton.<init>(java.lang.String,
javax.swing.JPanel)
- even though MyPanel is a JPanel that implements ActionListener, and
MyButton has the required constructor (Eclipse compiles the code with no
errors or warnings).
Another workaround I tried was to create an anonymous class inside
MyPanel that implements ActionListener, and to give the object of this
class as a parameter to MyButton's constructor:
private getFirst() {
if (first==null) {
first=new MyButton("one", listener);
}
return first;
}
With that, I get a different error on each button:
java.lang.IllegalArgumentException(IWAV0177E Expression "listener" is
too complicated. - new MyButton("one", listener))
What else can be done?
- Noam.
>
> Noam Tamim wrote:
>
>> Rich Kulp wrote:
>>
>>> WE currently don't support factory methods. It is a high priority for
>>> us but probably won't make it for VE 1.1
>>
>>
>>
>> Is there a different approach I can try to achieve the same result
>> (i.e. reduce code duplication while keeping VE useful)?
>>
>> Thanks,
>> Noam.
>
>
|
|
| |
Re: Swing: using factory methods to build GUI [message #93616 is a reply to message #93531] |
Sat, 11 June 2005 07:34   |
Eclipse User |
|
|
|
I've just downloaded and installed VE 1.1M2, with all its dependencies -
and I'm sorry to say this does not work. This might be a bug, so please
take the following as a test case:
public class MyPanel extends JPanel {
private JButton jButton = null;
public MyPanel() {
super();
initialize();
}
private ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(e);
}
}; // @jve:decl-index:0
private void initialize() {
this.setLayout(new FlowLayout());
this.setSize(300, 61);
this.add(getJButton(), null);
}
private JButton getJButton() {
if (jButton == null) {
jButton = new MyButton("one", listener);
}
return jButton;
}
} // @jve:decl-index=0:visual-constraint="10,10"
public class MyButton extends JButton {
public MyButton() {
// Are no-arguments-ctors a requirement for visual classes?
}
public MyButton(String text, ActionListener listener) {
super(text);
addActionListener(listener);
setBackground(java.awt.Color.green);
}
}
I've also tried initializing the listener in the initialize() method
(before adding getJButton() to the panel). It didn't change anything.
Thanks for your time,
Noam.
Rich Kulp wrote:
> We just released 1.1M2 and there you can use the MyButton("one",
> listener) type construction if you do one more thing. On the line where
> you declare the listener variable add this comment:
> // @jve:decl-index=0
> This tells us that listener is of interest and we should pay attention
> to it.
|
|
|
Re: Swing: using factory methods to build GUI [message #93909 is a reply to message #93616] |
Mon, 13 June 2005 20:58  |
Eclipse User |
|
|
|
Hi Noam,
There are a couple problems with the source pattern of the code.
First, '// @jve:decl-index=0' works only for fields/variables which are
on a single line. I opened bug
https://bugs.eclipse.org/bugs/show_bug.cgi?id=99637 for this.
Second, VE doesnt handle anonymous inner classes, so the ActionListener
field cannot be instantiated on the remote VM. I created a new class
below where the listener is a static inner class (pasted source below) -
which should work. Except that it throws a NPE exception. I have opened
defecthttps://bugs.eclipse.org/bugs/show_bug.cgi?id=99859 to track this
problem. Thanks for finding the problems.
Regards,
Sri.
-----------------------------------------
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class Newsg1 extends JPanel {
private JButton jButton = null;
public static class EventHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.out.println(e);
}
}
EventHandler eventHandler = new EventHandler(); //@jve:decl-index=0
public Newsg1() {
super();
initialize();
}
private void initialize() {
this.setLayout(new FlowLayout());
this.setSize(300, 61);
this.add(getJButton(), null);
}
private JButton getJButton() {
if (jButton == null) {
jButton = new MyButton("one", eventHandler);
}
return jButton;
}
} // @jve:decl-index=0:visual-constraint="10,10"
-------------------------------------------------
Noam Tamim wrote:
> I've just downloaded and installed VE 1.1M2, with all its dependencies -
> and I'm sorry to say this does not work. This might be a bug, so please
> take the following as a test case:
>
> public class MyPanel extends JPanel {
> private JButton jButton = null;
> public MyPanel() {
> super();
> initialize();
> }
> private ActionListener listener = new ActionListener() {
> public void actionPerformed(ActionEvent e) {
> System.out.println(e);
> }
> }; // @jve:decl-index:0
> private void initialize() {
> this.setLayout(new FlowLayout());
> this.setSize(300, 61);
> this.add(getJButton(), null);
> }
> private JButton getJButton() {
> if (jButton == null) {
> jButton = new MyButton("one", listener);
> }
> return jButton;
> }
> } // @jve:decl-index=0:visual-constraint="10,10"
>
> public class MyButton extends JButton {
> public MyButton() {
> // Are no-arguments-ctors a requirement for visual classes?
> }
> public MyButton(String text, ActionListener listener) {
> super(text);
> addActionListener(listener);
> setBackground(java.awt.Color.green);
> }
> }
>
> I've also tried initializing the listener in the initialize() method
> (before adding getJButton() to the panel). It didn't change anything.
>
>
> Thanks for your time,
> Noam.
>
>
> Rich Kulp wrote:
>
>> We just released 1.1M2 and there you can use the MyButton("one",
>> listener) type construction if you do one more thing. On the line
>> where you declare the listener variable add this comment:
>> // @jve:decl-index=0
>> This tells us that listener is of interest and we should pay attention
>> to it.
|
|
| |
Re: Swing: using factory methods to build GUI [message #607918 is a reply to message #93406] |
Fri, 10 June 2005 14:29  |
Eclipse User |
|
|
|
Rich Kulp wrote:
> WE currently don't support factory methods. It is a high priority for us
> but probably won't make it for VE 1.1
Is there a different approach I can try to achieve the same result (i.e.
reduce code duplication while keeping VE useful)?
Thanks,
Noam.
|
|
|
Re: Swing: using factory methods to build GUI [message #607920 is a reply to message #93436] |
Fri, 10 June 2005 14:58  |
Eclipse User |
|
|
|
Originally posted by: richkulp.us.NO_SPAM.ibm.com
Instead of a factory method you would need to create a specialized
subclass of JButton that when created initializes itself the way you want.
Noam Tamim wrote:
> Rich Kulp wrote:
>
>> WE currently don't support factory methods. It is a high priority for
>> us but probably won't make it for VE 1.1
>
>
> Is there a different approach I can try to achieve the same result (i.e.
> reduce code duplication while keeping VE useful)?
>
> Thanks,
> Noam.
--
Thanks,
Rich Kulp
|
|
|
Re: Swing: using factory methods to build GUI [message #607921 is a reply to message #93466] |
Fri, 10 June 2005 15:36  |
Eclipse User |
|
|
|
Rich Kulp wrote:
> Instead of a factory method you would need to create a specialized
> subclass of JButton that when created initializes itself the way you
> want.
I tried that approach. Actually, the problem I posted here is a
simplification of the real thing: I already use an extended JButton (I
needed to add a field anyway). Let's call that subclass MyButton.
One of the properties shared among all of the buttons is an
ActionListener, implemented by MyPanel. So I added a MyButton
constructor that takes a String and an ActionListener.
This makes the VE give the following error (in the JavaBeans tree):
java.lang.NoSuchMethodException(MyButton.<init>(java.lang.String,
javax.swing.JPanel)
- even though MyPanel is a JPanel that implements ActionListener, and
MyButton has the required constructor (Eclipse compiles the code with no
errors or warnings).
Another workaround I tried was to create an anonymous class inside
MyPanel that implements ActionListener, and to give the object of this
class as a parameter to MyButton's constructor:
private getFirst() {
if (first==null) {
first=new MyButton("one", listener);
}
return first;
}
With that, I get a different error on each button:
java.lang.IllegalArgumentException(IWAV0177E Expression "listener" is
too complicated. - new MyButton("one", listener))
What else can be done?
- Noam.
>
> Noam Tamim wrote:
>
>> Rich Kulp wrote:
>>
>>> WE currently don't support factory methods. It is a high priority for
>>> us but probably won't make it for VE 1.1
>>
>>
>>
>> Is there a different approach I can try to achieve the same result
>> (i.e. reduce code duplication while keeping VE useful)?
>>
>> Thanks,
>> Noam.
>
>
|
|
|
Re: Swing: using factory methods to build GUI [message #607924 is a reply to message #93480] |
Fri, 10 June 2005 18:19  |
Eclipse User |
|
|
|
Originally posted by: richkulp.us.NO_SPAM.ibm.com
The stuff you are trying to do will probably not work in 1.0.x. The
constructor is too complicated to use.
We just released 1.1M2 and there you can use the MyButton("one",
listener) type construction if you do one more thing. On the line where
you declare the listener variable add this comment:
// @jve:decl-index=0
This tells us that listener is of interest and we should pay attention
to it.
The reason the ctor failed when you passed in JPanel is because we have
to look up the constructors, and we don't take interfaces into account,
just the class. You didn't have a ctor that took JPanel .
--
Thanks,
Rich Kulp
|
|
|
Re: Swing: using factory methods to build GUI [message #607930 is a reply to message #93531] |
Sat, 11 June 2005 07:34  |
Eclipse User |
|
|
|
I've just downloaded and installed VE 1.1M2, with all its dependencies -
and I'm sorry to say this does not work. This might be a bug, so please
take the following as a test case:
public class MyPanel extends JPanel {
private JButton jButton = null;
public MyPanel() {
super();
initialize();
}
private ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(e);
}
}; // @jve:decl-index:0
private void initialize() {
this.setLayout(new FlowLayout());
this.setSize(300, 61);
this.add(getJButton(), null);
}
private JButton getJButton() {
if (jButton == null) {
jButton = new MyButton("one", listener);
}
return jButton;
}
} // @jve:decl-index=0:visual-constraint="10,10"
public class MyButton extends JButton {
public MyButton() {
// Are no-arguments-ctors a requirement for visual classes?
}
public MyButton(String text, ActionListener listener) {
super(text);
addActionListener(listener);
setBackground(java.awt.Color.green);
}
}
I've also tried initializing the listener in the initialize() method
(before adding getJButton() to the panel). It didn't change anything.
Thanks for your time,
Noam.
Rich Kulp wrote:
> We just released 1.1M2 and there you can use the MyButton("one",
> listener) type construction if you do one more thing. On the line where
> you declare the listener variable add this comment:
> // @jve:decl-index=0
> This tells us that listener is of interest and we should pay attention
> to it.
|
|
|
Re: Swing: using factory methods to build GUI [message #607950 is a reply to message #93616] |
Mon, 13 June 2005 20:58  |
Eclipse User |
|
|
|
Hi Noam,
There are a couple problems with the source pattern of the code.
First, '// @jve:decl-index=0' works only for fields/variables which are
on a single line. I opened bug
https://bugs.eclipse.org/bugs/show_bug.cgi?id=99637 for this.
Second, VE doesnt handle anonymous inner classes, so the ActionListener
field cannot be instantiated on the remote VM. I created a new class
below where the listener is a static inner class (pasted source below) -
which should work. Except that it throws a NPE exception. I have opened
defecthttps://bugs.eclipse.org/bugs/show_bug.cgi?id=99859 to track this
problem. Thanks for finding the problems.
Regards,
Sri.
-----------------------------------------
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class Newsg1 extends JPanel {
private JButton jButton = null;
public static class EventHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.out.println(e);
}
}
EventHandler eventHandler = new EventHandler(); //@jve:decl-index=0
public Newsg1() {
super();
initialize();
}
private void initialize() {
this.setLayout(new FlowLayout());
this.setSize(300, 61);
this.add(getJButton(), null);
}
private JButton getJButton() {
if (jButton == null) {
jButton = new MyButton("one", eventHandler);
}
return jButton;
}
} // @jve:decl-index=0:visual-constraint="10,10"
-------------------------------------------------
Noam Tamim wrote:
> I've just downloaded and installed VE 1.1M2, with all its dependencies -
> and I'm sorry to say this does not work. This might be a bug, so please
> take the following as a test case:
>
> public class MyPanel extends JPanel {
> private JButton jButton = null;
> public MyPanel() {
> super();
> initialize();
> }
> private ActionListener listener = new ActionListener() {
> public void actionPerformed(ActionEvent e) {
> System.out.println(e);
> }
> }; // @jve:decl-index:0
> private void initialize() {
> this.setLayout(new FlowLayout());
> this.setSize(300, 61);
> this.add(getJButton(), null);
> }
> private JButton getJButton() {
> if (jButton == null) {
> jButton = new MyButton("one", listener);
> }
> return jButton;
> }
> } // @jve:decl-index=0:visual-constraint="10,10"
>
> public class MyButton extends JButton {
> public MyButton() {
> // Are no-arguments-ctors a requirement for visual classes?
> }
> public MyButton(String text, ActionListener listener) {
> super(text);
> addActionListener(listener);
> setBackground(java.awt.Color.green);
> }
> }
>
> I've also tried initializing the listener in the initialize() method
> (before adding getJButton() to the panel). It didn't change anything.
>
>
> Thanks for your time,
> Noam.
>
>
> Rich Kulp wrote:
>
>> We just released 1.1M2 and there you can use the MyButton("one",
>> listener) type construction if you do one more thing. On the line
>> where you declare the listener variable add this comment:
>> // @jve:decl-index=0
>> This tells us that listener is of interest and we should pay attention
>> to it.
|
|
|
Goto Forum:
Current Time: Mon Jun 23 03:33:51 EDT 2025
Powered by FUDForum. Page generated in 0.14948 seconds
|