www.webdeveloper.com
Results 1 to 15 of 15

Thread: button press help??

  1. #1
    Join Date
    Feb 2006
    Posts
    98

    Exclamation button press help??

    This is my program:>>>>>>>>

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;

    public class Salary {
    public static void main (String [] args) {

    int Button_width = 230;
    int Button_height = 25;

    JLabel IntroLabel = new JLabel (" Welcome to salary sheet");
    final JTextField Introtextfield = new JTextField(15);
    JLabel Intro2Label = new JLabel ("Please select the employee' name that you would like to edit");
    final JTextField Intro2textfield = new JTextField(15);

    JButton Name1button = new JButton("Bob Carey");
    Name1button.setPreferredSize (new Dimension (Button_width, Button_height));

    JButton Name2button = new JButton("Luis Garcia");
    Name2button.setPreferredSize (new Dimension (Button_width, Button_height));

    JButton Name3button = new JButton("Xavi Alonso");
    Name3button.setPreferredSize (new Dimension (Button_width, Button_height));

    JButton Name4button = new JButton("Steven Gerrard");
    Name4button.setPreferredSize (new Dimension (Button_width, Button_height));


    class SaveButtonListener implements ActionListener {

    public void actionPerformed (ActionEvent event) {
    }
    };
    JFrame frame = new JFrame ("Salary Sheet");
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

    frame.setLayout (new GridLayout (6,2));
    frame.add (IntroLabel);
    frame.add (Intro2Label);
    frame.add (Name1button);
    frame.add (Name2button);
    frame.add (Name3button);
    frame.add (Name4button);

    frame.pack ();
    frame.show ();
    }
    }





    Can anyone pls help me wit some sample code so wen i press a button for example the first name (Bob Carey) that another window opens up??
    THANKS

  2. #2
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    u need to create another frame and set its status to viewable. that will show the second window (and this will be done in the event capture method (actionEvent)
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  3. #3
    Join Date
    Feb 2006
    Posts
    98

    Question ok

    So how do i do that. wud u be able to give me an example so wen i press a button another window opens. wats the code?

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
    public class employee {
    public static void main (String[] args) {

    int Button_width = 230;
    int Button_height = 25;

    JLabel IntroLabel = new JLabel ("Hello");
    final JTextField Introtextfield = new JTextField(15);
    JFrame frame = new JFrame ("Salary Sheet");
    class SaveButtonListener implements ActionListener {
    public void actionPerformed (ActionEvent event) {
    }
    };
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    frame.setLayout (new GridLayout (1,2));
    frame.add (IntroLabel);

    frame.pack ();
    frame.show ();
    }
    }

    ......................................................................
    for the above how do i make it possible to call it to open another button wen i press one of the buttons (Bob Carey), How?

  4. #4
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    Here u go. Ofcourse there is allot more to add there but this should answer ur immidiate question..good luck

    PHP Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class 
    Salary extends JFrame implements ActionListener{

        public 
    Salary(){
            
    int Button_width 230;
            
    int Button_height 25;

            
    JLabel IntroLabel = new JLabel(" Welcome to salary sheet");
            final 
    JTextField Introtextfield = new JTextField(15);
            
    JLabel Intro2Label = new JLabel("Please select the employee' name that you would like to edit");
            final 
    JTextField Intro2textfield = new JTextField(15);

            
    JButton Name1button = new JButton("Bob Carey");
            
    Name1button.setPreferredSize(new Dimension(Button_widthButton_height));
            
    Name1button.addActionListener(this);

            
    JButton Name2button = new JButton("Luis Garcia");
            
    Name2button.setPreferredSize(new Dimension(Button_widthButton_height));

            
    JButton Name3button = new JButton("Xavi Alonso");
            
    Name3button.setPreferredSize(new Dimension(Button_widthButton_height));

            
    JButton Name4button = new JButton("Steven Gerrard");
            
    Name4button.setPreferredSize(new Dimension(Button_widthButton_height));

            
    this.setTitle("Salary Sheet");
            
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
    Container cframe this.getContentPane();


            
    cframe.setLayout(new GridLayout(62));
            
    cframe.add(IntroLabel);
            
    cframe.add(Intro2Label);
            
    cframe.add(Name1button);
            
    cframe.add(Name2button);
            
    cframe.add(Name3button);
            
    cframe.add(Name4button);

            
    this.pack();
            
    this.show();
        }

        public 
    void actionPerformed(ActionEvent e) {

            if(
    e.getSource() instanceof JButton ){
                
    JButton jb = (JButtone.getSource();
                if(
    jb.getText().equals("Bob Carey")){
                    
    openNewFrame("This is a second frame.");
                }
            }
        }

        public 
    void openNewFrame(String frameTitle){

            
    JFrame frame = new JFrame(frameTitle);
            
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            
    Container cframe frame.getContentPane();
            
    frame.setSize(new Dimension(200,200));

            
    frame.show();
        }
        
        public static 
    void main(String [] args) {

           new 
    Salary();
        }


    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  5. #5
    Join Date
    Feb 2006
    Posts
    98

    Thumbs up Thanks

    Thanks a span. I really needed that.

  6. #6
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    my pleasure....have fun coding..:-)
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  7. #7
    Join Date
    Feb 2006
    Posts
    98

    Question Another question

    wat part of the code to i have to copy again so that wen i press another name it opens??????


    AND



    is ir possible for my program that wen u press another button the user can create another person? HOW? And that the person inherits everythin the same as the other people there already??

    The code below is wat all employees should get?\/
    *********************************************************
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;

    public class Employee {

    public static void main (String [] args) {

    int Button_width = 65;
    int Button_height = 25;

    JLabel NameLabel = new JLabel ("Name");
    final JTextField Nametextfield = new JTextField(15);
    JLabel BasicSalaryLabel = new JLabel ("Basic salary:");
    final JTextField BasicSalarytextfield = new JTextField (15);
    JLabel LunchAllowLabel = new JLabel ("Lunch Allowance:");
    final JTextField LunchAllowtextfield = new JTextField (15);
    JLabel HousingAllowLabel = new JLabel ("Housing Allowance:");
    final JTextField HousingAllowtextfield = new JTextField (15);
    JLabel TravelAllowLabel = new JLabel ("Travel Allowance:");
    final JTextField TravelAllowtextfield = new JTextField(15);
    JLabel BonusLabel = new JLabel ("Bonus:");
    final JTextField Bonustextfield = new JTextField(15);
    JLabel OvertimeLabel = new JLabel ("Overtime:");
    final JTextField Overtimetextfield = new JTextField(15);
    JLabel GrossSalaryLabel = new JLabel ("Gross Salary:");
    final JTextField GrossSalarytextfield = new JTextField(15);

    JButton Savebutton = new JButton("Save");
    Savebutton.setPreferredSize (new Dimension (Button_width, Button_height));

    JButton Openbutton = new JButton("Open");
    Openbutton.setPreferredSize (new Dimension (Button_width, Button_height));



    class SaveButtonListener implements ActionListener {
    public void actionPerformed (ActionEvent event) {
    try {
    FileWriter writer = new FileWriter ("data.txt");
    PrintWriter out = new PrintWriter (writer);


    String AddName = Nametextfield.getText();
    String AddBasicSalary = BasicSalarytextfield.getText();
    String AddLunchAllow = LunchAllowtextfield.getText();
    String AddHousingAllow = HousingAllowtextfield.getText();
    String AddTravelAllow = TravelAllowtextfield.getText();
    String AddBonus = Bonustextfield.getText();
    String AddOvertime = Overtimetextfield.getText();
    String AddGrossSalary = GrossSalarytextfield.getText();
    out.println (AddName);
    out.println (AddBasicSalary);
    out.println (AddLunchAllow);
    out.println (AddHousingAllow);
    out.println (AddTravelAllow);
    out.println (AddBonus);
    out.println (AddOvertime);
    out.println (AddGrossSalary);
    out.close();
    }

    catch (IOException exception) {

    System.out.println ("can't write to file");
    }

    }
    };

    ActionListener listener = new SaveButtonListener ();
    Savebutton.addActionListener (listener);


    JFrame frame = new JFrame ("Salary Sheet");
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

    frame.setLayout (new GridLayout (10,3));

    frame.add (NameLabel);
    frame.add (Nametextfield);
    frame.add (BasicSalaryLabel);
    frame.add (BasicSalarytextfield);
    frame.add (LunchAllowLabel);
    frame.add (LunchAllowtextfield);
    frame.add (HousingAllowLabel);
    frame.add (HousingAllowtextfield);
    frame.add (TravelAllowLabel);
    frame.add (TravelAllowtextfield);
    frame.add (BonusLabel);
    frame.add (Bonustextfield);
    frame.add (OvertimeLabel);
    frame.add (Overtimetextfield);
    frame.add (GrossSalaryLabel);
    frame.add (GrossSalarytextfield);
    frame.add (Savebutton);
    frame.add (Openbutton);

    frame.pack ();
    frame.show ();

    }
    }
    ********************************************************

  8. #8
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    u will need to add following line for every button for it do something

    Name1button.addActionListener(this);

    once that is done then you will need to define what will it do in the actionPerformed method.

    After that u will need to create another class such as person and it should inherit the parent class that u want.
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  9. #9
    Join Date
    Feb 2006
    Posts
    98

    Talking so...

    i did the first bit.
    Then do i copy this(below) but just change the name to the other name i have????
    AND 2) How wud i call another class to open up in a new window wen i press a button (see Employee). i wanna call that to open wen i press Bob Carey?

    if(e.getSource() instanceof JButton ){
    JButton jb = (JButton) e.getSource();
    if(jb.getText().equals("Bob Carey")){
    openNewFrame("This is a second frame.");
    }
    }

    ****************************************************
    Do u know how o answer my other quetion in which another employee may be added by simply clickin a button??
    Last edited by The One; 02-14-2006 at 08:36 AM. Reason: change

  10. #10
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    yes copy only this bit


    if(jb.getText().equals("Bob Carey")){
    openNewFrame("This is a second frame.");
    }
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  11. #11
    Join Date
    Feb 2006
    Posts
    98

    I tried but..

    i did as u said (Below), and changed the nane to Luis Garcia but wen i run the program and press Luis Garcia, nothin happens?


    public void actionPerformed(ActionEvent e) {

    if(e.getSource() instanceof JButton ){
    JButton jb = (JButton) e.getSource();
    if(jb.getText().equals("Bob Carey")){
    openNewFrame("This is a second frame.");

    if(jb.getText().equals("Luis Garcia")){
    openNewFrame("This is a second frame.");
    }
    }
    }
    }
    }

  12. #12
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    in addition to that, you need to tell the frame that there is an event capture attached to the button.
    Name2button.addActionListener(this);
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  13. #13
    Join Date
    Feb 2006
    Posts
    98

    Red face still..

    I did all the following,:

    JButton Name2button = new JButton("Luis Garcia");
    Name2button.setPreferredSize(new Dimension(Button_width, Button_height));
    Name2button.addActionListener(this);
    .
    .
    .
    .
    public void actionPerformed(ActionEvent e) {

    if(e.getSource() instanceof JButton ){
    JButton jb = (JButton) e.getSource();
    if(jb.getText().equals("Bob Carey")){
    openNewFrame("This is a second frame.");

    if(jb.getText().equals("Luis Garcia")){
    openNewFrame("This is a second frame.");
    }
    }
    }
    }
    }



    ********
    is that all i do? Cos still wen i press Luis Garcia, Nothin Happens????

  14. #14
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    u must be doing something wrong...:-)

    here is the working example

    PHP Code:

     import javax
    .swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class 
    Salary extends JFrame implements ActionListener{

        public 
    Salary(){
            
    int Button_width 230;
            
    int Button_height 25;

            
    JLabel IntroLabel = new JLabel(" Welcome to salary sheet");
            final 
    JTextField Introtextfield = new JTextField(15);
            
    JLabel Intro2Label = new JLabel("Please select the employee' name that you would like to edit");
            final 
    JTextField Intro2textfield = new JTextField(15);

            
    JButton Name1button = new JButton("Bob Carey");
            
    Name1button.setPreferredSize(new Dimension(Button_widthButton_height));
            
    Name1button.addActionListener(this);

            
    JButton Name2button = new JButton("Luis Garcia");
            
    Name2button.setPreferredSize(new Dimension(Button_widthButton_height));
            
    Name2button.addActionListener(this);

            
    JButton Name3button = new JButton("Xavi Alonso");
            
    Name3button.setPreferredSize(new Dimension(Button_widthButton_height));

            
    JButton Name4button = new JButton("Steven Gerrard");
            
    Name4button.setPreferredSize(new Dimension(Button_widthButton_height));

            
    this.setTitle("Salary Sheet");
            
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
    Container cframe this.getContentPane();


            
    cframe.setLayout(new GridLayout(62));
            
    cframe.add(IntroLabel);
            
    cframe.add(Intro2Label);
            
    cframe.add(Name1button);
            
    cframe.add(Name2button);
            
    cframe.add(Name3button);
            
    cframe.add(Name4button);

            
    this.pack();
            
    this.show();
        }

        public 
    void actionPerformed(ActionEvent e) {

            if(
    e.getSource() instanceof JButton ){
                
    JButton jb = (JButtone.getSource();
                if(
    jb.getText().equals("Bob Carey")){
                    
    openNewFrame("This is a second frame.");
                }
                if(
    jb.getText().equals("Luis Garcia")){
                    
    openNewFrame("This is a second frame.");
                }
            }
        }

        public 
    void openNewFrame(String frameTitle){

            
    JFrame frame = new JFrame(frameTitle);
            
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            
    Container cframe frame.getContentPane();
            
    frame.setSize(new Dimension(200,200));

            
    frame.show();
        }

        public static 
    void main(String [] args) {

           new 
    Salary();
        }


    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  15. #15
    Join Date
    Feb 2006
    Posts
    98

    Smile ok

    Alright. I may have missed somethin out ill go check.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles