getting two instances of JDialog - java

I am a creating a certain project in java.
I added a button to sign up the user on one jframe and on button click first jframe dispose and second opens up where i have added a back button to go to first frame and login button. When login button is clicked a jdialog appears.
(I extends the jdialog class)
I have found out that if i click back button and then again click on sign up button, second jframe opens up which is normal but when i click login two instances of jdialog appears.
Here is the code for first JFrame button
frontsignbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new SignUpNew().buildDesign();
frontframe.dispose();
}
});
And for second
login.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
new Login();
}
});
And JDialog code
public class Login extends JDialog {
private JLabel usernametext = new JLabel("User Name");
private JLabel userpasswordtext = new JLabel("Password");
private JLabel message = new JLabel();
private JTextField userfield = new JTextField();
private JPasswordField password = new JPasswordField();
private JButton submit = new JButton("Submit");
{
setTitle("Login");
setLayout(null);
setSize(400, 300);
setLocation(400, 300);
setVisible(true);
setResizable(false);
usernametext.setBounds(20, 20, 80, 10);
userpasswordtext.setBounds(20, 80, 80, 10);
userfield.setBounds(150, 20, 100, 20);
password.setBounds(150, 80, 100, 20);
submit.setBounds(75, 140, 100, 30);
message.setBounds(140, 240, 230, 30);
add(usernametext);
add(userpasswordtext);
add(userfield);
add(password);
add(submit);
add(message);
}
Any idea why this happens. I want to fix this.

Related

What do I do wrong and the second button acts weird?

I know the code is bad and easy. I am a starter and need some help. I want to add a second button to my code. I feel like I changed the coordinates of the button so it doesn't "fall" on the first one. Any suggestions? (Also, .this is a bit unknown to me so I just copy - pasted it hoping it doesnt affect the second button. I feel like it has to do with .this and the coordinates of the button I entered).
import java.awt.*;
import java.awt.event.*;
public class CalculatorGUI extends Frame implements ActionListener
{
static Operand op;
TextField display;
Button button0;
Button button1;
public CalculatorGUI(String title, Operand op)
{
super(title);
CalculatorGUI.op = op;
this.setLayout(null);
this.setFont(new Font("TimesRoman", Font.PLAIN, 14));
this.setBackground(Color.blue);
button0 = new Button("0");
button0.setBounds(64, 265, 35, 28);
button0.setFont(new Font("TimesRoman", Font.PLAIN, 14));
button0.setForeground(Color.blue);
button0.setFocusable(false);
button0.addActionListener(this);
this.add(button0);
this.setSize(283,320);
this.setLocation(40,30);
this.setVisible(true);
this.setResizable(false);
button1 = new Button("1");
button1.setBounds(64, 230, 35, 28);
button1.setFont(new Font("TimesRoman", Font.PLAIN, 14));
button1.setForeground(Color.blue);
button1.setFocusable(false);
button1.addActionListener(this);
this.add(button1);
this.setSize(283,320);
this.setLocation(40,30);
this.setVisible(true);
this.setResizable(false);
this.addWindowListener(new CloseWindowAndExit());
display = new TextField("");
display.setEditable(false);
display.setBounds(13, 55, 257, 30);
this.add(display);
}
class CloseWindowAndExit extends WindowAdapter
{
public void windowClosing(WindowEvent closeWindowAndExit)
{
System.exit(0);
}
}
#Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button0)
{
display.setText("Button0 is pressed");
CalculatorGUI.op.addDigit('0');
}
}
}
Do not add buttons directly to the main frame. Instead, create another panel and add the buttons to it:
Panel newPanel = new Panel(new GridBagLayout());
Button button0 = new Button("BUTTON_0");
button0.setBounds(10, 265, 30, 30);
button0.setFont(new Font("TimesRoman", Font.PLAIN, 14));
button0.setForeground(Color.blue);
button0.setFocusable(false);
Button button1 = new Button("BUTTON_1");
button1.setBounds(10, 265, 30, 30);
button1.setFont(new Font("TimesRoman", Font.PLAIN, 14));
button1.setForeground(Color.blue);
button1.setFocusable(false);
newPanel.add(button0);
newPanel.add(button1);
this.add(newPanel);

JButton - No Click Anmation

