I've been playing with Java21ea getting ready for its release this fall
and have found some warnings that I don't really know how to deal with.
I understand why you don't want 'this' escaping the constructor but I
don't understand how Java21 think that a lot of the methods we have
always used are going to cause that to happen. Please see the code
below and the script to compile it. I've commented out several lines
but you can un-comment them and see the warnings from the compiler. The first one that I really don't understand is Frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE). I would appreciate
any comments or maybe I just don't know what I'm doing and there is a
better way. If so please feel free to critique. It compiles and will
run but not work correctly with the lines commented out. Put them back
in and compile them to see the warnings.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class test extends JFrame {
private static final long serialVersionUID = 1L;
public test() {
super("test");
//setLayout(new GridBagLayout()); // [this-escape]
//setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // [this-escape]
GridBagConstraints c = new GridBagConstraints();
JLabel l = new JLabel("this is a JLabel");
l.setBorder(new EmptyBorder(20,20,20,20)); // [this-escape] sometimes
//add(l,c); // [this-escape]
//add(l); // [this-escape]
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
test t = new test();
t.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
t.dispose();
}
});
t.pack();
t.setVisible(true);
});
}
}
[...] I've commented out several lines
but you can un-comment them and see the warnings from the compiler. The first one that I really don't understand is Frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE). [...]
public class test extends JFrame {
public test() {
super("test");
//setLayout(new GridBagLayout()); // [this-escape] //setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //
[this-escape]
GridBagConstraints c = new GridBagConstraints();
JLabel l = new JLabel("this is a JLabel");
l.setBorder(new EmptyBorder(20,20,20,20)); // [this-escape]
sometimes
//add(l,c); // [this-escape]
//add(l); // [this-escape]
Hi Knute,
Still Swing-ing? :-)
The one I don't get (and haven't been able to reproduce) is the
#setBorder call.
Is [this-escape] a warning/error that's been specifically added in
JDK21? I reckon the problem itself isn't new or changed, and that it's
always been there.
As such, you would have two choices, as before: ignore the warning or
change your habits.
For illustration, with Swing I'd made it a habit to construct my UI in #addNotify, for self-constructing components. That wouldn't be
applicable to the frame, which you could for instance construct
externally (I mean in a factory) with a self-constructing content pane.
On Friday, June 9, 2023 at 9:32:41 PM UTC-4, Knute Johnson wrote:
[...] I've commented out several lines
but you can un-comment them and see the warnings from the compiler. The
first one that I really don't understand is
Frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE). [...]
I've not tried Java21 and am just guessing wildly, so take
my observations with a grain -- nay, a tonne -- of salt:
public class test extends JFrame {
Have you tried making the class final? Alternatively, ...
public test() {
super("test");
//setLayout(new GridBagLayout()); // [this-escape]
//setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //
[this-escape]
... have you tried super.setXXX instead of bare setXXX?
l.setBorder(new EmptyBorder(20,20,20,20)); // [this-escape]
sometimes
I don't understand (or even guess at) this one, not at all.
I've been playing with Java21ea getting ready for its release this fall
and have found some warnings that I don't really know how to deal with.
I understand why you don't want 'this' escaping the constructor but I
don't understand how Java21 think that a lot of the methods we have
always used are going to cause that to happen.
On 6/10/23 06:46, Daniele Futtorovic wrote:
As such, you would have two choices, as before: ignore the warning or
change your habits.
See Eric Sosman's post.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 335 |
Nodes: | 16 (2 / 14) |
Uptime: | 149:07:13 |
Calls: | 7,411 |
Calls today: | 1 |
Files: | 12,670 |
Messages: | 5,613,192 |