User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 397,612 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,539 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 738 | Replies: 9
Reply
Join Date: Jul 2007
Location: iasi
Posts: 27
Reputation: claudiu_is is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
claudiu_is's Avatar
claudiu_is claudiu_is is offline Offline
Light Poster

how to center a dialog window?

  #1  
Sep 26th, 2007
Hy,
I have a JFrame main window and a Dialog window, how can i use the method setPositionRelativeTo() so that my Dialog window positions centered on the main window ?
Thanks
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 2,639
Reputation: Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light 
Rep Power: 11
Solved Threads: 261
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Maven

Re: how to center a dialog window?

  #2  
Sep 26th, 2007
If you pass the JFrame reference as the "owner" parameter to the JDialog constructor it will center on it. Example:
        JFrame frame = new JFrame();
        frame.setBounds(200,200,300, 400);
        frame.setVisible(true);
        
        JDialog dialog = new JDialog(frame,"test");
        dialog.setVisible(true);
Reply With Quote  
Join Date: Jul 2007
Location: iasi
Posts: 27
Reputation: claudiu_is is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
claudiu_is's Avatar
claudiu_is claudiu_is is offline Offline
Light Poster

Re: how to center a dialog window?

  #3  
Sep 26th, 2007
my Dialog class is used inside the main window class, so i`m using "this" in Dialog`s object constructor, but the Dialog window is not centered;
please let me know if you want to post the code
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 2,639
Reputation: Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light 
Rep Power: 11
Solved Threads: 261
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Maven

Re: how to center a dialog window?

  #4  
Sep 26th, 2007
Originally Posted by claudiu_is View Post
my Dialog class is used inside the main window class, so i`m using "this" in Dialog`s object constructor, but the Dialog window is not centered;
please let me know if you want to post the code

Yes, the code would probably help. If your main window class extends JFrame and is valid and visible when the JDialog is shown, then using "this" for the owner should be all that is needed.
Reply With Quote  
Join Date: Jul 2007
Location: iasi
Posts: 27
Reputation: claudiu_is is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
claudiu_is's Avatar
claudiu_is claudiu_is is offline Offline
Light Poster

Re: how to center a dialog window?

  #5  
