my problem currently is that I have a JFrame (with some buttons on it)...when I clicked on the "New" button, it will call an InternalFrame on the screen...the internal frame screen came out and everything is good until i move the internal frame around and found that it is set at the back of everything...
I have tried the .toFront() , .setAlwaysOnTop() and everything...until I came across the JLayeredPane and I think it is something I am looking for...but I couldn't make it work>< can anyone else guide me through it? Thanks!
enter image description here
Please do tell the extra information you all need..will provide them ASAP
WindowConstruct wconstruct;
JDesktopPane desktop = new JDesktopPane();
JInternalFrame InternalWindows = new JInternalFrame();
public MainUser(){
wconstruct = new WindowConstruct("..:: User's Helpdesk Main Page ::..", 1500, 800, false, null, "user");
wconstruct.add(desktop);
wconstruct.btnNew.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
if(src == wconstruct.btnNew){
InternalWindows.setSize(500, 300);
InternalWindows.setTitle("New task");
InternalWindows.setLayout(null);
InternalWindows.setLocation(100,50);
InternalWindows.setClosable(true);
desktop.add(InternalWindows);
InternalWindows.setVisible(true);
}
}
enter image description here
1 Answer 1
I don't seem to get any exceptions, attempting to create an MCVE with the code you've shown. It's not the best code, but I tried to maintain what your code looks like as much as possible. Look over it and let me know how my code differs. And again, to better help us get down to the root of the problem, you should always post an MCVE. That means the code should be runnable, i.e copy,paste,compile,run.
And also please consider all my above comments (i.e. maybe using a JDialog, if you don't plan to make this application an MDI (see the link in comment) type application.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.SwingUtilities;
public class MainUser implements ActionListener {
WindowConstruct wconstruct;
JDesktopPane desktop = new JDesktopPane();
JInternalFrame InternalWindows = new JInternalFrame();
public MainUser() {
wconstruct = new WindowConstruct("..:: User's Helpdesk Main Page ::..",
500, 500);
wconstruct.add(desktop);
wconstruct.btnNew.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
if (src == wconstruct.btnNew) {
InternalWindows.setSize(500, 300);
InternalWindows.setTitle("New task");
InternalWindows.setLayout(null);
InternalWindows.setClosable(true);
InternalWindows.setLocation(100, 50);
desktop.add(InternalWindows);
InternalWindows.setVisible(true);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MainUser();
}
});
}
class WindowConstruct extends JFrame {
JButton btnNew = new JButton("Add New");
public WindowConstruct(String title, int width, int height) {
super(title);
setSize(width, height);
add(btnNew, BorderLayout.PAGE_END);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
}
6 Comments
Explore related questions
See similar questions with these tags.
JDesktopPane, not directly to the frame. The image you're showing is a symptom of doing the latter. See How to use Internal Frames. Or maybe if you want the internal frame to "always be on top", you're probably looking to use a model JDialog, instead of an internal frame. See How to use Dialogs