WindowBuilder Can not render GUI [message #1011863] |
Wed, 20 February 2013 11:46 |
Sikandar Ali Messages: 3 Registered: February 2013 |
Junior Member |
|
|
package com.swimap.omc.dcctool.view;
import java.awt.EventQueue;
public class MyFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MyFrame frame = new MyFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MyFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new GridBagLayout());
contentPane.add(getExpPathTf());
setContentPane(contentPane);
}
private JTextField getExpPathTf() {
JTextField exportPathTf = SwingUtil.createJTextField();
exportPathTf.setText("sikandar");
exportPathTf.setEditable(false);
return exportPathTf;
}
private static class SwingUtil {
public static JTextField createJTextField() {
JTextField tf = new JTextField();
tf.setBounds(10, 10, 100, 20);
return tf;
}
}
}
-
Attachment: MyFrame.java
(Size: 1.39KB, Downloaded 439 times)
|
|
|
|
|
Re: WindowBuilder Can not render GUI [message #1012961 is a reply to message #1012249] |
Fri, 22 February 2013 14:22 |
Eric Clayberg Messages: 979 Registered: July 2009 Location: Boston, MA |
Senior Member |
|
|
Your initial example was caused by using an inner/private class.
Your new example is something entirely different and also working as designed (for different reasons).
As written, your getTF() method does not represent a supported component creation pattern.
The method name begins with "get" which implies a getter, but it actually creates a new component every time rather than following the typical lazy creation pattern.
Even when it appears to "work", this is just a side effect of a different feature.
public JTextField getTF() {
System.out.println("abc");
return SwingUtil.createTF();
}
We have support for evaluating simple "single return statement" methods at design time to provide values for constants, parameters, etc. Once you add a second statement, we will no longer evaluate it given the likelihood of dangerous side effects at design time.
So...if you want to make it a real getter, follow the normal lazy creation patter like this:
private JTextField tf;
...
private JTextField getTF() {
if (tf == null) {
System.out.println("abc");
tf = SwingUtil.createTF();
}
return tf;
}
And yes, extract SwingUtil into a top-level class, rename it to SomethingFactory, or mark the createTF() method as a factory...
class SwingUtil {
/**
* @wbp.factory
*/
public static JTextField createTF() {
JTextField tf = new JTextField();
tf.setBounds(20, 20, 200, 20);
tf.setText("Test : Text");
return tf;
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.03820 seconds