0

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.

Gelidus
  • 28
  • 4

2 Answers2

1

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.

m.cekiera
  • 5,365
  • 5
  • 21
  • 35
  • Thank you :) I want to put the slider to the background, and didnt even think about set the window alwaysOnTop... I think i need to sleep :) – Gelidus Jul 04 '15 at 20:24
  • JFrame ignores setAlwaysOnTop, but to works with JDialog – mKorbel Jul 05 '15 at 06:30
  • @mKorbel what do you mean? It works with example from question, one frame stays on top, one is behind it – m.cekiera Jul 05 '15 at 07:15
  • JFrame doesn't react to the (maybe works in this case, milion times here about), it is basics funcionality for JDialog – mKorbel Jul 05 '15 at 08:37
  • @mKorbel JDiaglog is not good for me here. And maybe you are right and JFrame ignores setAlwaysOnTop (I havent tried it) but i have to use alwaysOnTop in the code of the Application Window. I already use this slider in my main project and working perfectly. Anyway thank you for your answer maybe it going to be useful later. – Gelidus Jul 06 '15 at 13:33
0

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..