1

Is it possible to set up two different JFrames and show them side by side? Without use Internalframe, multiple Jpanels etc. .

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Michal
  • 65
  • 2
  • 9
  • 1
    Take a look at JFrame - [setLocation](http://www.java2s.com/Code/JavaAPI/javax.swing/JFramesetLocationintxinty.htm) method, which combined with setting size of frames, can position both side by side – makciook Jan 29 '13 at 18:02
  • Just to clarify: Do you mean two application windows side by side, or two sections (in one application window) side by side? – thatidiotguy Jan 29 '13 at 18:02
  • 1
    Not everything possible is wise. Seems like you want one JFrame with two JPanels side by side. – Gilbert Le Blanc Jan 29 '13 at 18:11
  • @makciook Yes, it can be do as you said. But is there another posibility? – Michal Jan 29 '13 at 18:15
  • @thatidiotguy I mean 2 jframes – Michal Jan 29 '13 at 18:18
  • @GilbertLeBlanc I think about that, bu i am interested different method now – Michal Jan 29 '13 at 18:21
  • 3
    I really would consider @GilbertLeBlanc 's advice especially considering the answers provided in [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice). – Guillaume Polet Jan 29 '13 at 23:22

3 Answers3

0

1st position your frames on each screen devices.

frame1.setLocation(pointOnFirstScreen);
frame2.setLocation(pointOnSecondScreen);

working example:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class GuiApp1 {
protected void twoscreen() {
Point p1 = null;
Point p2 = null;
for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment ().getScreenDevices()) {
    if (p1 == null) {
        p1 = gd.getDefaultConfiguration().getBounds().getLocation();
    } else if (p2 == null) {
        p2 = gd.getDefaultConfiguration().getBounds().getLocation();
    }
}
if (p2 == null) {
    p2 = p1;
}
createFrameAtLocation(p1);
createFrameAtLocation(p2);
 }

 private void createFrameAtLocation(Point p) {
final JFrame frame = new JFrame();
frame.setTitle("Test frame on two screens");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
final JTextArea textareaA = new JTextArea(24, 80);
textareaA.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
panel.add(textareaA, BorderLayout.CENTER);
frame.setLocation(p);
frame.add(panel);
frame.pack();
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
frame.setVisible(true);
 }

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {


    public void run() {
        new GuiApp1().twoscreen();
    }
});
  }

}
joey rohan
  • 3,505
  • 5
  • 33
  • 70
0

Yes, as follows;

JFrame leftFrame = new JFrame();

// get the top left point of the left frame
Point leftFrameLocation = leftFrame.getLocation();
// then make a new point with the same top (y) and add the width of the frame (x)
Point rightFrameLocation = new Point(
            leftFrameLocation.x + leftFrame.getWidth(),
            leftFrameLocation.y);

JFrame rightFrame = new JFrame();
rightFrame.setLocation(rightFrameLocation); // and that's the new location
Peter Wanden
  • 316
  • 2
  • 6
0

Yes, here is a very simple example; for my own uses I do the same thing with non-modal JDialogs.

    public static void main(String[] args) {
        JFrame frameL = new JFrame("Left Frame");
        frameL.setSize(400, 200); // this size arbitrary; set how you need.
        frameL.setLocationRelativeTo(null);
        frameL.setVisible(true);

        Point p = frameL.getLocationOnScreen();
        Dimension d = frameL.getSize();

        JFrame frameR = new JFrame("Right Frame");
        frameR.setSize(300, 160); // this size arbitrary; set how you need.
        frameR.setLocation((p.x + d.width), p.y);
        frameR.setVisible(true);

        while(frameL.isVisible()) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.exit(0);
    }
selofain
  • 11
  • 4