Huhu liebes Forum,
kann mal bitte jemand der ein Windows oder einen Mäc zur Hand hat diesen Quelltext kompilieren und bei sich probieren?
Bei mir gibt das Programm beim Druck auf den Knopf das aus:
Bei Euch auch?
Das Problem führt bei mir reproduzierbar dazu, dass [c]childDialog.setLocationRelativeTo(parentFrame)[/c] nicht richtig funktioniert, wenn das [c]parentFrame[/c] zuvor geändert wurde. Erst wenn die EventQueue zwischen der Änderung der Location des [c]parentFrame[/c]s und dem [c]childDialog.setLocationRelativeTo(parentFrame)[/c] Events abarbeiten konnte, funktioniert das. Mit anderen Worten, das funktioniert nicht richtig :noe::
Aber das funktioniert :bahnhof::
Ebenius
kann mal bitte jemand der ein Windows oder einen Mäc zur Hand hat diesen Quelltext kompilieren und bei sich probieren?
Java:
/* (@)LocationOnScreenBugFun.java */
/* Copyright 2012 Sebastian Haufe
* Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
[url]http://www.apache.org/licenses/LICENSE-2.0[/url]
* Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Window;
import java.awt.event.ActionEvent;
import javax.swing.*;
/**
* Demonstrates how location and locationOnScreen work differently on my
* Ubuntu 11.10 with Java 1.6.0_26-b03.
*
* @version $Revision$ as of $Date$
* @author Sebastian Haufe
*/
public class LocationOnScreenBugFun {
/** Creates the GUI. Call on EDT, only! */
static void createAndShowGui() {
final JPanel contentPane = new JPanel(new BorderLayout(6, 6));
contentPane.add(new JButton(new AbstractAction("Relocate") {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e) {
final Component button = (Component) e.getSource();
final Window w = SwingUtilities.getWindowAncestor(button);
System.out.printf("Old location: %s%n ... on screen: %s%n",
w.getLocation(), w.getLocationOnScreen());
final int screenWidth = w.getToolkit().getScreenSize().width;
w.setLocation(screenWidth - w.getWidth(), 0);
System.out.printf("New location: %s%n ... on screen: %s%n",
w.getLocation(), w.getLocationOnScreen());
}
}));
final JFrame f = new JFrame("Test Frame: LocationOnScreenBugFun"); //$NON-NLS-1$
f.setContentPane(contentPane);
f.pack();
f.setLocation(0, 0);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setVisible(true);
}
/** @param args ignored */
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Bei mir gibt das Programm beim Druck auf den Knopf das aus:
Code:
Old location: java.awt.Point[x=0,y=0]
... on screen: java.awt.Point[x=0,y=0]
New location: java.awt.Point[x=3740,y=0]
... on screen: java.awt.Point[x=0,y=0]
Das Problem führt bei mir reproduzierbar dazu, dass [c]childDialog.setLocationRelativeTo(parentFrame)[/c] nicht richtig funktioniert, wenn das [c]parentFrame[/c] zuvor geändert wurde. Erst wenn die EventQueue zwischen der Änderung der Location des [c]parentFrame[/c]s und dem [c]childDialog.setLocationRelativeTo(parentFrame)[/c] Events abarbeiten konnte, funktioniert das. Mit anderen Worten, das funktioniert nicht richtig :noe::
Java:
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
parentFrame.setLocation(newLocation)
childDialog.setLocationRelativeTo(parentFrame);
}
});
Java:
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
parentFrame.setLocation(newLocation)
}
});
// The EventQueue now processes all pending events before setLocationRelativeTo() is called
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
childDialog.setLocationRelativeTo(parentFrame);
}
});
Ebenius