i read some contributions about this but nothing helped.
I try to make a little application with a gui,
but the problem is, if i click the button, no animation appears and nothing happens. I hope you can help me.
That's the code:
public class StartFrame extends JFrame{
JTextField eingabe;
JLabel inhalt;
JButton button;
JCheckBox fett;
JCheckBox kursiv;
JCheckBox groß;
JPanel panel;
StartFrame(int sizeWidth, int sizeHeight, String title){
setSize(sizeWidth, sizeHeight);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle(title);
setLocationRelativeTo(null);
setLayout(null);
inhalt = new JLabel("TeXt");
inhalt.setBounds(0, 0, 500, 60);
Font font = inhalt.getFont().deriveFont(Font.ITALIC, 15);
inhalt.setFont(font);
inhalt.setToolTipText("Das ist ein Text");
add(inhalt);
button = new JButton("HIER");
button.setBounds(100, 10, 100, 50);
button.addActionListener(new ClickListener());
button.setEnabled(false);
add(button);
eingabe = new JTextField();
eingabe.setBounds(300, 50, 150, 25);
eingabe.addCaretListener(new SchreibkopfListener());
add(eingabe);
panel = new JPanel();
panel.setLayout(null);
panel.setBounds(10, 200, 150, 100);
add(panel);
fett = new JCheckBox("Fett");
fett.setBounds(0, 0, 150, 25);
fett.addItemListener(new FettListener());
panel.add(fett);
kursiv = new JCheckBox("Kursiv");
kursiv.setBounds(0, 25, 150, 25);
panel.add(kursiv);
groß = new JCheckBox("Groß");
groß.setBounds(0, 50, 150, 25);
panel.add(groß);
setVisible(true);
}
private class ClickListener implements ActionListener{
#Override
public void actionPerformed(ActionEvent arg0) {
inhalt.setText(eingabe.getText());
}
}
private class SchreibkopfListener implements CaretListener{
#Override
public void caretUpdate(CaretEvent arg0) {
String inTextField = eingabe.getText();
inTextField = inTextField.trim();
if(inTextField.isEmpty()){
button.setEnabled(false);
}else{
button.setEnabled(true);
}
}
}
private class FettListener implements ItemListener{
#Override
public void itemStateChanged(ItemEvent arg0) {
if(fett.isSelected()){
Font font = inhalt.getFont().deriveFont(Font.BOLD, 15);
inhalt.setFont(font);
}else{
Font font = inhalt.getFont().deriveFont(Font.ITALIC, 15);
inhalt.setFont(font);
}
}
}
}
Here's a wonderful lesson into why null layouts suck (and you should stop using them)
Let's just take a little look at these lines...
nhalt = new JLabel("TeXt");
inhalt.setBounds(0, 0, 500, 60);
inhalt.setToolTipText("Das ist ein Text");
//...
button = new JButton("HIER");
button.setBounds(100, 10, 100, 50);
So, the label starts at 0x0 and expands through 500x60, okay, the button starts at 100x10 and expands through 100x50, this means that the label and the button actually collide
Now, I know what you're thinking, but I add the button after the label, but this isn't how components actually get painted/respond to events.
Components are painted in reverse order of how they are added, this means that the label actually resides OVER the button (in terms of the Z-order), this also means that the label receives events BEFORE the button, this is important because...
inhalt.setToolTipText("Das ist ein Text");
installs a MouseListener on the JLabel, which consumes ALL mouse events, preventing the button from been notified.
So, long answer short, don't use null layouts (and take the time to better understand how the API actually works :P)

Adding components by constructor

I have problem with adding Label by constructor, when I make it by method it's no problem
private void addLabel() {
System.out.println("asd");
JLabel label = new JLabel("asd");
label.setBounds(10, 40, 100, 25);
add(label);
repaint();
validate();
System.out.println("asd2");
}
But when i try to do this same by new class and constructor i doesn't work...
Main frame:
public class Frame extends JFrame {
JButton button = new JButton("new");
AddButton button2 = new AddButton();
public Frame() {
setLayout(null);
setSize(400, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
button.setBounds(40, 10, 50, 25);
add(button);
button2.setBounds(40, 40, 100, 25);
add(button);
}
public static void main(String[] args) {
Frame ap = new Frame();
ap.setVisible(true);
}
AddButton class:
public class AddButton extends JPanel {
JLabel label = new JLabel("asd");
public AddButton() {
label.setBounds(10, 40, 100, 25);
add(label);
repaint();
validate();
}
}
Ok i got it, I tried to add "button" two times istead button and button2 :D
Your constructor doesn't make sense, that's not how you should use constructors - constructors are used to create an instance of a class.
When you write
AddButton button2 = new AddButton();
then button2 is of type AddButton, and add doesn't accept this type of object.
You can Edit like this
public class AddButton extends JPanel {
JLabel label;
public AddButton() {
label=new JLabel("asd");
label.setBounds(10, 40, 100, 25);
add(label);
repaint();
validate();
}
}

RE-enable a button after closing a frame java

so im making a program for my project.
and when i clicked a button it must open anohter frame and make the button unclickable. and when you closed the popup frame the button must re enable. so this is
my main frame
package Option2;
import javax.swing.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MainMenu {
int intCtr1 = 0;
JFrame frame1 = new JFrame("EXD LAN PARTY");
JButton Button1 = new JButton();
JButton Button2 = new JButton();
JButton Button3 = new JButton();
JButton Button4 = new JButton();
JLabel Label1 = new JLabel();
public void MainMenu(){
//BUTTON1
Button1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/PI2.jpg")));
Button1.setBackground(Color.white);
Button1.setBounds(50, 350, 150, 150);
Button1.setToolTipText("Personal Info");
//BUTTON1 END
//BUTTON2
Button2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/PC.jpg")));
Button2.setBackground(Color.white);
Button2.setBounds(250, 350, 150, 150);
Button2.setToolTipText("PC INFO");
//BUTTON2 END
//BUTTON3
Button3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/Games.jpg")));
Button3.setBackground(Color.white);
Button3.setBounds(450, 350, 150, 150);
Button3.setToolTipText("Games");
//BUTTON3 END
//BUTTON4 END
Button4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/Players.jpg")));
Button4.setBackground(Color.white);
Button4.setBounds(650, 350, 150, 150);
Button3.setToolTipText("Players");
//BUTTON4 END
//LABEL1
Label1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/EXD.jpg")));
Label1.setBounds(50, 50, 800, 250);
//LABEL1 END
//Frame1
frame1.getContentPane().setBackground(Color.black);
frame1.setResizable(false);
frame1.setLayout(null);
frame1.setSize(870,650);
frame1.setVisible(true);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.add(Label1);
frame1.add(Button1);
frame1.add(Button2);
frame1.add(Button3);
frame1.add(Button4);
//Frame1 END
Button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
PersonalInfo objPI = new PersonalInfo();
objPI.Menu1();
Button1.setEnabled(false);
}
});
Button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
PCInfo objPCI = new PCInfo();
objPCI.Menu2();
}
});
Button3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
Games objGames = new Games();
objGames.Menu3();
}
});
Button4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
Players objPlayers = new Players();
objPlayers.Menu4();
}
});
}
public void dim1(){
if(intCtr1 == 1){
MainMenu objMM = new MainMenu();
objMM.Button1.setEnabled(true);
System.out.println("SD");
}
}
}
**and this is my sub frame**
package Option2;
import javax.swing.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PersonalInfo {
String[] arrSex = {"Male","Female"};
JFrame frame1 = new JFrame("Personal Info");
JLabel label1 = new JLabel("ID");
JLabel label2 = new JLabel("Last Name");
JLabel label3 = new JLabel("First Name");
JLabel label4 = new JLabel("Middle Name");
JLabel label5 = new JLabel("SEX");
JLabel label6 = new JLabel();
JTextField tf1 = new JTextField();
JTextField tf2 = new JTextField();
JTextField tf3 = new JTextField();
JTextField tf4 = new JTextField();
JComboBox CB1 = new JComboBox(arrSex);
JButton Button1 = new JButton("NEW");
JButton Button2 = new JButton("SAVE");
JButton Button3 = new JButton("EDIT");
JButton Button4 = new JButton("CANCEL");
JButton Button5 = new JButton();
JButton Button6 = new JButton();
JButton Button7 = new JButton();
JButton Button8 = new JButton();
public void Menu1(){
//Frame1
frame1.add(label6);
frame1.add(label1);
frame1.add(tf1);
frame1.add(label2);
frame1.add(tf2);
frame1.add(label3);
frame1.add(tf3);
frame1.add(label4);
frame1.add(tf4);
frame1.add(label5);
frame1.add(CB1);
frame1.add(Button1);
frame1.add(Button2);
frame1.add(Button3);
frame1.add(Button4);
frame1.add(Button5);
frame1.add(Button6);
frame1.add(Button7);
frame1.add(Button8);
frame1.setVisible(true);
frame1.getContentPane().setBackground(Color.black);
frame1.setSize(600,600);
frame1.setResizable(false);
frame1.setLayout(null);
frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame1.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
MainMenu objMM = new MainMenu();
objMM.intCtr1=1;
objMM.dim1();
}
});
//Frame1 End
//LABEL6
label6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/PI.jpg")));
label6.setBounds(30, 5, 800, 250);
//LABEL6 END
//LABEl1
label1.setBounds(100, 220, 50,50);
label1.setForeground(Color.white);
label1.setFont(new Font("Serif", Font.BOLD, 18));
//Label1 end
//Tf
tf1.setBounds(130, 230, 400,30);
tf1.setEnabled(false);
SmartC objSMC = new SmartC();
tf1.setText(objSMC.SmartCounter("ABC123415XYZS"));
//tf end
//label2
label2.setBounds(35, 255, 120,50);
label2.setForeground(Color.white);
label2.setFont(new Font("Serif", Font.BOLD, 18));
//label2 end
//Tf2
tf2.setBounds(130, 270, 400,30);
//tf2 end
//label3
label3.setBounds(35, 295, 120,50);
label3.setForeground(Color.white);
label3.setFont(new Font("Serif", Font.BOLD, 18));
//label3 end
//Tf3
tf3.setBounds(130, 310 , 400, 30);
//tf3 end
//label4
label4.setBounds(15, 335, 120,50);
label4.setForeground(Color.white);
label4.setFont(new Font("Serif", Font.BOLD, 18));
//label4 end
//Tf4
tf4.setBounds(130, 350 , 400, 30);
//tf4 end
//label4
label5.setBounds(85, 375, 120,50);
label5.setForeground(Color.white);
label5.setFont(new Font("Serif", Font.BOLD, 18));
//label4 end
//cb1
CB1.setBounds(130, 390, 100, 30);
CB1.setBackground(Color.white);
//cb1 end
//button1
Button1.setBounds(35, 450, 100, 30);
Button1.setBackground(Color.white);
//
//
Button2.setBounds(150, 450, 100, 30);
Button2.setBackground(Color.white);
//
//
Button3.setBounds(335, 450, 100, 30);
Button3.setBackground(Color.white);
//
//
Button4.setBounds(450, 450, 100, 30);
Button4.setBackground(Color.white);
//
//
Button5.setBounds(35, 500, 100, 50);
Button5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/First.jpg")));
Button5.setBackground(Color.white);
//
//
Button6.setBounds(150, 500, 100, 50);
Button6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/Previous.jpg")));
Button6.setBackground(Color.white);
//
//
Button7.setBounds(335, 500, 100, 50);
Button7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/Next.jpg")));
Button7.setBackground(Color.white);
//
//
Button8.setBounds(450, 500, 100, 50);
Button8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/Last.jpg")));
Button8.setBackground(Color.white);
//
//
}
}
Any Suggestions about my coding is accepted. Sorry Still learning about Java :)
and when i clicked a button it must open anohter frame
You should NOT be creating another JFrame.
Instead you should be creating a modal JDialog. The dialog will not allow you to click on the frame until the dialog is closed.
Any Suggestions about my coding is accepted
Follow Java naming conventions. Variable names should NOT start with an upper case character. Sometimes you follow this guideline and sometimes you don't. Be consistent!
Don't use setBounds(...). Swing was designed to be used with layout managers!
First, please read Java naming conventions.
You need to pass the reference of MainMenu to PersonalInfo in order to achieve what you need, here:
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
PersonalInfo objPI = new PersonalInfo(this);
objPI.menu1();
button1.setEnabled(false);
}
});
And you need to add a constructor to PersonalInfo:
private MainMenu m;
public PersonalInfo(MainMenu m) {
this.m = m;
}
Add a public method to MainMenu:
public void enableMyButton() {
button1.setEnabled(true);
}
Now you can add an event listener to PersonalInfo to enable the button of the MainMenu frame:
frame1.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
this.m.enableMyButton();
}
});

