JDIalog, HTML Code, setLocationRelativeTo; not centered

[nobr]Hi,

I am building my own modal dialog; it's a JLabel with formatted HTML Code - I only use the bold <b> and break

tag in the default font size and -type.

I use setLocationRelativeTo() to center the dialog to my main JFrame; however, the dialog is not centered, it's shifted down to the right. Even if I just use some letters in my HTML code the dialog isn't centered. Here is my code:

publicclass TDDialogextends JDialogimplements ActionListener, WindowListener{

private JPanel exercisePanel, containerPanel;

private JLabel exerciseLabel;

private JButton readyButton =new JButton("Ready");

private JButton showSolutionButton =new JButton("Solution");

private JTextField solutionTextfield;

private String solutionString;

private Font font =new Font("SansSerif", Font.PLAIN, 12);

public TDDialog(Frame ownerFrame){

super(ownerFrame,true);

addWindowListener(this);

setTitle("Exercise");

setLayout(new BorderLayout());

exerciseLabel =new JLabel();

exerciseLabel.setFont(font);

exercisePanel =new JPanel();

exercisePanel.add(exerciseLabel);

solutionTextfield =new JTextField();

solutionTextfield.addActionListener(this);

containerPanel =new JPanel();

containerPanel.setLayout(new BorderLayout());

containerPanel.add(readyButton, BorderLayout.LINE_START);

containerPanel.add(showSolutionButton, BorderLayout.CENTER);

readyButton.setFont(font);

showSolutionButton.setFont(font);

readyButton.addActionListener(this);

showSolutionButton.addActionListener(this);

exerciseLabel.setText("<html><body>3 Lines<br><b>3 Lines</b><br>3 Lines<br></body></html>");

add(exercisePanel, BorderLayout.PAGE_START);

add(solutionTextfield, BorderLayout.CENTER);

add(containerPanel, BorderLayout.PAGE_END);

setResizable(false);

pack();

setLocationRelativeTo(ownerFrame);

setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

}

// action- and window-listeners...

Am I building the JDialog correctly? Is there a better, a more recommended or "nicer" way? Why isn't hte dialog centered?

Edit: I really would like to assign Duke Stars to my question, however, since the last forum maintenance, I can't do this any more "Server Error

This server has encountered an internal error which prevents it from fulfilling your request. The most likely cause is a misconfiguration. Please ask the administrator to look for messages in the server's error log." This happens every time at different PCs with different browsers / versions / proxy-settings.

Message was edited by:

SFL[/nobr]

[3955 byte] By [SFLa] at [2007-11-27 10:45:01]
# 1

> Edit: I really would like to assign Duke Stars to my question,

Don't worry about Dukes. If you post a real SSCCE you will get more help. The posted code is not executable. You don't have the code to build your frame and invoke the dialog.

> Even if I just use some letters in my HTML code the dialog isn't centered

Are you implying that it works with setText("aaa") but not setText("<html>aaa</html>")?

The only suggestion I would have is to move the setResizable() method after the pack();

camickra at 2007-7-28 20:10:27 > top of Java-index,Desktop,Core GUI APIs...
# 2

Here is the missing part for a SSCCE; I noticed that the modal dialog is centered in relation to the screen, not to the "ownerFrame", although I pass ownerFrame to the setRelativeTo statement.

import java.awt.Dimension;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class GUI_Test extends JComponent implements MouseListener {

private static final long serialVersionUID = 1;

private JPanel pmPanel = new JPanel();

private TDDialog tdd = new TDDialog(GUI_Test.frame);

private static JFrame frame;

public GUI_Test() {

addMouseListener(this);

add(pmPanel);

}

private static void createAndShowGUI() {

JFrame.setDefaultLookAndFeelDecorated(true);

frame = new JFrame();

frame.setTitle("GUI_Test");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

GUI_Test newContentPane = new GUI_Test();

newContentPane.setOpaque(true);

frame.setPreferredSize(new Dimension(500, 500));

frame.setResizable(false);

frame.setContentPane(newContentPane);

frame.pack();

frame.setVisible(true);

}

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGUI();

}

});

}

public void mouseClicked(MouseEvent arg0) {

tdd.setVisible(true);

}

public void mouseEntered(MouseEvent arg0) {}

public void mouseExited(MouseEvent arg0) {}

public void mousePressed(MouseEvent arg0) {}

public void mouseReleased(MouseEvent arg0) {}

}

Thanks for your help!

