I have Jpanel which is maximized on a button click. I do it by setting the dimensions to the screensize and then calling setLocationRelativeTo(null). This works fine if i have the panel in primary monitor, but when the panel is on other monitor the button click always makes the panel to move to primary monitor. Is there a way to get the currently used monitor of the panel and use the same for new location. Thanks in advance.
Asked
Active
Viewed 1,786 times
0
-
1http://stackoverflow.com/questions/10888131/determine-windows-display-number-and-or-layout-via-java – Aubin Nov 29 '12 at 17:54
-
Hi, I have seen this link, in my case there is no issue if the Panel is in primary monitor already, since the button click moves the window to the primary monitor. To be more clear, I want to identity the monitor in which the panel is currently, user could move it between monitors. I want to find in which monitor the window is currently and set the new location also to the same. how this could be done using GraphicsConfiguration? I could not figure it out from the Javadocs. – vumaasha Nov 29 '12 at 19:38
-
http://stackoverflow.com/questions/1248386/how-do-i-determine-which-monitor-a-swing-mouse-event-occurs-in Following the above thread helped – user111093 Dec 05 '12 at 12:17
1 Answers
0
I have only one screen at home and will be back to my job monday, but the following code set the title of the JFrame to "\Display0" under Windows Seven 64 bits. I suppose this string is the ID of the Screen and the second Screen is named "\Display1".
I suggest to run this application on multi-screen PC to evaluate it.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Frm
extends
JFrame
implements
ActionListener
{
Frm()
{
setDefaultCloseOperation( EXIT_ON_CLOSE );
setPreferredSize( new Dimension( 640, 480 ));
setLayout( new BorderLayout());
JButton getScreenBtn = new JButton( "Get Screen ID" );
add( getScreenBtn, BorderLayout.CENTER );
getScreenBtn.addActionListener( this );
pack();
setLocationRelativeTo( null );
setVisible( true );
}
@Override
public void actionPerformed( ActionEvent e ) {
setTitle( getGraphicsConfiguration().getDevice().getIDstring());
}
public static void main( String[] args ) {
SwingUtilities.invokeLater( new Runnable() {
@Override public void run() { new Frm(); }});
}
}

Aubin
- 14,617
- 9
- 61
- 84