Question

I am creating a Swing Application. I have one main JFrame and a JDesktopPane. I added one button and one label on main frame. But if I open any JInternalFrame on Main Frame button and label covers the internal frame.

(JButtonand JLabel appear foreground of JInternalFrame). If I click internal frame button go to background.

Can you help to solve this?

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

public class MainFrame {

   JFrame frame1 ;
   JDesktopPane desktop ;

       public MainFrame () {
         frame1 = new JFrame("EMPLOYEE LEAVE TRACKER");
         frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame1.setExtendedState(Frame.MAXIMIZED_BOTH);
         frame1.repaint();
         desktop = new JDesktopPane();        //Creates a new JDesktopPane.
         frame1.setContentPane(desktop);
         frame1.setSize(900,700);
         frame1.setVisible(true);
         desktop.setBackground(Color.DARK_GRAY );

    //Creates a JLabel on JDesktopPane.
    JLabel label1 = new JLabel("EMPLOYEE LEAVE TRACKER", SwingConstants.CENTER);
       label1.setFont(new Font("SansSerif",Font.ITALIC + Font.BOLD,54));
       label1.setBounds(new Rectangle(new Point(275, 100),label1.getPreferredSize()));
    //Creates a JButon on JDesktopPane.


   JButton Leave = new JButton("Leave Management");
     Leave.setHorizontalTextPosition(JButton.CENTER);
     Leave.setBounds(new Rectangle(new Point(700,200),Leave.getPreferredSize()));
     Leave.setSize(300, 300);
     Leave.addActionListener(new java.awt.event.ActionListener()      {
        @Override
        public void actionPerformed(java.awt.event.ActionEvent e)   {
        frame1.add(LeaveManagment()); 
        }
    });

  //Look and Feel

     try {
        UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch(Exception e) {
          System.out.println("Error setting native LAF: " + e);
        }

    desktop.add(Leave);
    desktop.add(label1);

     }
   //Creating JInternalFrame

    public JInternalFrame LeaveManagment(){              
    final JInternalFrame employeeFrame = new JInternalFrame("LEAVE M" +
            "ANAGEMNT", true, true, true, true);
    employeeFrame.getContentPane().setBackground(Color.white);
    employeeFrame.setSize(900,700);
    employeeFrame.setVisible(true);
    employeeFrame.setMaximizable(true);
    employeeFrame.setResizable(true);
    JComponent c = (JComponent)
    employeeFrame.getContentPane();
    c.setLayout(new FlowLayout());

        return employeeFrame;
}

    public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      public void run() {
      new MainFrame ();
      }
});
}}
Was it helpful?

Solution

From the sound of it, you've added the buttons to the JDesktopPane.

The desktop pane is a type of JLayeredPane, this allows you to place components on, we'll, layers.

While this is a neat feature, IMHO, only JInternalFrames and the occasional special "window" should appear on the desktop (although the desktop manager does use a type of button to represent minimised windows)

Personally, I'd place the buttons also where, not on the desktop.

Take a look at How to Use Internal Frames and How to use Layered Pane

OTHER TIPS

MadProgrammer has it right. One solution is to add your components to the appropriate JLayeredPane layer. i.e.,

// Note name change of JButton to adhere to Java naming standards:
desktop.add(leaveManagementBtn, JLayeredPane.DEFAULT_LAYER);
desktop.add(label1, JLayeredPane.DEFAULT_LAYER); // note layer component being added to

and,

public void actionPerformed(java.awt.event.ActionEvent e) {
  // again method name capitalization changed to adhere to standard
  // again, component added to appropriate layer
  frame1.add(leaveManagment(), JLayeredPane.PALETTE_LAYER);
}

Again, MadProgrammer is right, the buttons should not be on the JDesktopFrame but on a button bar off of the desktop. I recommend that you accept his answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top