How to change a JFrame opacity realtime - java

Is there any good way to change a JFrame opacity real time. right now i need to restart the window to get the opacity
if (Variables.LoggerOpacity){
if (AWTUtilities.isTranslucencySupported(AWTUtilities.Translucency.TRANSLUCENT)) {
AWTUtilities.setWindowOpaque(Frame, true);
AWTUtilities.setWindowOpacity(Frame, 0.60f);
}
}
When i use
AWTUtilities.setWindowOpacity(Frame, 0.60f);
On a button JCheckBox i won't change the opacity.
Q: How can i change the opacity realtime?

Even if you have set the JFrame to static, you should be able to reference it if your opacity method is within the same class, if it isn't - create a getter method to reference your JFrame and pass that to your function. Here's an example program that executes and the opacity works fine:
public class JFrameOpacityExample extends JFrame {
private static JFrame myFrame;
private static boolean loggerOpacity;
private static JButton button;
public static void main(String[] args) {
myFrame = new JFrame("Test Frame");
myFrame.setSize(400, 400);
myFrame.setVisible(true);
JPanel panel = new JPanel();
button = new JButton("Press me");
button.setBounds(100, 100, 50, 50);
button.setVisible(true);
panel.add(button);
myFrame.add(panel);
loggerOpacity = true;
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Object src = evt.getSource();
if (src == button && loggerOpacity) {
AWTUtilities.setWindowOpacity(myFrame, 0.40f);
}
}
});
}
}

Add the following command to a frame's constructor. The name of the frame in this example is MyFrame.
jCheckBox1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
AWTUtilities.setWindowOpacity(MyFrame.this, 0.2f);
}
});

Related

How can i make a JButton to switch its function

I had a frame with two buttons. The first button opened a second frame and the other button was closing it. It worked. Now I want to make a single button that opens a frame and by clicking it again the frame shall close. But it is not working. I set boolean conditions. By clicking the button once it is set from false to true. If it is set to "true" the actionListener is supposed to recognize that annother action (close frame) shall be performed. But nothing happens. Here the code.
public class Main {
public static void main(String[] args) {
FrameOne frameOne = new FrameOne ();
}
}
.
public class FrameOne extends JFrame implements ActionListener {
private FrameTwo frameTwo;
private boolean frameIsOpen = false; // By clicking the button once the value is set to true.
private JButton btn = new JButton();
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
FrameOne(){
btn.addActionListener(this);
add(btn);
setSize(400,400);
setLocation(300, 250);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btn && frameIsOpen == false) {
frameTwo = new FrameTwo(); // Opens the new frame
boolean frameIsOpen = true;} // Fulfills the condition for the else if statement.
else if (e.getSource() == btn && frameIsOpen == true) {
frameTwo.dispatchEvent(new WindowEvent(frameTwo, WindowEvent.WINDOW_CLOSING)); }
}
}
.
public class FrameTwo extends JDialog {
FrameTwo() {
setSize(400,400);
setLocation(900, 250);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setVisible(true);
}
You do not need having a boolean value. You can make the frameTwo null when you close it. Then check if it is null and create/show a new dialog. Also since you want to create a new dialog every time (and not hide it), I suggest you instead of frameTwo.dispatchEvent(new WindowEvent(frameTwo, WindowEvent.WINDOW_CLOSING)); to use frameTwo.dispose().
An example:
public class TestFrame extends JFrame {
private JDialog dialog;
public TestFrame() {
super("test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
JButton button = new JButton("open");
button.addActionListener(e -> {
if (dialog == null) {
dialog = new CustomDialog();
dialog.setVisible(true);
} else {
dialog.dispose();
dialog = null;
}
});
add(button);
setLocationByPlatform(true);
pack();
}
static class CustomDialog extends JDialog {
public CustomDialog() {
super();
add(new JLabel("hello world"));
setLocationByPlatform(true);
pack();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new TestFrame().setVisible(true));
}
}

JFrame, toFront(), ActionListener

My problem is this. I got this two windows that work together and move together.
However if I then open broweser or something that will go in front of the screen, and then I try to show my program in front by clicking it on taskbar, then only one window goes in front. Dialog is in the back and I dont know how to fixe it.
I know there is function ToFront() however I stil dont know how to use it in this scenario.
Instead of creating two JFrames, create a JFrame for your main window and create all other windows as non-modal JDialogs, with the JFrame as their owner. This will cause them to be stacked as a single group; whenever the user brings one to the front, all are brought to the front.
This should solve your problem, as VGR already said... Non-modal Dialog will follow it's parent:
public class FocusMain extends JFrame {
private static FocusMain frame;
private static JDialog dialog;
private JCheckBox checkBox;
private JPanel contentPane;
public static void main(String[] args) {
frame = new FocusMain();
frame.setVisible(true);
dialog = new JDialog(frame);
dialog.setSize(100, 100);
}
public FocusMain() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
setContentPane(contentPane);
checkBox = new JCheckBox("show dialog");
checkBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (checkBox.isSelected()) {
dialog.setVisible(true);
} else {
dialog.setVisible(false);
}
}
});
contentPane.add(checkBox);
}
}
With extended JDialog you will need to pass the parent frame through the constructor and if your constructor looks like this: public ExtendedJDialog(JFrame parentFrame) then you can connect it with it's parent frame with super(parentFrame); as the first line in your constructor...
public class FocusMain extends JFrame {
private static FocusMain frame;
private static FocusDialog dialog;
private JCheckBox checkBox;
private JPanel contentPane;
public static void main(String[] args) {
frame = new FocusMain();
frame.setVisible(true);
dialog = new FocusDialog(frame);
}
public FocusMain() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
setContentPane(contentPane);
checkBox = new JCheckBox("show dialog");
checkBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (checkBox.isSelected()) {
dialog.setVisible(true);
} else {
dialog.setVisible(false);
}
}
});
contentPane.add(checkBox);
}
}
and extended JDialog
public class FocusDialog extends JDialog {
public FocusDialog(JFrame parentFrame) {
super(parentFrame);
setSize(100, 100);
}
}
if you need the dialog to block the parent, use super(parentFrame, true);

