Gelidus
Gelidus

Reputation: 28

How can I force JFrame to stay in the background while moving?

I made a sliding JFrame "app". The code is simple and short, because it is just a test, before i put the code into my main project.

It is almost working perfectly for me. There is only one problem, when the frame sliding out it goes on top, but i want it to stay in the background, behind the main window. (There will be buttons on it, so i will have to use this frame after it comes out)

Here is the code of the main window:

public class Window {

    private JFrame frame;
    static Slider s;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Window window = new Window();
                    window.frame.setVisible(true);
                    s = new Slider();
                    s.setVisible(false);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Window() {
        initialize();
    }

    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 501, 414);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
        JButton button = new JButton(">>>>");
        frame.getContentPane().add(button, BorderLayout.EAST);

        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                float x = frame.getX();
                float y = frame.getY()+55;
                for(int i = (int) x; i < x+500; i++){           
                    s.setVisible(true);
                    try {
                        Thread.sleep(1);
                        s.setBounds(i, (int) y, 450, 250);
                    } catch (InterruptedException e1) {
                        e1.printStackTrace();
                    }                   
                }
            }
        });


    }

}

//And here is the code of the slider: 

public class Slider extends JFrame {

    private JPanel contentPane;
    static Slider frame;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    frame = new Slider();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Slider() {
        setAutoRequestFocus(false);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

    }

}

(The main app is not resizable, thats why height and width are constant.)

Any help would be appreciated.

Upvotes: 0

Views: 271

Answers (2)

m.cekiera
m.cekiera

Reputation: 5395

You can use also:

frame.setAlwaysOnTop(true);

and if you don't want frame to be on top all the time, just change it after sliding out.

Upvotes: 1

mohammad haris
mohammad haris

Reputation: 145

use desktop pane and add internal frames on it, desktop pane will be fixed at background and you can add slider on internal frame.. desktop pane will act as a background window..

Upvotes: 0

Related Questions