Java: Popup not always visible - java

I use a Popup to display an update progress. I put a semi-transparent panel above the main window for effect. In far the most cases the Popup is visible but on some computers it's not. It seems to be related to specific computers. Does anyone know a solution or have a better way to implement this?
//Disable main components
tabs.setEnabledAt(0, false);
tabs.setEnabledAt(1, false);
comPorts_CB.setEnabled(false);
getinfo_B.setEnabled(false);
//Add effect panel
pop_effect_panel = new JPanel();
pop_effect_panel.setBackground(new Color(255, 255, 255, 192));
pop_effect_panel.setBounds(0, 0, 1000, 1000);
pop_effect_panel.setLayout(null);
pop_effect_panel.setOpaque(true);
getContentPane().add(pop_effect_panel);
getContentPane().setComponentZOrder(pop_effect_panel, 0);
getContentPane().setEnabled(false);
pop_effect_panel.invalidate();
//Create pop-up panel
pop_panel = new JPanel();
pop_panel.setBackground(BACKGROUND_COLOR);
pop_panel.setSize(300, 300);
pop_panel.setLayout(null);
pop_panel.setOpaque(true);
pop_panel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
pop_progress_TA = new NonSelectableTextArea();
pop_progress_TA.setBounds(2, 2, 296, 268);
pop_progress_TA.setBackground(BACKGROUND_COLOR);
pop_panel.add(pop_progress_TA);
pop_progress_bar = new JProgressBar();
pop_progress_bar.setBounds(1, 270, 240, 28);
pop_progress_bar.setValue(0);
pop_progress_bar.setStringPainted(true);
pop_progress_bar.setString("");
pop_panel.add(pop_progress_bar);
pop_ok_B = new JButton("OK");
pop_ok_B.setBounds(241, 270, 57, 28);
pop_ok_B.setEnabled(false);
pop_panel.add(pop_ok_B);
final Popup popup = PopupFactory.getSharedInstance().getPopup(getContentPane(), pop_panel, 100, 250);
popup.show();
pop_ok_B.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
popup.hide();
//Remove effect panel
getContentPane().remove(pop_effect_panel);
getContentPane().validate();
//Enable main components
tabs.setEnabledAt(0, true);
tabs.setEnabledAt(1, true);
comPorts_CB.setEnabled(true);
getinfo_B.setEnabled(true);
}
});
pop_progress_TA.requestFocusInWindow();

Ensure that:
The progress dialog is created and updated on the EDT.
The long running task is completed off the EDT.

consider that would be better look for/using un-decorated Modal JDialog or JWindow (by defalut un-decorated) instead of JPopup

Related

JButton, JTextFeild, JLabel set background color not working

This is my code for JButton. I am facing problem in setting background color for this Login button
JButton btnlogin = new JButton("Log in");
btnlogin.setFont(new Font("Lucida Grande", Font.BOLD, 14));
btnlogin.setAlignmentX(Component.CENTER_ALIGNMENT);
btnlogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
EmailValidator emailValidator = new EmailValidator();
if(!emailValidator.validate(txtEmail.getText().trim())) {
System.out.print("Invalid Email ID");
validationtext.setText("Invalid Email");
}
}
});
btnlogin.setBounds(210, 432, 200, 48);
btnlogin.setBackground(new Color(66, 185, 217));
frame.getContentPane().add(btnlogin);
I have already tried setting opaque value as true, but still not working.
You just need to add two lines before setting background color to get your expected result:
Set opaque value as true by writing this : btnlogin.setOpaque(true);
By default a border is set in JFrame, you have to change it according to your wish. Here I am using example of MatteBorder : btnlogin.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
Then after you will get your expected result.
This code actually changes the color of the button:
btnlogin.setOpaque(true);
btnlogin.setBorder(null);
btnlogin.setBackground(Color.red); // Any color you wish

JPanel shrinks when I click on button

Whenever I click my show password button the panels shrink and become as in the pictures my code is below:
public class AssetLogin extends JFrame implements ActionListener, MouseListener {
private static final long serialVersionUID = 1L;
private JPanel layout, panLogin, panEmail, panPassword;
private JButton btnShowPassword;
private JTextField txtEmail;
private JPasswordField txtPassword;
public AssetLogin() {
super("Asset And Equipment Tracking");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setExtendedState(Frame.MAXIMIZED_BOTH);
this.setLocationRelativeTo(null);
this.pack();
this.setVisible(true);
this.setResizable(false);
this.setMinimumSize(new Dimension(750, 500));
JFrame.setDefaultLookAndFeelDecorated(false);
getContentPane().setLayout(new BorderLayout(0, 0));
UIManager.put("TextField.border", BorderFactory.createMatteBorder(0, 0, 0, 0, Color.WHITE));
UIManager.put("PasswordField.border", BorderFactory.createMatteBorder(0, 0, 0, 0, Color.WHITE));
UIManager.put("Button.background", Color.WHITE);
UIManager.put("Button.foreground", Color.GRAY);
UIManager.put("Button.border", BorderFactory.createMatteBorder(0, 0, 0, 0, Color.WHITE));
UIManager.put("Button.focus", new Color(0,0,0,0));
UIManager.put("Button.select", new Color(0, 0, 0, 0));
layout = new JPanel();
layout.setLayout(null);
layout.setSize(this.getWidth(), this.getHeight());
layout.setBackground(Color.WHITE);
initialize();
getContentPane().add(layout);
}
private void initialize() {
panEmail = new JPanel();
panPassword = new JPanel();
txtEmail = new JTextField();
txtPassword = new JPasswordField();
setBoundsResize();
setDecorations();
btnShowPassword.addActionListener(this);
btnShowPassword.addMouseListener(this);
panLogin.add(panEmail);
panLogin.add(panPassword);
panEmail.add(txtEmail);
panPassword.add(txtPassword);
panPassword.add(btnShowPassword);
layout.add(panLogin);
}
private void setBoundsResize () {
panLogin.setBounds(layout.getWidth() / 2 - layout.getWidth() / 4, layout.getHeight() / 2 - layout.getHeight() / 3, layout.getWidth() / 2, (2 * layout.getHeight()) / 3);
panEmail.setBounds(10, 10, panLogin.getWidth() - 10, 60);
panPassword.setBounds(10, 100, panLogin.getWidth() - 10, 60);
btnShowPassword.setBounds(panPassword.getWidth() - 120, 15, 100, panPassword.getHeight() - 25);
txtEmail.setBounds(10, 15, panEmail.getWidth() - 20, panEmail.getHeight() - 25);
txtPassword.setBounds(10, 15, panPassword.getWidth() - 130, panPassword.getHeight() - 25);
}
private void setDecorations() {
layout.setBackground(Color.WHITE);
layout.setBorder(BorderFactory.createMatteBorder(0, 2, 2, 2, Color.LIGHT_GRAY));
panLogin.setOpaque(false);
panEmail.setOpaque(false);
panPassword.setOpaque(false);
panEmail.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Email"));
panPassword.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Password"));
btnShowPassword.setOpaque(false);
txtPassword.setEchoChar('*');
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnShowPassword) {
if (txtPassword.getEchoChar() == '*') {
txtPassword.setEchoChar( (char) 0 );
//btnShowPassword.setOpaque(true);
btnShowPassword.setBorder(
BorderFactory.createLineBorder(Color.black));
} else {
txtPassword.setEchoChar( '*' );
//btnShowPassword.setOpaque(false);
btnShowPassword.setBorder(null);
}
}
}
#Override
public void mousePressed(MouseEvent e) {
btnShowPassword.setBackground(null);
}
#Override
public void mouseReleased(MouseEvent e) {
btnShowPassword.setBackground(Color.LIGHT_GRAY);
}
}
of course i added the rest of the mouse event methods but the thing is that as i said the JPanels shrinks when i click "show password" button here how it should look like:
how it look like when I click the button:
Why is this happening?
Try getting rid of:
this.pack();
The Window.pack() method causes your frame to resize which will cause all your child elements to resize to their preferred sizes:
Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. The resulting width and height of the window are automatically enlarged if either of dimensions is less than the minimum size as specified by the previous call to the setMinimumSize method.
If the window and/or its owner are not displayable yet, both of them are made displayable before calculating the preferred size. The Window is validated after its size is being calculated.
If you're not setting your preferred sizes correctly this can cause the types of distortion you're seeing.
Don't forget to add the layout for each panel since this will cause that problem when you are putting:
this.pack();
Since the Window.pack() method causes your frame to resize which will cause all your child elements to resize to their preferred sizes:
Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. The resulting width and height of the window are automatically enlarged if either of dimensions is less than the minimum size as specified by the previous call to the setMinimumSize method.
If the window and/or its owner are not displayable yet, both of them are made displayable before calculating the preferred size. The Window is validated after its size is being calculated.
If you're not setting your preferred sizes correctly this can cause the types of distortion you're seeing.

