JButton btnNewButton = new JButton("Register Student");
btnNewButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
RegisterStudent panel_1 = new RegisterStudent();
panel_1.setVisible(true);
}
});
btnNewButton.setBounds(0, 162, 167, 37);
panel.add(btnNewButton);
Is there a way, that IF one specific window is open already, it cant be opened once again?
Because, i don't want the user to click on a button several times, causing several windows to be opened with the same content?
Create the panel_1 variable outside of the mouse listener block and initialize it to null. When the mouse is clicked, check if panel_1 is null, and if it is, create it.
final RegisterStudent panel_1 = new RegisterStudent();
JButton btnNewButton = new JButton("Register Student");
btnNewButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
panel_1.setVisible(true);
}
});
btnNewButton.setBounds(0, 162, 167, 37);
panel.add(btnNewButton);
You can get an array of all open windows from Window.getWindows() since 1.6 or all open Frames with Frame.getFrames() since 1.2. You can use the name property or the window class (RegisterStudent) to test if the windows is already open and set the focus on it instead open another one.
Related
I would have a problem with the edges of my jbutton. In practice, in the code below I inserted a button that should not have edges but instead appear as in the photo below.
JButton btnRes = new JButton();
btnRes.setBorderPainted(false);
btnRes.setContentAreaFilled(false);
btnRes.setOpaque(false);
btnRes.setBorder(null);
btnRes.setIcon(new ImageIcon(Main.class.getResource(image1)));
btnRes.setPressedIcon(new ImageIcon(Main.class.getResource(image2)));
btnRes.setRolloverIcon(new ImageIcon(Main.class.getResource(image3)));
btnRes.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//TODO
}
});
btnRes.setBounds(496, 342, 138, 48);
frame.getContentPane().add(btnRes);
and this is the result:
Image of this JButton
The border is however visible, how I can fix this?
You see the border that is added to the button because it is selected. Try:
btnRes.setFocusPainted(false);
My java program is opening new window when i press one button in first window. Then i need to close first window. When i try to close first window with System.exit(0); it closes second window. I tryed setVisible(false); then first window doesn't close. Please help!
Whole code:
public class NameChooser extends JFrame implements ActionListener {
public NameChooser() {
RunNC();
}
public final void RunNC() {
JPanel panel = new JPanel();
JLabel label = new JLabel("Enter your name that will be shown in game!");
label.setBounds(20, 10, 500, 25);
panel.add(label);
JLabel error = new JLabel("");
error.setForeground(Color.red);
panel.add(error);
JTextField name = new JTextField(30);
name.setBounds(50, 40, 180, 25);
panel.add(name);
JButton playButton = new JButton( new AbstractAction("Play") {
#Override
public void actionPerformed(ActionEvent e) {
String enteredname = name.getText();
if("".equals(enteredname)) {
error.setVisible(true);
error.setText("Invalid name!");
error.setBounds(105, 95, 100, 25);
System.out.println("Invalid name!");
}
else if(enteredname.length() > 10) {
error.setVisible(true);
error.setText("Name cant have more than 10 characters!");
error.setBounds(25, 95, 600, 25);
System.out.println("Name cant have more than 10 characters!");
}
error.setVisible(false);
GameWindow game = new GameWindow();
game.StartGame();
// I need to close window on this line!
}
});
playButton.setBounds(110, 70, 60, 25);
panel.add(playButton);
}
Any help?
Your problem is caused because System.exit() causes the Java VM to terminate completely - and both of your windows are running on the same VM instance.
Use Jframe.dispose() instead on the one you want to close.
System.exit(0);
doesn't "close a window", it terminates the JVM in which your entire application is running, hence, it terminates the entire application.
If we're talking about JFrames, try by using the dispose() method to close your seperate screens.
If you want us to comment on your setVisible(false), show us the code where you call it, it might be you're calling it on the wrong variable.
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));
I have created a JComboBox in order to add players to a list as I am creating a game. I am trying to show an error message using JOptionPane if the text field is left empty I did this using the method below.
btnAddPlayer = new JButton("Add Player");
btnAddPlayer.addActionListener(new ActionListener() { //This is the layout for the list of points that are possible to achieve
public void actionPerformed(ActionEvent arg0) {
if (txtAddPlayer.equals("")){
JOptionPane.showMessageDialog(btnAddPlayer, this, "Please Enter Full Details", NumofAnswers);//THIS IS THE METHOD I TRIED
} else {
comboBox.addItem(txtAddPlayer.getText());
}
}
});
btnAddPlayer.setBounds(469, 243, 89, 23);
panel.add(btnAddPlayer);
txtAddPlayer = new JTextField();
txtAddPlayer.setBounds(373, 244, 86, 20);
panel.add(txtAddPlayer);
txtAddPlayer.setColumns(10);
I am not sure why this is not working. Please provide an answer using my code.
Regards,
Should be
If (textField.gettext().equals(""))
After the password is entered i want the window to disappear and pop a new window.
JButton btnEnter = new JButton("Enter");
btnEnter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
if(passwordField.equals("test"))
{
frame.setVisible(false);
}
else if(!passwordField.equals("test"))
{
JOptionPane.showMessageDialog(null,"Access Denied!!");
}
}
});
btnEnter.setBounds(149, 184, 117, 29);
frame.getContentPane().add(btnEnter);
I'm assuming passwordField is a JTextField, if so, you need to get the text from it, just .getText() I think and store that in a string. Then test the string. At the moment you are testing if your JTextField equals the string.
Create 2 JFrames and make a reference for each one:
JFrame oldFrame = new JFrame();
// ...
JFrame newFrame = new JFrame();
// ...
// ...
if(passwordField.equals("test"))
{
oldFrame.setVisible(false);
newFrame.setVisible(true);
}