SFLa at 2007-7-28 20:10:27 > top of Java-index,Desktop,Core GUI APIs...
# 3

> Here is the missing part for a SSCCE;

No, it is still not a SSCCE.

First of all it doesn't compile. The TDDialog is incomplete. It doesn't have code for any of the listeners. That code is not necessary anyway since your question has nothing to do about listeners. You question is about centering the dialog. So get rid of all that code in the SSCCE. Its not relevant and we don't want to read through the code.

A SSCCE is a really really really simple program that shows what you are attempting to do in the simplest way possible. You write the SSCCE to prove you understand the basic concept of what you are trying to do. Once you understand the basic concept you incorporate what you have learned into you read program. So again all the JComponents in your dialog are irrelevant and just clutter the code.

Here is a SSCCE:

import javax.swing.*;

public class Test

{

public static void main(String args[])

{

JFrame frame = new JFrame();

frame.setBounds(100, 100, 500, 500);

frame.setVisible(true);

JDialog dialog = new JDialog();

dialog.setSize(300, 300);

dialog.setLocationRelativeTo(frame);

dialog.setVisible(true);

}

}

Now tell me what is different between my code and your code? Why does my code work?

camickra at 2007-7-28 20:10:27 > top of Java-index,Desktop,Core GUI APIs...
# 4

camickr, keep cool ;) After my opinion it doesn't make that sense to provide a SSCCE without showing you what I am doing in my code.

Well... I still can't find the error. "ownerFrame" contains the correct frame; this frame is being displayed.

Thanks a lot for your help!

SFLa at 2007-7-28 20:10:27 > top of Java-index,Desktop,Core GUI APIs...
# 5

> camickr, keep cool

Don't tell me to keep cool. Twice you've been asked for a SSCCE to demonstrate what you are doing and to prove that you understand what you are doing.

You post code that doesn't compile and you have yet to produce a SSCCE that demonstrates what you are doing. If you want me to spend time helping, then you spend time proving you understand what you are doing.

Here is a SSCCE that demonstrate what your code is doing:

import javax.swing.*;

public class Test

{

public static void main(String args[])

{

// create a static variable initialised to null

JFrame frame = null;

// create the dialog and invoke the code in the construtor

JDialog dialog = new JDialog();

dialog.setSize(300, 300);

dialog.setLocationRelativeTo(frame);

// create and show the frame

frame = new JFrame();

frame.setBounds(100, 100, 500, 500);

frame.setVisible(true);

// click on button and show dialog

dialog.setVisible(true);

}

}

Again, I ask what is the difference in order of execution of the code? Where did you set the location of the dialog? Now you have simple code to experiment with. It only took 2 minutes to create this.

Again, the whole point of a SSCCE is to simplify the problem.

camickra at 2007-7-28 20:10:27 > top of Java-index,Desktop,Core GUI APIs...
# 6

> Where did you set the location

> of the dialog?

My whole GUI is being drawn starting at (0, 0). When I call the JDialog I set the location using setLocationRelativeTo(ownerFrame). In the API I read the following:

public void setLocationRelativeTo(Component c)

(...)

If the component is not currently showing, or c is null, the window is placed at the center of the screen. (...)

I can't see a difference between my and your example...?

SFLa at 2007-7-28 20:10:27 > top of Java-index,Desktop,Core GUI APIs...
# 7

> If the component is null, the window is placed at the center of the screen

Exactly. This is whats happening to you. So the question is why is the component null?

I gave you two examples with different ordering.

> When I call the JDialog I set the location using setLocationRelativeTo(ownerFrame)

I don't care about your example. Its too confusing.

I gave you two really simple examples, with the order of code execution slightly changed.

Do you understand the differences in the order of execution? Did you read the comment at the top of my second example? Did you add any System.out.println(...) statements in your program to display the value of any of the variable used?

camickra at 2007-7-28 20:10:27 > top of Java-index,Desktop,Core GUI APIs...
# 8

I now had time enough to review my code and corrected my mistake. Thanks and sorry, camickr!

I often just declare an object without doing the instantiation and initialization :-/

The JDialog now centers - but still not correctly, it's shifted a little bit to the top-left corner of the frame. Is this due to the HTML code or another layout-error?

SFLa at 2007-7-28 20:10:27 > top of Java-index,Desktop,Core GUI APIs...
# 9

> Is this due to the HTML code or another layout-error?

No idea. You still haven't posted a SSCCE showing the problem.

camickra at 2007-7-28 20:10:27 > top of Java-index,Desktop,Core GUI APIs...