Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This reminded me of a very favourite post of mine, using <a href="http://docs.oracle.com/javase/7/docs/api/java/awt/Window.html#setLocationByPlatform%28boolean%29" rel="nofollow noreferrer">Window.setLocationByPlatform(true)</a>, on StackOverflow.</p> <p><a href="https://stackoverflow.com/q/7143287/1057230">How to best position Swing GUIs</a></p> <p><strong>EDIT 1 :</strong></p> <p>You can add a <code>FocusListener</code> to your <code>JDialog</code> and on <code>focusGained(...)</code> method, you can use <code>setLocationRelativeTo(null)</code> for both the <code>JFrame</code> and the <code>JDialog</code>, so that they both come to the center of the screen no matter where they are before.</p> <pre><code>import java.awt.*; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; /** * Created with IntelliJ IDEA. * User: Gagandeep Bali * Date: 1/14/13 * Time: 7:34 PM * To change this template use File | Settings | File Templates. */ public class FrameFocus { private JFrame mainwindow; private CustomDialog customDialog; private void displayGUI() { mainwindow = new JFrame("Frame Focus Window Example"); customDialog = new CustomDialog(mainwindow, "Modal Dialog", true); mainwindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); JPanel contentPane = new JPanel(); JButton mainButton = new JButton( "Click me to open a MODAL Dialog"); mainButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (!customDialog.isShowing()) customDialog.setVisible(true); } }); contentPane.add(mainButton); mainwindow.setContentPane(contentPane); mainwindow.pack(); mainwindow.setLocationByPlatform(true); mainwindow.setVisible(true); } public static void main(String... args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new FrameFocus().displayGUI(); } }); } } class CustomDialog extends JDialog { private JFrame mainWindow; public CustomDialog(JFrame owner, String title, boolean modal) { super(owner, title, modal); mainWindow = owner; JPanel contentPane = new JPanel(); JLabel dialogLabel = new JLabel( "I am a Label on JDialog.", JLabel.CENTER); contentPane.add(dialogLabel); setContentPane(contentPane); pack(); addFocusListener(new FocusListener() { @Override public void focusGained(FocusEvent e) { mainWindow.setLocationRelativeTo(null); setLocationRelativeTo(null); } @Override public void focusLost(FocusEvent e) { /* * Nothing written for this part yet */ } }); } } </code></pre> <p><strong>EDIT 2 :</strong></p> <p>I searched a bit here and there, and it turns out, in my opinion, that actually on which <code>Monitor Screen</code> your application comes at the first instance, will determine it's <a href="http://docs.oracle.com/javase/7/docs/api/java/awt/Component.html#getGraphicsConfiguration%28%29" rel="nofollow noreferrer">GraphicsConfiguration</a>. Though as I roamed through the API, there is only a getter method for the said <code>GraphicsConfiguration</code> thingy and no setter methods for the same (Still You can specify one through the constructor of any top level Window i.e. <a href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JFrame.html#JFrame%28java.lang.String,%20java.awt.GraphicsConfiguration%29" rel="nofollow noreferrer">JFrame(...)</a>/<a href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JDialog.html#JDialog%28java.awt.Frame,%20java.lang.String,%20boolean,%20java.awt.GraphicsConfiguration%29" rel="nofollow noreferrer">JDialog(...)</a>).</p> <p>Now you can occupy your head with this code, which can be used to determine the appropriate location, that you want to set, again, you might have to use <code>focusGain()</code> method in my opinion, to satisfy condition 2 of your question. Have a look at the code attached, though no need to create a <code>new JFrame/JDialog</code>, just watch how to get coordinates for the screen (that you can add in the <code>focusGain()</code> method to determine the location of the whole Application.)</p> <pre><code>GraphicsEnvironment ge = GraphicsEnvironment. getLocalGraphicsEnvironment(); GraphicsDevice[] gs = ge.getScreenDevices(); for (int j = 0; j &lt; gs.length; j++) { GraphicsDevice gd = gs[j]; GraphicsConfiguration[] gc = gd.getConfigurations(); for (int i=0; i &lt; gc.length; i++) { JFrame f = new JFrame(gs[j].getDefaultConfiguration()); Canvas c = new Canvas(gc[i]); Rectangle gcBounds = gc[i].getBounds(); int xoffs = gcBounds.x; int yoffs = gcBounds.y; f.getContentPane().add(c); f.setLocation((i*50)+xoffs, (i*60)+yoffs); f.show(); } } </code></pre> <p><strong>EDIT 3 :</strong></p> <p>Try to change this :</p> <pre><code>int x = loc.getX() + (mainWindow.getWidth() - getWidth()) / 2; int y = loc.getY() + (mainWindow.getHeight() - getHeight()) / 2; setLocation(x, y); </code></pre> <p>to just : </p> <pre><code>setLocationRelativeTo(mainWindow); </code></pre> <p>To test the above thingy, I used my <code>FrameFocus</code> Class as is, though I had added your changes to my <code>CustomDialog</code> method, as shown in this modified <code>CustomDialog</code> Class.</p> <pre><code>class CustomDialog extends JDialog { private JFrame mainWindow; public CustomDialog(JFrame owner, String title, boolean modal) { super(owner, title, modal); mainWindow = owner; JPanel contentPane = new JPanel(); JLabel dialogLabel = new JLabel( "I am a Label on JDialog.", JLabel.CENTER); contentPane.add(dialogLabel); setContentPane(contentPane); pack(); addFocusListener(new FocusListener() { @Override public void focusGained(FocusEvent e) { //mainWindow.setLocationRelativeTo(null); //setLocationRelativeTo(null); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice[] gs = ge.getScreenDevices(); for (int j = 0; j &lt; gs.length; j++) { GraphicsDevice gd = gs[j]; GraphicsConfiguration[] gc = gd.getConfigurations(); for (int i=0; i &lt; gc.length; i++) { Rectangle gcBounds = gc[i].getBounds(); Point loc = mainWindow.getLocationOnScreen(); if (gcBounds.contains(loc)) { System.out.println("at " + j + " screen"); int x = gcBounds.x + (gcBounds.width - mainWindow.getWidth()) / 2; int y = gcBounds.y + (gcBounds.height - mainWindow.getHeight()) / 2; mainWindow.setLocation(x, y); //x = (int) (loc.getX() + (mainWindow.getWidth() - CustomDialog.this.getWidth()) / 2); //y = (int) (loc.getY() + (mainWindow.getHeight() - CustomDialog.this.getHeight()) / 2); //CustomDialog.this.setLocation(x, y); CustomDialog.this.setLocationRelativeTo(mainWindow); break; } } } } @Override public void focusLost(FocusEvent e) { /* * Nothing written for this part yet */ } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload