0

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

asked Aug 18, 2014 at 11:37
8
  • You should be adding the JInternalFrame to a 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 Commented Aug 18, 2014 at 11:49
  • I have tried JDesktopPane...but when I called the JDesktopPane...my entire system will be covered by it...how can I do it to fit only for the internal frame? Commented Aug 18, 2014 at 11:51
  • The JDesktopPane should be initially alreayd added to the frame, as a part of the initial application (search MDI). You're probably better of just using a JDialog Commented Aug 18, 2014 at 11:52
  • Alright...will try it first :D Commented Aug 18, 2014 at 11:53
  • @peeskillet..I have tried creating a JDesktopPane and then add my JFrame into it first...when I clicked the button..then only the JDesktopPane add the internal frame...but I got an error saying java.lang.IllegalArgumentException: adding a window to a container Commented Aug 18, 2014 at 12:20

1 Answer 1

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);
 }
 }
}
answered Aug 18, 2014 at 12:42
Sign up to request clarification or add additional context in comments.

6 Comments

I tried to use ur code and grab wat I can...when i clicked on the btnNew...nothing come out
the exact code is running though @_@ i will update my latest code and let u check
Was that your complete update? You changed two lines. Sorry but I don't think I can help you if you don't give me something I actually run and test.
found the problem...i setLayout(null) for the wconstruct...once i taken it off...the internal frame can show...but when i move the internal frame over the buttons, they will be gone...and i have to click on them to make them appear again
Don't use null layouts/setBounds, period. If you want further help, I'm going to need something to run.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.