1

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 :)

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Please read [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice) – DavidPostill Aug 23 '14 at 08:39

2 Answers2

4

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!

camickr
  • 321,443
  • 19
  • 166
  • 288
1

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();
                }
            });
Jaec
  • 390
  • 2
  • 8
  • 18
  • HELP! This PersonalInfo objPI = new PersonalInfo(this); wants me to add this constructor PersonalInfo(ActionListener actionListener) not this public void PersonalInfo(MainMenu m) and also a problem in my Window Listener. this.m.enableMyButton(); wants me to declare this PersonalInfo.this.m.enableMyButton(); – Joorj Pagarigan Aug 23 '14 at 09:18
  • It's not public void PersonalInfo(MainMenu m), it's public PersonalInfo(MainMenu m). A constructor, not a method. – Jaec Aug 24 '14 at 17:44
  • ow :)) Now the only error is this part public void windowClosing(WindowEvent e){ ** this.m.enableMyButton(); ** and this part. PersonalInfo objPI = new PersonalInfo(this); – Joorj Pagarigan Aug 28 '14 at 15:12