How should I make a "back button" to switch JPanels on a main JFrame class work with multiple JPanel classes?

I'm currently working on a game for school, and I've hit a brick wall. I've created a main class that sets up the JFrame, and in that JFrame have JPanel buttons that open a server JPanel class, a client JPanel class, and buttons for options, and exiting the game. Now where I'm stuck is how I should make buttons to go back to the main JPanel using a back button on the server/client JPanel class. Here's the code I have at the moment:
MainUI.class (a different class runs this):
public class MainUI extends JFrame {
private JPanel contentPane;
private JTextField textField;
public MainUI() {
// Sets up the frame
setTitle("Pong Legacy | Prototype v0.1.0");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 500);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
// Starts the Server window
JButton btnStartServer = new JButton("Start Server");
btnStartServer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JPanel server = new ServerUI();
getContentPane().removeAll();
getContentPane().setLayout(new BorderLayout());
getContentPane().add(server);
getContentPane().validate();
getContentPane().repaint();
}
});
btnStartServer.setBounds(97, 364, 100, 25);
contentPane.add(btnStartServer);
// Starts the Client window
JButton btnStartClient = new JButton("Start Client");
btnStartClient.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JPanel client = new ClientUI();
getContentPane().removeAll();
getContentPane().setLayout(new BorderLayout());
getContentPane().add(client);
getContentPane().validate();
getContentPane().repaint();
}
});
btnStartClient.setBounds(97, 400, 100, 25);
contentPane.add(btnStartClient);
// Opens the Options menu
// (To Do)
JButton btnOptions = new JButton("Options");
btnOptions.setBounds(37, 436, 100, 25);
contentPane.add(btnOptions);
// Quits the game
JButton btnQuitGame = new JButton("Quit Game");
btnQuitGame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btnQuitGame.setBounds(157, 436, 100, 25);
contentPane.add(btnQuitGame);
// Username Field
JLabel lblUsername = new JLabel("Username:");
lblUsername.setBounds(121, 45, 52, 14);
contentPane.add(lblUsername);
textField = new JTextField();
textField.setBounds(104, 67, 86, 20);
contentPane.add(textField);
textField.setColumns(10);
}
}
ServerUI.class:
public class MainUI extends JFrame {
private JPanel contentPane;
private JTextField textField;
public MainUI() {
// Sets up the frame
setTitle("Pong Legacy | Prototype v0.1.0");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 500);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
// Starts the Server window
JButton btnStartServer = new JButton("Start Server");
btnStartServer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JPanel server = new ServerUI();
getContentPane().removeAll();
getContentPane().setLayout(new BorderLayout());
getContentPane().add(server);
getContentPane().validate();
getContentPane().repaint();
}
});
btnStartServer.setBounds(97, 364, 100, 25);
contentPane.add(btnStartServer);
// Starts the Client window
JButton btnStartClient = new JButton("Start Client");
btnStartClient.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JPanel client = new ClientUI();
getContentPane().removeAll();
getContentPane().setLayout(new BorderLayout());
getContentPane().add(client);
getContentPane().validate();
getContentPane().repaint();
}
});
btnStartClient.setBounds(97, 400, 100, 25);
contentPane.add(btnStartClient);
// Opens the Options menu
// (To Do)
JButton btnOptions = new JButton("Options");
btnOptions.setBounds(37, 436, 100, 25);
contentPane.add(btnOptions);
// Quits the game
JButton btnQuitGame = new JButton("Quit Game");
btnQuitGame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btnQuitGame.setBounds(157, 436, 100, 25);
contentPane.add(btnQuitGame);
// Username Field
JLabel lblUsername = new JLabel("Username:");
lblUsername.setBounds(121, 45, 52, 14);
contentPane.add(lblUsername);
textField = new JTextField();
textField.setBounds(104, 67, 86, 20);
contentPane.add(textField);
textField.setColumns(10);
}
}
ClientUI.class:
public class ClientUI extends JPanel {
private JTextField textField;
public ClientUI() {
setLayout(null);
JButton btnConnect = new JButton("Connect");
btnConnect.setBounds(47, 400, 200, 25);
add(btnConnect);
JButton btnBack = new JButton("Back");
btnBack.setBounds(117, 436, 60, 25);
add(btnBack);
JRadioButton rdbtnSelectAServer = new JRadioButton("Select a server from the list:");
rdbtnSelectAServer.setBounds(66, 25, 161, 25);
add(rdbtnSelectAServer);
JRadioButton rdbtnManualConnection = new JRadioButton("Manual Connection:");
rdbtnManualConnection.setBounds(87, 325, 120, 25);
add(rdbtnManualConnection);
textField = new JTextField();
textField.setBounds(47, 355, 200, 25);
add(textField);
textField.setColumns(10);
}
}
I've heard that I could use a CardLayout, too, but I want to see if I can do it this way.

Categories