Jbutton disappears in design view

I'm using WindowBuilder in Eclipse. I Created the following method to apply the same properties to certain types of buttons in my application.
In design view, my btn_Subscribe is invisible, but it appears when debugging.
However my btn_Login is visible in design view... I don't get it. I'm using my method when I add the JButton to the content
// ************************ LOGIN BUTTON ************************ \\
JButton btn_Login = new JButton("");
btn_Login.setIcon(new ImageIcon(DietProject.class.getResource("/images/img_login.png")));
btn_Login.setBounds(226, 89, 91, 32);
frmDietPlanner.getContentPane().add(setupCustomButton(btn_Login));
// ************************ SUBSCRIBE BUTTON ************************ \\
JButton btn_Subscribe = new JButton("");
btn_Subscribe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btn_Subscribe.setIcon(new ImageIcon(DietProject.class.getResource("/images/img_subscribe.png")));
btn_Subscribe.setBounds(10, 11, 103, 32);
frmDietPlanner.getContentPane().add(setupCustomButton(btn_Subscribe));
private JButton setupCustomButton(JButton jb)
{
// Remove the ugly border and background on the button
jb.setBorderPainted(false);
jb.setBorder(null);
jb.setContentAreaFilled(false);
jb.setCursor(new Cursor(Cursor.HAND_CURSOR));
return jb;
}
I assume frmDietPlanner is a JFrame which has by default BorderLayout manager.
By
frmDietPlanner.getContentPane().add(setupCustomButton(btn_Login));
frmDietPlanner.getContentPane().add(setupCustomButton(btn_Subscribe));
You are adding two buttons to the same location.
If you want to use setBounds set layout manager to null.
An MCVE for the problem and the solution looks like this:
import javax.swing.JButton;
import javax.swing.JFrame;
public class Frame extends JFrame {
Frame()
{
/////////////////////////////
setLayout(null);
////////////////////////////
setSize(400,400);
JButton btn_Login = new JButton("A");
btn_Login.setBounds(226, 89, 91, 32);
getContentPane().add(btn_Login);
JButton btn_Subscribe = new JButton("B");
btn_Subscribe.setBounds(10, 11, 103, 32);
getContentPane().add(btn_Subscribe);
setVisible(true);
}
public static void main(String[] args)
{
new Frame();
}
}
If I do the following, it shows up in the designer just fine. I don't get why using a method to apply the same properties would cause the designer not to display the button.... and at the same time it works perfectly fine for my login button! I really don't feel like doing this code for every single button I plan on using....
//frmDietPlanner.getContentPane().add(setupCustomButton(btn_Subscribe));
frmDietPlanner.getContentPane().add(btn_Subscribe);
btn_Subscribe.setBorderPainted(false);
btn_Subscribe.setBorder(null);
btn_Subscribe.setContentAreaFilled(false);
btn_Subscribe.setCursor(new Cursor(Cursor.HAND_CURSOR));

JFrame setComponentZOrder() changes size of the objects