how to paint images on clicking button in frame?

I've made two buttons in frame .I want to know how to display different images on clicking different buttons?
is there another way out or i have to make panel?I am at beginner stage
package prac;
import java.awt.*;
import java.awt.event.*;
public class b extends Frame implements ActionListener{
String msg;
Button one,two;
b()
{ setSize(1000,500);
setVisible(true);
setLayout(new FlowLayout(FlowLayout.LEFT));
one=new Button("1");
two=new Button("2");
add(one);
add(two);
one.addActionListener(this);
two.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
msg=e.getActionCommand();
if(msg.equals("1"))
{
msg="Pressed 1";
}
else
msg="pressed 2";
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,100,300);
}
public static void main(String s[])
{
new b();
}
}
Use JLabel and change the icon when button is clicked.
Some points:
call setVisible(true) in the end after adding all the component.
use JFrame#pack() method that automatically fit the components in the JFrame based on component's preferred dimension instead of calling JFrame#setSize() method.
sample code:
final JLabel jlabel = new JLabel();
add(jlabel);
final Image image1 = ImageIO.read(new File("resources/1.png"));
final Image image2 = ImageIO.read(new File("resources/2.png"));
JPanel panel = new JPanel();
JButton jbutton1 = new JButton("Show first image");
jbutton1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
jlabel.setIcon(new ImageIcon(image1));
}
});
JButton jbutton2 = new JButton("Show second image");
jbutton2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
jlabel.setIcon(new ImageIcon(image2));
}
});
panel.add(jbutton1);
panel.add(jbutton2);
add(panel, BorderLayout.NORTH);

Add JPanel to contentPane within actionListener

I'm trying to add a JPanel to my JFrame within an actionListener method, but it appears only after the second click on the button. This is a portion of my code where panCours is a JPanel and ConstituerData the targeted JFrame :
addCours.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
panCours.setBounds(215, 2, 480, 400);
panCours.setBorder(BorderFactory.createTitledBorder("Saisir les données concernant le cours"));
ConstituerData.this.getContentPane().add(panCours);
}
});
I don't understand why it doesn't appear as soon as I click on the button. Any explanation and help about how to fix this ?
You'll need to add a call to repaint(); (as well as probably revalidate();) to get the JPanel to show immediately. A basic example demonstrating your problem (and the solution) below;
public class Test {
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
JButton button = new JButton("Test");
button.setBounds(20, 30, 100, 40);
button.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
JPanel panel = new JPanel();
panel.setBackground(Color.red);
panel.setBounds(215, 2, 480, 480);
frame.add(panel);
frame.revalidate(); // Repaint here!! Removing these calls
frame.repaint(); // demonstrates the problem you are having.
}
});
frame.add(button);
frame.setSize(695, 482);
frame.setVisible(true);
}
}
The above said, (as suggested by others) it's only right that I recommend against the use of a null layout in future. The swing layouts are a little awkward to begin with, but they will help you a great deal in the long run.
the answer can be found in the following snippet:
you need to revalidate() the contentPane, not repaint the frame. you can add any panel you want to the contentpane like this. if you declare contentPane as a private field you dont need your getContentPane() call. contentPane is global so it can be reffered to directly from anywhere within the class.
be careful about NullPointerExeptions which can be thrown if you refer to it before initialising.
public class testframe extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
testframe frame = new testframe();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public testframe() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
setContentPane(contentPane);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JPanel a = new JPanel();
contentPane.add(a);
contentPane.revalidate();
}
});
contentPane.add(btnNewButton);
}
}

How do I change a panel with mouselistener?

I'm wanting the panel to expand and display buttons and stuff when the mouse is hovered over, then apon mouse exiting the panel must return top original size.
So far I can only print a message:
public JPanel GUI()
{
final JPanel totalGUI = new JPanel();
totalGUI.setBackground(Color.blue);
totalGUI.setLayout(null);
//+++++++++++++++
// - - - - - - PANEL 1!
//+++++++++++++++
JPanel SearchPanel = new JPanel(); //Create new grid bag layout
SearchPanel.setLocation(5, 5);
SearchPanel.setSize(420, 120);
totalGUI.add(SearchPanel);
SearchPanel.addMouseListener(this);
return totalGUI;
}
public void mouseEntered(MouseEvent e) {
System.out.print("Mouse entered");
}
public void mouseExited(MouseEvent e) {
System.out.print("Mouse exited");
}
private static void createAndShowGUI()
{
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("RWB");
gay3 demo = new gay3();
frame.setContentPane(demo.GUI());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setSize(650, 500);
frame.setVisible(true);
}
public static void main(String[] args)
{
createAndShowGUI();
}
You could do this inside the mouseEntered and mouseExited methods:
JPanel source = (JPanel)e.getSource();
//Edit the searchPanel here
If searchPanel isn't the only component that uses this MouseListener, you may first have to check if the source really is an instance of JPanel:
Object o = e.getSource();
if(o instanceof JPanel) {
JPanel source = (JPanel)o;
//Edit the searchPanel here
}
This isn't necessary if you know the source is a JPanel.

Categories

Resources