Sep 26th, 2007
The Dialog class - 231(see the 1st constructor)
the dialog window is created at line 210


  1.  
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.io.*;
  6. import java.io.FileOutputStream;
  7. import java.io.FileInputStream;
  8. import java.io.PrintStream;
  9. import java.io.DataInputStream;
  10. import java.lang.*;
  11. import java.lang.String;
  12.  
  13.  
  14. public class MyMain extends JFrame implements ActionListener, TextListener{
  15. static String in = " ", aux = "", uChar = "", uChar2 = "";
  16. public static String cuvant = "";
  17. public static String cuvant2 = "";
  18. public static String raspuns = "no";
  19. static int v[]; //contine 0-litera neintoarsa si 1-litera intoarsa
  20. static int nrLinii, s;
  21. static int gen = 0;
  22. public static int pas = 6;//vietzi
  23. static char c;
  24. static int nrJocT = 0;
  25. public AskWindow askW;
  26.  
  27. boolean ok, ok2;
  28. static JLabel j, j2, j3;
  29. TextField t;
  30. JPanel p1, x, y;
  31. static JPanel p2;
  32. JButton b;
  33. Image img;
  34. static DrawingPanel drawing_panel;
  35. public MyMain(String titlu) {
  36. super(titlu);
  37. setSize(220, 320);
  38. // MetodasetLayoutnuseaplicadirectferestrei
  39. getContentPane().setLayout(new BorderLayout());
  40. // ComponenteleaudenumiriceincepculiteraJ
  41. // TextulpoatefisiinformatHTML
  42. b = new JButton("Cauta");
  43. b.setEnabled(false);
  44. b.addActionListener(this);
  45.  
  46. img = Toolkit.getDefaultToolkit().getImage("Hangman-0 (192 x 192).png");
  47. drawing_panel = new DrawingPanel (img);
  48.  
  49. p2 = new JPanel();
  50. j = new JLabel("");
  51. j2 = new JLabel("");
  52. j3 = new JLabel("");
  53. j.setHorizontalAlignment(SwingConstants.CENTER);
  54. j2.setHorizontalAlignment(SwingConstants.CENTER);
  55. j3.setHorizontalAlignment(SwingConstants.CENTER);
  56. p2.setLayout(new BorderLayout());
  57. p2.add(drawing_panel, BorderLayout.CENTER);
  58. p2.setBorder(BorderFactory.createLineBorder(Color.BLACK));
  59. //p2.setSize(300, 200);
  60. p1 = new JPanel();
  61. p1.setLayout(new FlowLayout());
  62. p2.setPreferredSize(new Dimension(220, 200));
  63. p1.setPreferredSize(new Dimension(220, 100));
  64. t = new TextField(1);
  65. t.addTextListener(this);
  66. x = new JPanel();
  67. y = new JPanel();
  68. x.setLayout(new BorderLayout());
  69. y.setLayout(new FlowLayout());
  70. x.add(j, BorderLayout.NORTH);
  71. x.add(j2, BorderLayout.CENTER);
  72. x.add(j3, BorderLayout.SOUTH);
  73. y.add(t);
  74. y.add(b);
  75. p1.add(y, BorderLayout.NORTH);
  76. p1.add(x, BorderLayout.SOUTH);
  77. p1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
  78. getContentPane().add(p1, BorderLayout.SOUTH);
  79. getContentPane().add(p2, BorderLayout.CENTER);
  80. pack();
  81. //setSize(220,300);
  82. this.setLocation(50, 50);
  83. rep();
  84. //afisNrJocT();
  85.  
  86.  
  87. }
  88.  
  89. public static void rep(){
  90. cuvant2 = "";
  91. pas = 6;
  92. getWord();
  93. for (int i=0;i<cuvant.length();i++)
  94. if(v[i] == 1)
  95. cuvant2 += cuvant.charAt(i) + " ";
  96. else
  97. cuvant2 += "_ ";
  98. j.setText(cuvant2);
  99. j2.setText("");
  100. j3.setText("Introduceti o litera");
  101.  
  102. }
  103. public void afisNrJocT(){
  104. askW = new AskWindow(this, false);
  105. }
  106.  
  107. public void textValueChanged(TextEvent e){
  108. in = t.getText();
  109. if (in.length() != 0){
  110. do{
  111. if((in.charAt(0) < 65) || (in.charAt(0) > 122))
  112. t.setText("");
  113. in = t.getText();
  114. }
  115. while(in.length()!=0 && (in.charAt(0) < 65 || in.charAt(0) > 122) );//sa fie litera
  116. b.setEnabled(true);}
  117. else
  118. b.setEnabled(false);
  119. if (in.length() > 1){
  120. t.setText("" + in.charAt(0));
  121. t.setCaretPosition(5);
  122. }
  123. }
  124.  
  125. private static void getWord() {
  126. try {// numarare cuvinte din fisier(un cuvant/linie)
  127. FileInputStream input = new FileInputStream("cuvinte.txt");
  128. DataInputStream dIn = new DataInputStream(input);
  129. while ((aux = dIn.readLine()) != null) {
  130. nrLinii++;
  131. }
  132. } catch (Exception e) {
  133. System.err.println("nu pot citi din fish1");
  134. }
  135.  
  136. gen = (int)(Math.random()*nrLinii+1);//numar random pt alegerea random a cuv din fisier
  137. int nrL = 0;
  138. s = 0;
  139. try{//citire aleatoare a unui cuvant din fisier
  140. FileInputStream input = new FileInputStream("cuvinte.txt");
  141. DataInputStream dIn = new DataInputStream(input);
  142. while ((aux = dIn.readLine())!=null){
  143. nrL++;
  144. if (gen == nrL){
  145. cuvant ="";
  146. cuvant += aux;
  147. }
  148. }
  149. }
  150. catch(Exception e){
  151. System.err.println("nu pot citi din fish2");}
  152. v = new int[cuvant.length()];
  153. for (int i=1; i<cuvant.length()-1;i++){v[i] = 0;}
  154. v[0] = 1;
  155. for (int i=1;i < cuvant.length()-1;i++)//afisare primei si ultimei litere, precum si a celor din interiorul cuvantului similare cu acestea
  156. if(cuvant.charAt(0) == cuvant.charAt(i) || (i!=cuvant.length()-1 && cuvant.charAt(cuvant.length()-1) == cuvant.charAt(i)))
  157. v[i] = 1;
  158. v[cuvant.length()-1] = 1;
  159.  
  160. for (int i=0; i < cuvant.length(); i++){//calcularea nr de litere nedescoperite
  161. if (v[i] == 0)
  162. s++;
  163. }
  164. }
  165.  
  166. public void actionPerformed(ActionEvent e){
  167. t.setText("");
  168. t.requestFocusInWindow();
  169. ok = false;
  170. for (int i=0; i<cuvant.length();i++){
  171. if(cuvant.charAt(i)
  172. == Character.toLowerCase(in.charAt(0))) {//daca litera citita este in cuvant
  173. if(v[i] != 1){//daca e nedescoperita
  174. v[i] = 1;//se descopera
  175. s--;//scade nru de lit nedescoperite
  176. }//daca era deja descoperita, nu tbia ca 's' sa scada
  177. ok = true;//litera gasita
  178. }
  179. }
  180. if(ok == false){//litera negasita
  181.  
  182. ok2 = false;
  183. for (int i=0; i<uChar.length(); i++)
  184. if (uChar.charAt(i) == Character.toLowerCase(in.charAt(0)))
  185. ok2 = true;
  186.  
  187. if(ok2 == false){
  188. uChar += in.toLowerCase();//se adauga la vectorul de litere negasite
  189. pas--;
  190. drawing_panel = null;
  191. drawing_panel = new DrawingPanel(Toolkit.getDefaultToolkit().getImage("Hangman-" +(6-pas)+ " (192 x 192).png"));
  192. p2.add(drawing_panel, BorderLayout.CENTER);
  193. }
  194. }
  195. cuvant2 = "";
  196. for (int i=0; i<cuvant.length();i++){//afisarea cuvantului actualizat
  197. if(v[i] == 0)
  198. cuvant2 += ("_ ");
  199. else
  200. cuvant2 += cuvant.charAt(i) + " ";
  201. }
  202. if (pas == 0){afisNrJocT();}
  203.  
  204. j.setText(cuvant2);
  205. //for (int j = 0;j < uChar.length();j++)//afisare litere negasite
  206. j2.setText(uChar);
  207. if(s == 0){
  208. j3.setText("joc terminat");
  209. nrJocT++;
  210. askW = new AskWindow(this, "Continuati jocul?", true);
  211. }
  212. else
  213. j3.setText("Mai ai "+pas+" pasi pana la moarte!");
  214.  
  215.  
  216. }
  217. public static void main(String args[]){
  218. MyMain f;
  219. f = new MyMain("test");
  220. f.addWindowListener (new WindowAdapter () {
  221. public void windowClosing (WindowEvent event)
  222. {
  223. System.exit (0);
  224. }
  225. });
  226. f.show();
  227.  
  228.  
  229. }
  230.  
  231. public class AskWindow extends Dialog implements ActionListener{
  232. private TextField text;
  233. private Button ok, ko, cancel;
  234.  
  235. public AskWindow(Frame parinte, String titlu, boolean modala){
  236. super(parinte, titlu, modala);
  237. this.addWindowListener(new WindowAdapter(){
  238. public void windowClosing(WindowEvent e){
  239. MyMain.raspuns = null;
  240. dispose();
  241. }
  242. });
  243.  
  244. Panel panel = new Panel();
  245. ok = new Button("OK");
  246. cancel = new Button("Cancel");
  247. panel.add(ok);
  248. panel.add(cancel);
  249. add(panel,BorderLayout.SOUTH);
  250. pack();
  251. ok.addActionListener(this);
  252. cancel.addActionListener(this);
  253. show();
  254. }
  255. public AskWindow(Frame parinte, boolean modala){
  256. super(parinte, modala);
  257. JLabel l;
  258. int t = 5;
  259. this.addWindowListener(new WindowAdapter(){
  260. public void windowClosing(WindowEvent e){
  261. MyMain.raspuns = null;
  262. dispose();
  263. }
  264. });
  265. Panel panel = new Panel();
  266. ko = new Button("OK");
  267. l = new JLabel();
  268. l.setText("ati castigat de "+ nrJocT + " ori");
  269. panel.add(l);
  270. panel.add(ko);
  271. add(panel, BorderLayout.SOUTH);
  272. pack();
  273. ko.addActionListener(this);
  274. show();
  275. for (int i=0; i < t; i++)
  276. {
  277. ko.setLabel("" + (t-i));
  278. try{
  279. Thread.sleep(1000);
  280. }
  281. catch(InterruptedException o){}
  282. }
  283.  
  284. }
  285.  
  286. public void actionPerformed(ActionEvent e){
  287. Object sursa = e.getSource();
  288. int t = 5;
  289. if (sursa == ok){
  290. MyMain.raspuns = "yes";
  291. MyMain.rep();
  292. dispose();
  293. }
  294. else
  295. if (sursa == cancel){
  296. MyMain.raspuns = "no";
  297. dispose();
  298. afisNrJocT();
  299. }
  300. else
  301. System.exit(0);
  302.  
  303. }
  304.  
  305. }
  306.  
  307. class DrawingPanel extends JPanel {
  308. Image img;
  309.  
  310. DrawingPanel(Image img) {
  311. this.img = img;
  312. }
  313.  
  314. public void paintComponent(Graphics g) {
  315. super.paintComponent(g);
  316. g.drawImage(img, 5, 5, this);
  317.  
  318. }
  319.  
  320. }
  321. }
  322.  
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 2,639
Reputation: Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light 
Rep Power: 11
Solved Threads: 261
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Maven