I'm trying to write an undecorated JFrame. I'm trying to put my button over my background label. However setting the button's Z order causes the button streches to size of jframe and neither setBounds() nor setSize() changes the situation. Here is my code:
import javax.swing.*;
public class MyApp {
public static void main(String[] args) {
JFrame mainFrame = new JFrame();
mainFrame.setBounds(0, 112, 100, 50);
mainFrame.setLayout(null);
mainFrame.setUndecorated(true);
JLabel lblBackground = new JLabel(new ImageIcon(JFrame.class.getResource("/res/green.png")));
lblBackground.setBounds(0, 0, 100, 50);
JButton btnStart = new JButton("");
btnStart.setBounds(5, 15, 10, 15);
mainFrame.add(lblBackground);
mainFrame.add(btnStart);
mainFrame.setComponentZOrder(btnStart, 0);
mainFrame.setComponentZOrder(btnStart, 1);
mainFrame.setVisible(true);
}
}
Thanks for replies.
Use a JLayeredPane for this.
You will need to create a new JLayeredPane:
JLayeredPane layered = new JLayeredPane();
Set your JFrame to use this as a content pane:
mainFrame.setContentPane(layered);
And add your components, in this format:
layered.add(Component c, int layerNumber);
Hope that works for you!
More on JLayeredPanes
Don't play with null layouts. Swing was designed to be used with layout managers
Add the button to the label. For example:
JLabel label = new JLabel(...);
label.setLayout( new FlowLayout() );
JButton button = new JButton(...);
label.add( button );
frame.add(label);
frame.pack();
frame.setVisible(true);
Now the frame should be the same size as your image. The button should be centered on the top of the image. If you want the button positioned somewhere else then use a different layout manager.

Improper Alignment Error with Java AWT. Cannot get my label to move

I have put a label into my frame, but it refuses to move. SetBounds() is not working, and I get an improper alignment error if I put any argument past "Result" below that isn't 0, 1, or 2, none of which put it in the correct place. Here's where I declare the Label:
Label result = new Label("Result.", 3);
Here's the SetBounds statement:
result.setBounds(0, 1500, 100, 20);
This program I am writing, I simply just want to have the user input 2 numbers, add them, and print the result using GUI components. The result is the label which refuses to change. The code of the entire program is below, and the program is still not done yet, but if you compile it, result is always stuck to the left and I want it to be at the same level as the TextFields. This problem is actually happening with the other labels, Help1, and Help2. Please don't tell me I have to use swing! I dislike swing.
I have yet to change the event to where it adds the user inputs. I copied the event from a previous program.
The code: (Sorry for no comments, but it's not a huge program)
import java.awt.*;
import java.awt.event.*;
public class MouseClick {
TextField number1;
TextField number2;
public static void main(String[] args) {
MouseClick MC = new MouseClick();
}
public MouseClick() {
Frame f = new Frame("Addition Time!");
Button button = new Button("Click Here To Add The Two Numbers.");
button.setBounds(175, 250, 230, 30);
button.addMouseListener(new MyMouseListener());
f.add(button);
Label help1 = new Label("Enter the first number below.");
Label help2 = new Label("Enter the second number below.");
Label exprsn1 = new Label("+", 0);
Label exprsn2 = new Label("=", 0);
Label result = new Label("Result.", 3);
number1 = new TextField("TextField1", 100);
number2 = new TextField("TextField2", 100);
help1.setBounds(50, 80, 150, 20);
help2.setBounds(250, 80, 150, 20);
exprsn1.setBounds(00, 80, 30, 30);
exprsn2.setBounds(00, 80, 30, 30);
number1.setBounds(50, 100, 100, 20);
number2.setBounds(250, 100, 100, 20);
result.setBounds(0, 1500, 100, 20);
f.add(number1);
f.add(number2);
f.add(help1);
f.add(help2);
f.add(result);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
f.setSize(600, 300);
f.setVisible(true);
}
public class MyMouseListener extends MouseAdapter {
public void mouseClicked(MouseEvent me) {
String S = number1.getText();
number2.setText(S);
}
}
}
The error is because 3 is not an allowed value for parameter "alignment" in the constructor
Label(String text, int alignment)
Are you getting confused with TextField() which has a similar constructor?
TextField(String text, int columns)
The reason the label is not appearing in the correct location is because you've not specified your using null layout explicitly. You need this line:
public MouseClick() {
Frame f = new Frame("Addition Time!");
f.setLayout(null); // add this line

Categories