Question

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" );
    }
}
Était-ce utile?

La solution

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

Autres conseils

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();
            }
        });
    }

}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top