Click to See Complete Forum and Search --> : button press help??


The One
02-11-2006, 03:28 PM
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

Khalid Ali
02-13-2006, 12:38 PM
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)

The One
02-13-2006, 12:43 PM
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?

Khalid Ali
02-13-2006, 02:03 PM
Here u go. Ofcourse there is allot more to add there but this should answer ur immidiate question..good luck


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_width, Button_height));
Name1button.addActionListener(this);

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

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


cframe.setLayout(new GridLayout(6, 2));
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 = (JButton) e.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();
}

}

The One
02-13-2006, 02:17 PM
Thanks a span. I really needed that.

Khalid Ali
02-13-2006, 05:20 PM
my pleasure....have fun coding..:-)

The One
02-14-2006, 06:45 AM
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 ();

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

Khalid Ali
02-14-2006, 07:05 AM
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.

The One
02-14-2006, 09:11 AM
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??

Khalid Ali
02-14-2006, 10:12 AM
yes copy only this bit


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

The One
02-14-2006, 11:01 AM
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.");
}
}
}
}
}

Khalid Ali
02-14-2006, 12:52 PM
in addition to that, you need to tell the frame that there is an event capture attached to the button.
Name2button.addActionListener(this);

The One
02-14-2006, 01:21 PM
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????

Khalid Ali
02-14-2006, 01:47 PM
u must be doing something wrong...:-)

here is the working example



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_width, Button_height));
Name1button.addActionListener(this);

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

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

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


cframe.setLayout(new GridLayout(6, 2));
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 = (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.");
}
}
}

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();
}

}

The One
02-14-2006, 02:50 PM
Alright. I may have missed somethin out ill go check.