Re: how to center a dialog window?

  #6  
Sep 26th, 2007
The dialog is centered in the frame when I click the button. Where is it appearing for you?
Reply With Quote  
Join Date: Jul 2007
Location: iasi
Posts: 27
Reputation: claudiu_is is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
claudiu_is's Avatar
claudiu_is claudiu_is is offline Offline
Light Poster

Re: how to center a dialog window?

  #7  
Sep 26th, 2007
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 2,639
Reputation: Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light 
Rep Power: 11
Solved Threads: 261
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Maven

Re: how to center a dialog window?

  #8  
Sep 26th, 2007
Well, after finding this bug report http://bugs.sun.com/bugdatabase/view...bug_id=4102292 I tried running your app on the oldest JDK I have here, 1.4.2, and it still functions fine for me. The dialog is always centered in the frame. Are you running this in Windows or some other OS? I'm at a loss as to the cause if you are using Windows and a recent JDK.
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 2,639
Reputation: Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light 
Rep Power: 11
Solved Threads: 261
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Maven

Re: how to center a dialog window?

  #9  
Sep 26th, 2007
Have you tried adding
setLocationRelativeTo(parinte);
after the super() call or before show() in your dialog constructor already? You mentioned the method in your original post but not whether you had used it.
Reply With Quote  
Join Date: Jul 2007
Location: iasi
Posts: 27
Reputation: claudiu_is is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
claudiu_is's Avatar
claudiu_is claudiu_is is offline Offline
Light Poster

Re: how to center a dialog window?

  #10  
Sep 26th, 2007
I`m using Windows XP and JDK 1.6.0_02

it almost worked with setLocationRelativeTo(parinte) except that it isn`t exactly in the center, the top bottom corner of the dialog window is exactly on the center of the main window; earlier i tried to use that function, but i`ve made a mistake, i`ve used the "this" argument, my mistake, i`m a newbie in Java; thanks 4 your help
Last edited by claudiu_is : Sep 26th, 2007 at 2:09 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Java Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 7:48 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC