Reputation: 611
I have bought and set up a second monitor. When I open eclipse and drag it on the second monitor, if I run the swing applications, they appear on the firs one.
Additional info:
My drivers as well as eclipse are up to date.
Monitor 1: Laptop integrated monitor, QHD, 17'. Everything scales up, including swing. Handled by integrated Intel graphics>>(Windows decides, this cannot be changed)
Monitor 2: External HP 22' FHD monitor. When 51% of a window is moved to this screen, it scales down (yet still up from original size, usually tought for 1080p). Handles by NVIDIA Gpu>>(Windows decides, this cannot be changed)
EXAMPLE:
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class test {
public static void main(String[] args){
new test();
}
public test(){
JFrame testingBlack = new JFrame("MCVe");
JPantest testingB = new JPantest();
testingBlack.add(testingB);
testingBlack.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
testingBlack.setVisible(true);
}
private class JPantest extends JPanel{
public JPantest(){
super();
repaint();
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawLine(0, 0, 100, 100);
}
}
}
This program draws a simple line. I run it from ecplise, it opens from the first monitor and it's fine. I drag on the second monitor, and it turns black.
Upvotes: 2
Views: 1943
Reputation: 13427
Is there a swing parameter to make the programs run on the second monitor, or at least on the monitor where eclipse is running?
The location of your IDE is irrelevant. Any parentless window will be initialized at the (0, 0)
coordinate of the screen equivalent to that returned by GraphicsEnvironment#getDefaultScreenDevice()
. You can use setLocationByPlatform
to allow the windowing system (OS) to determine the location instead. If you want to force the location of your second monitor, you can do this:
public Main() {
JFrame testingBlack = new JFrame("MCVe");
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gds = ge.getScreenDevices();
GraphicsConfiguration gc = gds[1].getDefaultConfiguration();
Rectangle rect = gc.getBounds();
testingBlack.setLocation(rect.getLocation());
// or, if you like this style better
testingBlack.setLocation(GraphicsEnvironment
.getLocalGraphicsEnvironment()
.getScreenDevices()[1]
.getDefaultConfiguration()
.getBounds()
.getLocation());
testingBlack.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
testingBlack.setVisible(true);
}
Just make sure you are accessing the right device in your GraphicsDevice[]
to avoid AIOOB exceptions. The one at [0]
should be the default device (though I don't think there's such a guarantee).
If I move the swing program from the first monitor to the second one, it turns black. Why is that?
No idea. Must be something with your monitor setup.
Upvotes: 4