2

I'm creating an application that can run on two monitors. For that I created 2 JFrames. The first is my client application that will display information only i have to see. The second (lets call it TweetForm) is a jframe that will show information everybody can see. (probable monitor will be a TV). I've searched how to put this on two seperate screens and found the following solution: Show JFrame in a specific screen in dual monitor configuration

this works just fine, BUT: whenever i'm focus something on my 'mainmonitor' the tweetForm which is displaying on the TV gets minimized. How can I prevent the jframe from minimizing and always be displayed? (even though the first jframe is minimized or not)

CODE FROM 2nd JFRAME

/**
 * Creates new form TweetForm
 */
public TweetForm()  
{
    initComponents();
    dispose();
    setUndecorated(true);
    pack();
    setExtendedState(Frame.MAXIMIZED_BOTH);
    setTitle("Serious Request");
    this.setAlwaysOnTop(true);

    showOnScreen(1, this);
}

public static void showOnScreen( int screen, JFrame frame )
{
    GraphicsEnvironment ge = GraphicsEnvironment
        .getLocalGraphicsEnvironment();
    GraphicsDevice[] gs = ge.getScreenDevices();
    if( screen > -1 && screen < gs.length )
    {
        gs[screen].setFullScreenWindow( frame );
    }
    else if( gs.length > 0 )
    {
        gs[0].setFullScreenWindow( frame );
    }
    else
    {
        throw new RuntimeException( "No Screens Found" );
    }
}
Community
  • 1
  • 1
Jeroen van Veghel
  • 158
  • 1
  • 1
  • 14

2 Answers2

3

Use only one JFrame. Make the other one as JDialog and update data in dialog with respect to you frame.

Also see,

Use of Multiple JFrames.

Working with Multiple JFrames

Community
  • 1
  • 1
Amarnath
  • 8,736
  • 10
  • 54
  • 81
3

Not sure I understand the benefit of using setFullScreenWindow over using setExtendedState(MAXIMIZED) combined with setUndecorated(true). Anyway, wheter I use setFullScreenWindow() or not, I don't observe the behaviour you mention (see example code below, use ESC to exit application).

Any reason for calling dispose() in your constructor? Seems very odd to me.

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;

public class Test2JFrame {

    protected void initUI() {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        int i = 1;
        for (GraphicsDevice gd : ge.getScreenDevices()) {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(createLabel(String.valueOf(i)));
            frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "exit");
            frame.getRootPane().getActionMap().put("exit", new AbstractAction() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
            frame.setLocation(gd.getDefaultConfiguration().getBounds().getLocation());
            frame.setUndecorated(true);
            frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
            frame.setVisible(true);
            gd.setFullScreenWindow(frame);
            i++;
        }
    }

    private JLabel createLabel(String label) {
        JLabel jLabel = new JLabel(label);
        jLabel.setHorizontalAlignment(JLabel.CENTER);
        jLabel.setFont(jLabel.getFont().deriveFont(48.0f));
        jLabel.setFocusable(true);
        return jLabel;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test2JFrame().initUI();
            }
        });
    }

}
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • +1 nice example... but seems like it will use multiple `JFrame`s... wouldn't `JDialog` be better/better practice like @Che has mentioned? – David Kroukamp Jan 30 '13 at 16:27
  • 1
    @DavidKroukamp yes it will use multiple JFrame. For me, if the dialog is not modal, the same conclusions apply than the one on multiple JFrame. So for me, one is not particularly better than the other. The only difference is that a JFrame creates an additional "entry" in the taskbar (at least on Windows) which has its pros and cons. The choice between a JFrame or a JDialog does not make a big difference, in this situation, at least IMHO. – Guillaume Polet Jan 30 '13 at 16:33
  • +1 ... *if the dialog is not modal, the same conclusions apply than the one on multiple JFrame* ahhh as I thought (but did not have multiple screens to test. :)) – David Kroukamp Jan 30 '13 at 16:36
  • 2
    @DavidKroukamp the situation depicted in this question could make the use of multiple frames/dialogs acceptable (mainly because it uses a combination of "alwaysOnTop/Undecorated/Maximized" and multiple screens). – Guillaume Polet Jan 30 '13 at 16:39
  • Ahhh now you bought me over :) – David Kroukamp Jan 30 '13 at 16:41