Deepak
Deepak

Reputation: 6812

Transparency and JPanel

I want to show a form when i click on edit button on the JTable. The form that is displayed should overlap the JTable and should darken the jTable (just like a black background with transparency). How do i do this ? Do i have to add the jPanel to the window during creation of JFrame or shall i create the panel as a separate file and make it visible when the button is clicked. Tell me how to do this ?

EDIT

Something similar to this

enter image description here

EDIT 2

You have used JOption pane and the other suggestion was to use JDialog. But if i use either of those i cant create child window. I just need to call virtual keyboard from the popped up Jdialog window. I cant access the keyboard as the JDialog is holding the focus. How to solve this issue ?

EDIT 3

The current problem is, i am using virtual keyboard for typing the values in the form displayed by using JDialog. Now i cant able to open the virtual Keyboard and make it active. Even if i open it it is behind the JDialog and the focus is still with JDialog. I need to close the JDialog for using the virtual keyboard.

Upvotes: 2

Views: 1871

Answers (2)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

I'm a little late in answering as I was creating a test program, but my idea is the same as Andrew's (sorry Andrew, and 1+ to Andrew):

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DarkBackground extends JPanel {
   private static final Dimension MAIN_SIZE = new Dimension(800, 500);
   private static final Color DarkColor = new Color(0, 0, 0, 60);
   private JComponent glassPane;

   public DarkBackground() {
      JButton showDialogBtn = new JButton("Show Dialog");
      showDialogBtn.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            setGlassPaneVisible(true);
            JOptionPane.showMessageDialog(DarkBackground.this, "Foo");
            setGlassPaneVisible(false);
         }
      });
      add(showDialogBtn);
      setPreferredSize(MAIN_SIZE);

   }

   public void setGlassPane(JComponent glassPane) {
      JRootPane rootpane = SwingUtilities.getRootPane(this);
      this.glassPane = glassPane; 

      rootpane.setGlassPane(glassPane);
   }

   public void setGlassPaneVisible(boolean visible) {
      glassPane.setVisible(visible);
   }

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

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

class MyGlassPane extends JComponent {
   private Color backgroundColor;

   public MyGlassPane(Color backgroundColor) {
      this.backgroundColor = backgroundColor;
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.setColor(backgroundColor);
      g.fillRect(0, 0, getWidth(), getHeight());
   }
}

Upvotes: 6

Andrew Thompson
Andrew Thompson

Reputation: 168845

See How to Use Root Panes in the Java Tutorial. From the screen-shot it appears you need to pop a JOptionPane (or JDialog) for the input and draw the shading on the glass pane.

Upvotes: 4

Related Questions