Bring JInternalFrame to the front

I have JDesktopPaneone that contains the number JInternalFrames. I would like to be able to bring anyone JInternalFrameto the forefront, overlaying any other active frames. I found some code examples for this, but none of them work - the frame does NOT go to other active JInternalFrames. For instance.

public static void moveToFront (final JInternalFrame fr) {
        if (fr! = null) {
            processOnSwingEventThread (new Runnable () {
                public void run () {
                    fr.moveToFront ();
                    fr.setVisible (true);
                    try {
                        fr.setSelected (true);
                        if (fr.isIcon ()) {
                            fr.setIcon (false);
                        }
                        fr.setSelected (true);
                    } catch (PropertyVetoException ex) {
                        ex.printStackTrace ();
                    }
                    fr.requestFocus ();
                    fr.toFront ();
                }
            });
        }
    }
+3
source share
1 answer

According to the API, toFront or moveToFront should work (although toFront looks better than two of my API reads). Are these JInternalFrames sitting on a JDesktopPane? According to your message, it looks like they are. I wonder if there is a mistake elsewhere.

SSCCE (, ), , , . , , .

SSCCE:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.Random;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class InternalFrameToFront extends JPanel {
   private static final int FRAME_MAX = 21;
   private static final int DT_WIDTH = 700;
   private static final int DT_HEIGHT = 500;
   private static final Dimension DESKTOP_SIZE = new Dimension(DT_WIDTH, DT_HEIGHT);
   private static final int IF_WIDTH = 150;
   private static final int IF_HEIGHT = 100;
   private static final Dimension INT_FRAME_SIZE = new Dimension(IF_WIDTH, IF_HEIGHT);
   private Random random = new Random();
   private JInternalFrame[] internalFrames = new JInternalFrame[FRAME_MAX];

   public InternalFrameToFront() {
      JDesktopPane desktop = new JDesktopPane();
      desktop.setPreferredSize(DESKTOP_SIZE);
      for (int i = 0; i < internalFrames.length; i++) {
         JInternalFrame intFrame = new JInternalFrame("Frame Number " + i);
         intFrame.setSize(INT_FRAME_SIZE);
         int x = random.nextInt(DT_WIDTH - IF_WIDTH);
         int y = random.nextInt(DT_HEIGHT - IF_HEIGHT);
         intFrame.setLocation(x, y);
         intFrame.setVisible(true);
         desktop.add(intFrame);

         internalFrames[i] = intFrame;
      }

      JSlider slider = new JSlider(0, FRAME_MAX - 1, 0);
      slider.setMajorTickSpacing(5);
      slider.setMinorTickSpacing(1);
      slider.setPaintLabels(true);
      slider.setPaintTicks(true);
      JPanel sliderPanel = new JPanel();
      sliderPanel.add(slider);
      slider.addChangeListener(new ChangeListener() {
         public void stateChanged(ChangeEvent ce) {
            JSlider slider = (JSlider) ce.getSource();
            if (!slider.getValueIsAdjusting()) {
               int value = slider.getValue();
               internalFrames[value].toFront();
            }
         }
      });

      setLayout(new BorderLayout());
      add(desktop, BorderLayout.CENTER);
      add(sliderPanel, BorderLayout.SOUTH);
   }

   private static void createAndShowUI() {
      JFrame frame = new JFrame("InternalFrameToFront");
      frame.getContentPane().add(new InternalFrameToFront());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}
+5

Source: https://habr.com/ru/post/1793926/


All Articles