On the Linux platform, Frame :: getBounds and Frame :: setBounds do not work sequentially. This was already reported in 2003 (!). See here:
http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4806603
For convenience, I simplified the specified code, which leads to an error and inserts it as:
import java.awt.Button; import java.awt.Frame; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class GetBoundsBug extends Frame implements ActionListener { public static void main(String[] arg) { GetBoundsBug frame = new GetBoundsBug(); Button button = new Button("Click here!"); button.addActionListener(frame); frame.add(button); frame.setSize(300, 300); frame.setVisible(true); } @Override public void actionPerformed(ActionEvent event) { Rectangle bounds = getBounds(); bounds.y--; setBounds(bounds); bounds.y++; setBounds(bounds); } }
Unexpected behavior:. When the button is pressed, the window shifts a little lower! (My system has 28 pixels each click.)
Here is the screen recording: https://youtu.be/4qOf99LJOf8
This behavior was about 13 years old, therefore, probably, there will be no changes from the official side.
Does anyone have a workaround for this error? In particular, I would like to reliably store and restore the window / frame / dialog in the previous location on all platforms.
PS: My installation of java jdk1.8.0_102 for amd64 Oracle on Ubuntu 16 Linux. Since I recently migrated from Windows to Ubuntu, I know that on Windows the code above works as expected.
Adapting to Swing using SwingWorker has the same effect:
import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.SwingWorker; public class GetBoundsBug extends JFrame implements ActionListener { public static void main(String[] arg) { GetBoundsBug myJFrame = new GetBoundsBug(); JButton myJButton = new JButton("Click here!"); myJButton.addActionListener(myJFrame); myJFrame.setContentPane(myJButton); myJFrame.setSize(300, 300); myJFrame.setVisible(true); } @Override public void actionPerformed(ActionEvent event) { SwingWorker<Void, Void> mySwingWorker = new SwingWorker<Void, Void>() { @Override public Void doInBackground() { Rectangle myRectangle = getBounds(); myRectangle.y--; setBounds(myRectangle); myRectangle.y++; setBounds(myRectangle); return null; } }; mySwingWorker.execute(); } }
java linux
datahaki
source share