0

I will launch Window2 after I click a button. It's not launched immediately but after 3 seconds. What I want is to show it on the top of the screen. It works if the Window1 is still on the screen but fails if I minimize Window1.

The code is here:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TopTest {
    public static void main(String[] args) {
        TopTest test = new TopTest();
        test.createUI();
    }

    public void createUI(){
        JFrame frame = new JFrame("Window1");
        JPanel panel = new JPanel();
        JButton button = new JButton("show");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                Window window = new Window();
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                window.createUI();
            }
        });
        panel.add(button);
        frame.add(panel,BorderLayout.CENTER);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    class Window {
        public void createUI(){
            JFrame frame = new JFrame("Window2");
            frame.setExtendedState(Frame.MAXIMIZED_BOTH);
            frame.add(new MainPanel());
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }

        @SuppressWarnings("serial")
        class MainPanel extends JPanel{
            @Override
            public Dimension getPreferredSize() {
                // TODO Auto-generated method stub
                return new Dimension(800,600);
            }

            protected void paintComponent(Graphics g){
                g.setFont(new Font("Arial", Font.BOLD, 50));
                g.drawString("I am Window2", 200, 200);
            }
        }
    }
}

Thanks for your help in advance.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Eugene
  • 10,627
  • 5
  • 49
  • 67

1 Answers1

1

Add this line to the createGUI method

frame.setAlwaysOnTop(true);
Jens
  • 67,715
  • 15
  • 98
  • 113
  • 2
    @upma 1. `class Window {` should be `class MyWindow {` (potential conflict with `java.awt.Window`) 2. `JFrame frame = new JFrame("Window2");` should be `JDialog frame = new JDialog("Window2");` (then works AlwaysOnTop or Modality) 3. Thread.sleep(3000); should be Swing Timer (doesn't block repainting) – mKorbel Jul 24 '14 at 06:42
  • @mKorbel. You are so nice and I will pay attention on the advice raised by you. Thanks a lot. – Eugene Jul 24 '14 at 07:07
  • @upma :-) but you totally ignored the output from answer by MadProgrammer (yesterday question) in this posts – mKorbel Jul 24 '14 at 08:07
  • @mKorbel . Did you say http://stackoverflow.com/q/24904006/3378204 ? Yeah , his method to create rendered text is brilliant. I just want to show a demo as simple as possible so that I didn't use it in this post. :-) – Eugene Jul 24 '14 at 08:59