-1

I have the following code to call a new frame from a button:

final JButton btnFontHelp = new JButton("Font Help");
    btnFontHelp.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            Framefont fr = new Framefont();
            fr.setBounds(100, 150, 400, 170);
            fr.setVisible(true);

        }
    });

The fr.setBounds(100, 150) is absolute position based on the screen and not based on the mainframe. Is there any way to know the absolute position of the main screen, so that the child frame can appear at a certain position based on the mainsframe and not based on the position of the screen?

  • For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson Feb 21 '15 at 01:44
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Feb 21 '15 at 01:51
  • I prefer to use multiple Jframe as when I use Jpanel, it will cause blink and does not work properly when I use Miglayout. – lion blue sky Feb 22 '15 at 05:57
  • *"..when I use Jpanel, it will cause blink.."* Then you're obviously doing it wrong. Better to figure out what is going wrong than force multiple frames on the end user. – Andrew Thompson Feb 22 '15 at 06:22

2 Answers2

1

In the ActionListener you can add code like:

JButton button = (JButton)arg0.getSource();
Window frame = SwingUtilities.windowForComponent( button );
Rectangle bounds = frame.getLocation();

Now you can position your child window relative to its parent using the location/size information in the Rectangle.

Of course if the parent window is moved the child window will remain in its current location. If you want the child window to move you would need to add a ComponentListener to the frame and handle the componentMoved() event to reposition the child window.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Hi camickr, when I implemented your code, it does not work either. In fact, Rectangle cannot be implemented and should be changed into Point. Can you give a further help or explanation? Here is my code after implementing your suggestion: final JButton btnFontHelp = new JButton("Font Help"); btnFontHelp.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { Framefont fr = new Framefont(); fr.setBounds(100, 150, 400, 170); fr.setVisible(true); } }); – lion blue sky Feb 22 '15 at 05:54
  • Here is my code after implementing your suggestion: final JButton btnFontHelp = new JButton("Font Help"); btnFontHelp.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) {JButton btnFontHelp = (JButton)arg0.getSource(); Window frame = SwingUtilities.windowForComponent( btnFontHelp ); Point bounds = frame.getLocation(); Framefont fr = new Framefont(); fr.setBounds(100, 150, 400, 170); fr.setVisible(true); } }); – lion blue sky Feb 22 '15 at 05:59
  • ` Rectangle cannot be implemented and should be changed into Point.` you need the Rectangle because you need to know the location and size of the parent frame so you can position the child frame relative to the parent frame. Post a proper [SSCCE](http://sscce.org/) demonstrating the problem. A few lines of code in the comments doesn't help. – camickr Feb 22 '15 at 21:21
0

Instead of:

fr.setBounds(100, 150, 400, 170);

Try this:

fr.setLocationRelativeTo(parentFrame);
  • Hi Arvind, thank for your answer but it will put the childframe in the center of the parentframe. What I want is to put the childframe at a specific position of its parentframe, for example 200, 300. – lion blue sky Feb 21 '15 at 01:07
  • @lionbluesky, did you tried my suggestion? Did it worked for you? –  Feb 21 '15 at 01:08
  • I commented after I tried your suggestion and it position the new frame at the center of the mainframe – lion blue sky Feb 21 '15 at 01:23