-1

I have a main JFrame (1), after that I want to put a JDialog (2) below (same left border position):

dialog2.setLocation(frame.getX(), frame.getY() + frame.getHeight());

and put another JDialog (3) next to the right (same top border position):

dialog3.setLocation(frame.getX() + frame.getWidth(), frame.getY());

But the display has some strange padding among these frame & dialogs:

enter image description here

Test code:

public class Test {
    public static void main(String[] args) throws Exception {
        JFrame mainFrame = new JFrame();
        mainFrame.setSize(400, 400);
        mainFrame.setVisible(true);

        JFrame dialog = new JFrame(); // JDialog dialog = new JDialog();
        dialog.setSize(400, 400);
        dialog.setLocation(mainFrame.getX() + mainFrame.getWidth(), mainFrame.getY());
        dialog.setVisible(true);

        JFrame dialog1 = new JFrame(); // JDialog dialog1 = new JDialog();
        dialog1.setSize(400, 400);
        dialog1.setVisible(true);
        dialog1.setLocation(mainFrame.getX(), mainFrame.getY() + mainFrame.getHeight());
    }
}

Can anybody explain why and suggestion solution that makes those get closer without any padding?

yelliver
  • 5,648
  • 5
  • 34
  • 65

2 Answers2

2

Instead of using multiple JFrame's, why not use JPanel's instead?

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(Toolkit.getDefaultToolkit().getScreenSize());
    frame.getContentPane().setLayout(new BorderLayout());

    JPanel sidePanel = new JPanel(new BorderLayout());

    JPanel buttonPanel = new JPanel(new GridLayout(0,3));
    for(int i = 0; i < 6; i++) {
        JButton button = new JButton("Button " + (i+1));
        button.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
        buttonPanel.add(button);
    }
    buttonPanel.setBorder(BorderFactory.createLineBorder(Color.gray));

    JTextArea log = new JTextArea();
    for(int i = 0; i < 50; i++) {
        log.setText(log.getText() + "line " + (i+1) + "\n");
    }

    sidePanel.add(new JScrollPane(log), BorderLayout.CENTER);
    sidePanel.add(buttonPanel, BorderLayout.NORTH);

    JPanel browserPanel = new JPanel(new GridLayout());

    // code for browser

    frame.getContentPane().add(browserPanel, BorderLayout.CENTER);
    frame.getContentPane().add(sidePanel, BorderLayout.WEST);

    frame.setVisible(true);

This is what it ends up looking like:

cat

nyxaria
  • 479
  • 1
  • 3
  • 13
  • (1-) Use the proper add(...) method. The add(String, Component) method has been obsolete for over a decade, since JDK 1.1. – camickr Aug 25 '16 at 00:07
  • This is my very simple example, but if you create a program that contains many forms, you cannot stuff all of them into an only main form – yelliver Aug 25 '16 at 02:53
  • @yelliver there is always a way with layouts.. if indeed it is that complicated, look up GridBagLayout – nyxaria Aug 25 '16 at 08:05
  • @nyxaria do you know photoshop? are all funcitons shown in one ONLY form? anyway, it's not related to this question, I am asking about JFrame position – yelliver Aug 25 '16 at 08:16
  • @yelliver yes I've had my share of trying to replicate it in native Java, and I just set the toolbox to the left of the screen as seen above. If you do decide to use JFrame's, you will also need to handle the focus events as the toolbox will have the property always on top.. – nyxaria Aug 25 '16 at 08:54
0

Try using the getLocationOnScreen() on the frame instead of getX and getY

user489041
  • 27,916
  • 55
  • 135
  • 204