Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make JDialog inactive

I want to make JDialog-based window inactive, so all controls apeared disabled (in grey color). setEnabled(false) just makes impossible to click any control, even close window. But nothing turns gray. Help please.

EDIT: Here is sample code.

import javax.swing.JButton;
import javax.swing.JDialog;


public class Analyzer extends JDialog{

public Analyzer() {
    JButton but = new JButton("test");
    setLayout(null);
    but.setBounds(10,10,100,100);

    add(but);
    setSize( 200, 200);
    setVisible(true);
    setEnabled(false);
}

public static void main(String[] args) {
    new Analyzer();
}

}
like image 474
Aleksandr Kravets Avatar asked Feb 23 '12 14:02

Aleksandr Kravets


People also ask

How do I exit JDialog?

All you need to do is to create a custom subclass of JDialog , define an ActionListener for closing the JDialog , and register the Escape keystroke to call the action listener. Then, when the user presses the Escape key when the JDialog is open, JDialog automatically goes away.

Where is JDialog used in an application?

It is used to create a modeless dialog with specified Frame as its owner and an empty title. It is used to create a dialog with the specified title, owner Frame and modality.

What is the difference between JFrame and JDialog?

JFrame is a normal window with its normal buttons (optionally) and decorations. JDialog on the other side does not have a maximize and minimize buttons and usually are created with JOptionPane static methods, and are better fit to make them modal (they block other components until they are closed).

How do I open JDialog in center of screen?

If you really want to center a JDialog on screen, you can use code like this: // center a jdialog on screen JDialog d = new JDialog(); d. setSize(400, 300); d. setLocationRelativeTo(null); d.


1 Answers

The two ways I know to do this, one where you disable the components of a dialog recursively, and the second where you disable the entire dialog (including ability to drag the dialog):

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;

import javax.swing.*;

@SuppressWarnings("serial")
public class DisableEg extends JPanel {
   public static final String DISABLE_DIALOG_COMPONENTS = "Disable Dialog Components";
   public static final String ENABLE_DIALOG_COMPONENTS = "Enable Dialog Components";
   public static final String DISABLE_DIALOG = "Disable Dialog";
   public static final String ENABLE_DIALOG = "Enable Dialog";
   private static final int LOC_SHIFT = 150;
   private Analyzer analyzer;

   public DisableEg(JFrame frame) {
      analyzer = new Analyzer(frame);
      analyzer.pack();
      analyzer.setLocationRelativeTo(frame);
      Point location = analyzer.getLocation();
      location = new Point(location.x - LOC_SHIFT, location.y - LOC_SHIFT);
      analyzer.setLocation(location);
      analyzer.setVisible(true);

      add(new JButton(new AbstractAction(DISABLE_DIALOG_COMPONENTS) {

         @Override
         public void actionPerformed(ActionEvent evt) {
            AbstractButton btn = (AbstractButton) evt.getSource();
            if (btn.getText().equals(DISABLE_DIALOG_COMPONENTS)) {
               btn.setText(ENABLE_DIALOG_COMPONENTS);
               analyzer.setComponentEnabled(false);
            } else {
               btn.setText(DISABLE_DIALOG_COMPONENTS);
               analyzer.setComponentEnabled(true);
            }
         }
      }));
      add(new JButton(new AbstractAction(DISABLE_DIALOG) {

         @Override
         public void actionPerformed(ActionEvent evt) {
            AbstractButton btn = (AbstractButton) evt.getSource();
            if (btn.getText().equals(DISABLE_DIALOG)) {
               btn.setText(ENABLE_DIALOG);
               analyzer.setEnabled(false);
            } else {
               btn.setText(DISABLE_DIALOG);
               analyzer.setEnabled(true);
            }
         }
      }));
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("Disable Example");
      DisableEg mainPanel = new DisableEg(frame);

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

@SuppressWarnings("serial")
class Analyzer extends JDialog {

   public Analyzer(JFrame frame) {
      super(frame, "Analyzer Dialog", false);
      JButton but = new JButton("test");
      setLayout(new FlowLayout());

      add(but);
      setPreferredSize(new Dimension(200, 200));
   }

   public void setComponentEnabled(boolean enabled) {
      setComponentEnabled(enabled, getContentPane());

      // !! if you have menus to disable, you may need instead
      // setComponentEnabled(enabled, this); // !!
   }

   private void setComponentEnabled(boolean enabled, Component component) {
      component.setEnabled(enabled);
      if (component instanceof Container) {
         Component[] components = ((Container) component).getComponents();
         if (components != null && components.length > 0) {
            for (Component heldComponent : components) {
               setComponentEnabled(enabled, heldComponent);
            }
         }
      }
   }

}
like image 177
Hovercraft Full Of Eels Avatar answered Sep 22 '22 17:09

Hovercraft Full Of Eels