Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>use <a href="http://docs.oracle.com/javase/7/docs/api/java/awt/Window.html#setLocation%28int,%20int%29" rel="nofollow">JDialog.setLocation()</a> for moving <code>JDialog</code> on desired <a href="http://docs.oracle.com/javase/7/docs/api/java/awt/Point.html" rel="nofollow">Point on the screen</a></p> <pre><code>import java.awt.GridLayout; import java.awt.Point; import java.awt.event.ActionEvent; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.border.EmptyBorder; public class JDialogAtPoint { private JFrame frame = new JFrame(); private JPanel panel = new JPanel(); private JDialog dialog; private Point location; public JDialogAtPoint() { createGrid(); createDialog(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(panel); frame.setLocation(100, 100); frame.pack(); frame.setVisible(true); } private void createGrid() { panel.setLayout(new GridLayout(3, 3, 4, 4)); int l = 0; int row = 3; int col = 3; JButton buttons[][] = new JButton[row][col]; for (int i = 0; i &lt; row; i++) { for (int j = 0; j &lt; col; j++) { buttons[i][j] = new JButton(""); buttons[i][j].putClientProperty("column", i + 1); buttons[i][j].putClientProperty("row", j + 1); buttons[i][j].setAction(updateCol()); panel.add(buttons[i][j]); l++; } } } private void createDialog() { dialog = new JDialog(); dialog.setAlwaysOnTop(true); dialog.setModal(true); dialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE); JPanel pane = (JPanel) dialog.getContentPane(); pane.setBorder(new EmptyBorder(20, 20, 20, 20)); dialog.pack(); } public Action updateCol() { return new AbstractAction("Display JDialog at Point") { private static final long serialVersionUID = 1L; @Override public void actionPerformed(ActionEvent e) { JButton btn = (JButton) e.getSource(); System.out.println("Locations coordinates" + btn.getLocation()); System.out.println("clicked column " + btn.getClientProperty("column") + ", row " + btn.getClientProperty("row")); if (!dialog.isVisible()) { showingDialog(btn.getLocationOnScreen()); } } }; } private void showingDialog(final Point loc) { dialog.setVisible(false); location = loc; int x = location.x; int y = location.y; dialog.setLocation(x, y); Runnable doRun = new Runnable() { @Override public void run() {//dialog.setLocationRelativeTo(frame); dialog.setVisible(true); } }; SwingUtilities.invokeLater(doRun); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { JDialogAtPoint cf = new JDialogAtPoint(); } }); } } </code></pre>
 

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