JPanel opacity

Hi people,

Basically I have one JFrame with a few controls on it, at some point I want to set the frame's glassPane to a JPanel with some opacity, this means I want to be able to see the controls beneath it.

I have searched but I could not find anything that made me happy ;)

I am not used to work with swing since I am mainly a web developer, so sorry if this is of an easy answer.

Many Thanks,

MeTitus

[441 byte] By [Me_Titusa] at [2007-11-27 11:14:31]
# 1

This example should give you some ideas:

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

import javax.swing.border.*;

public class DisabledGlassPane extends JComponent

implements KeyListener

{

private final static Color DEFAULT_BACKGROUND = new Color(128, 128, 128, 128);

private final static Border MESSAGE_BORDER = new EmptyBorder(10, 10, 10, 10);

private JLabel message = new JLabel();

public DisabledGlassPane()

{

setOpaque( false );

setBackground( DEFAULT_BACKGROUND );

setLayout( new GridBagLayout() );

add(message, new GridBagConstraints());

message.setOpaque(true);

message.setBorder(MESSAGE_BORDER);

// Disable Mouse, Key and Focus events for the glass pane

addMouseListener( new MouseAdapter() {} );

addMouseMotionListener( new MouseMotionAdapter() {} );

addKeyListener( this );

setFocusTraversalKeys(

KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, Collections.EMPTY_SET );

setFocusTraversalKeys(

KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, Collections.EMPTY_SET );

}

protected void paintComponent(Graphics g)

{

g.setColor( getBackground() );

g.fillRect(0, 0, getSize().width, getSize().height);

}

public void setBackground(Color background)

{

super.setBackground( background );

Color messageBackground =

new Color(background.getRed(),

background.getGreen(), background.getBlue());

message.setBackground( messageBackground );

}

public void keyPressed(KeyEvent e)

{

e.consume();

}

public void keyTyped(KeyEvent e) {}

public void keyReleased(KeyEvent e)

{

e.consume();

}

public void activate(String text)

{

if (text != null && text.length() > 0)

{

message.setVisible( true );

message.setText( text );

message.setForeground( getForeground() );

}

else

message.setVisible( false );

setVisible( true );

setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

requestFocusInWindow();

}

public void deactivate()

{

SwingUtilities.invokeLater(new Runnable()

{

public void run()

{

setCursor(null);

setVisible( false );

}

});

}

public static void main(String[] args)

{

final DisabledGlassPane glassPane = new DisabledGlassPane();

glassPane.setBackground( new Color(255, 128, 128, 128) );

glassPane.setForeground( Color.WHITE );

final JTextField textField = new JTextField();

final JButton button = new JButton( "Click Me" );

button.setMnemonic('c');

button.addActionListener( new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

glassPane.activate("Please Wait...");

Thread thread = new Thread()

{

public void run()

{

try { this.sleep(5000); }

catch (InterruptedException ie) {}

glassPane.deactivate();

}

};

thread.start();

}

});

JFrame frame = new JFrame();

frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

frame.setGlassPane( glassPane );

frame.getContentPane().add(new JLabel("NORTH"), BorderLayout.NORTH );

frame.getContentPane().add( button );

//frame.getContentPane().add(new JTextField(), BorderLayout.SOUTH);

frame.getContentPane().add(textField, BorderLayout.SOUTH);

frame.setSize(300, 300);

frame.setLocationRelativeTo( null );

frame.setVisible(true);

}

}

camickra at 2007-7-29 14:07:21 > top of Java-index,Desktop,Core GUI APIs...
# 2

Cheers camickr, I will look at the code Monday morning.

MeTitus

Me_Titusa at 2007-7-29 14:07:21 > top of Java-index,Desktop,Core GUI APIs...