School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,144 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 3,811 people online right now. Registration is fast and FREE... Join Now!



Gui need help on so explaintion

Gui need help on so explaintion Rate Topic: -----

#1 kill99  Icon User is offline

  • D.I.C Head
  • PipPip
  • Group: Members
  • Posts: 53
  • Joined: 03-May 08


Dream Kudos: 0

Posted 05 April 2009 - 03:56 AM

/**
 * @(#)CompanyDriver.java
 *
 *
 * @author
 * @version 1.00 2009/3/24
 */

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

public class CompanyDriver extends JFrame implements ActionListener{

	JTextArea main;
	JScrollPane jsp;
	JButton addEmpBtn,addManBtn,delEmpBtn,searchEmpBtn,dispEmpBtn,endBtn,chgGrdBtn,allowBtn,SalBtn;
	JTextField delEmpJtf,searchEmpJtf,chgGrdJtf,allowJtf,getIdJtf;
	JPanel buttonContainer,bcA,bcB,bcC,bcD;
	Company fedX;

	public CompanyDriver(){
		fedX = new Company();

		main = new JTextArea();
		main.setEditable(false);
		main.append(fedX.menu());
		jsp = new JScrollPane(main);

		buttonContainer = new JPanel(new GridLayout(6,1));
		//initialise Buttons and add to button Container
		buttonContainer.add(addEmpBtn = new JButton("Add Employee"));
		buttonContainer.add(addManBtn = new JButton("Add Manager"));
		bcA = new JPanel();
		bcB = new JPanel();
		bcC = new JPanel();
		bcD = new JPanel();

		bcA.add(delEmpJtf = new JTextField(2));
		bcA.add(delEmpBtn = new JButton("Remove Employee"));

		bcB.add(searchEmpJtf = new JTextField(2));
		bcB.add(searchEmpBtn = new JButton("Search Employee"));

		bcC.add(chgGrdJtf = new JTextField(2));
		bcC.add(chgGrdBtn = new JButton("Change Grade"));
		bcC.add(getIdJtf = new JTextField(2));

		bcD.add(allowJtf = new JTextField(2));
		bcD.add(allowBtn = new JButton("Add Allowance"));
		buttonContainer.add(bcA);
		buttonContainer.add(bcB);
		buttonContainer.add(bcC);
		buttonContainer.add(dispEmpBtn = new JButton("Display All Employee(s)"));
		buttonContainer.add(bcD);
		buttonContainer.add(SalBtn = new JButton("Increase Salary"));
		buttonContainer.add(endBtn = new JButton("Exit Program"));

		getContentPane().add(main);
		getContentPane().add(buttonContainer,"East");

		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(500,250);
		setVisible(true);
		setTitle("Company Creator");


		addEmpBtn.addActionListener(this);
		addManBtn.addActionListener(this);
		delEmpBtn.addActionListener(this);
		searchEmpBtn.addActionListener(this);
		chgGrdBtn.addActionListener(this);
		dispEmpBtn.addActionListener(this);
		allowBtn.addActionListener(this);
		SalBtn.addActionListener(this);
		endBtn.addActionListener(	new ActionListener(){
				public void actionPerformed(ActionEvent e){
					System.exit(0);
				}});

		//add listener to close program fully when closed..
		addWindowListener(
		 new WindowAdapter( ) {
			public void windowClosing( WindowEvent e )
			   {
			  System.exit( 0 );
			   }
		 }
		  );
	}

	public void actionPerformed(ActionEvent e){
		if(e.getActionCommand() == addEmpBtn.getText()){ //if add Employee button is pressed
			new AddStaff(0);
		}
		else if(e.getActionCommand() == addManBtn.getText()){ //if add Employee button is pressed
			new AddStaff(1);
		}
		else if(e.getActionCommand() == delEmpBtn.getText()){ //if delete Employee button is pressed
			int delId = Integer.parseInt(delEmpJtf.getText());
			if(fedX.searchEmployee(delId) != null){
				main.setText("Deleting "+fedX.searchEmployee(delId));
				fedX.deleteEmployee(delId);
			}
			else{
				main.setText("User not found, delete failed");
			}



		}
		else if(e.getActionCommand() == searchEmpBtn.getText()){ //if search Employee button is pressed
		int serId = Integer.parseInt(searchEmpJtf.getText());
			if(fedX.searchEmployee(serId) != null){
				main.setText("User found\n "+fedX.searchEmployee(serId));
			}
			else{
				main.setText("Unable to find User");
			}
		}
		else if(e.getActionCommand() == chgGrdBtn.getText()){ //Change Grade button is pressed
			int chgId = Integer.parseInt(chgGrdJtf.getText());
				if(fedX.searchEmployee(chgId) != null){
				char grade = chgGrdJtf.getText().charAt(0);
				char id = getIdJtf.getText().charAt(0);
					main.setText("changing grade\n "+fedX.searchEmployee(chgId));

				}
				else{
					main.setText("Please Enter a Valid ID");
				}
			}


		else if(e.getActionCommand() == dispEmpBtn.getText()){ //if display all Employee button is pressed
			if(fedX.getEmpCount()>0){
				main.setText(fedX.displayAllEmployee());
			}
			else{
				main.setText("Database has no Employee yet");
			}
		}

		main.append("\n"+fedX.menu());

	}


	class AddStaff extends JFrame implements ActionListener{
		//set labels
		String[] labels = {"First Name","Last Name","Salary","Grade","Department"		};
		//Type:
		// 0 = Employee
		// 1 = Manager
		int type;
			JTextField[] jtf = new JTextField[5];
			JLabel[] jlb = new JLabel[5];
		public AddStaff(int typ){
			this.type = typ;
			JPanel jp1 = new JPanel(new GridLayout(6,2));


			for(int i = 0;i<5;i++){
				jtf[i] = new JTextField(10);//initialise textfield
				jlb[i] = new JLabel(labels[i]);//initialise labels according to labels
				if(i<4){ //insert for all employees
				jp1.add(jlb[i]);//insert label first
				jp1.add(jtf[i]);//insert textfield
				}
				if(type==1 && i == 4){//if is manager, include this field
					jp1.add(jlb[i]);
					jp1.add(jtf[i]);
				}
			}
			JButton okBtn = new JButton("Ok");
			JButton cancel = new JButton("Cancel");
			jp1.add(okBtn);
			jp1.add(cancel);
			getContentPane().add(jp1);

			if(type == 0)
				setTitle("Add Employee");
			else if(type == 1)
				setTitle("Add Manager");
			setSize(200,200);
			setVisible(true);

			cancel.addActionListener(	new ActionListener(){
				public void actionPerformed(ActionEvent e){
					setVisible(false);
				}});
			okBtn.addActionListener(this);
		}
		public void actionPerformed(ActionEvent e){
			if(type == 0){
				//required field is string, string, double, char
				fedX.addEmployee(jtf[0].getText(),jtf[1].getText(),Double.parseDouble(jtf[2].getText()),jtf[3].getText().charAt(0));
				main.setText("Employee Added");
			}
			if(type == 1){
				fedX.addManager(jtf[0].getText(),jtf[1].getText(),Double.parseDouble(jtf[2].getText()),jtf[3].getText().charAt(0),jtf[4].getText());
				main.setText("Manager Added");
			}
			setVisible(false);
		}
	}
	public static void main(String[] args){
		new CompanyDriver();
	}



}



i having error on this part any can help me and explain to me what i did wrong

else if(e.getActionCommand() == chgGrdBtn.getText()){ //Change Grade button is pressed
			int chgId = Integer.parseInt(chgGrdJtf.getText());
				if(fedX.searchEmployee(chgId) != null){
				char grade = chgGrdJtf.getText().charAt(0);
				char id = getIdJtf.getText().charAt(0);
					main.setText("changing grade\n "+fedX.searchEmployee(chgId));

				}
				else{
					main.setText("Please Enter a Valid ID");
				}
			}


Was This Post Helpful? 0
  • +
  • -


#2 Gloin  Icon User is offline

  • Expert Schmexpert...
  • Icon
  • Group: Mentors
  • Posts: 4,130
  • Joined: 04-August 08


Dream Kudos: 75

Posted 05 April 2009 - 04:02 AM

Does it compile?
Do you get any error-messages?
Do you get any output?
What output do you expect?
Did you try to debug?

Hard to solve your problem when you don't state what the problem is.
Was This Post Helpful? 0
  • +
  • -

#3 kill99  Icon User is offline

  • D.I.C Head
  • PipPip
  • Group: Members
  • Posts: 53
  • Joined: 03-May 08


Dream Kudos: 0

Posted 05 April 2009 - 04:47 AM

View PostGloin, on 5 Apr, 2009 - 04:02 AM, said:

Does it compile?
Do you get any error-messages?
Do you get any output?
What output do you expect?
Did you try to debug?

Hard to solve your problem when you don't state what the problem is.



Its able to compile and there is no error message able to run but when click on the change grade nothing happen no sure which part did i done wrong

the output i expect is when i search the id and click on the change grade will prompt me to enter the new grade and change it
Was This Post Helpful? 0
  • +
  • -

#4 Gloin  Icon User is offline

  • Expert Schmexpert...
  • Icon
  • Group: Mentors
  • Posts: 4,130
  • Joined: 04-August 08


Dream Kudos: 75

Posted 05 April 2009 - 04:59 AM

My recommendation would be to make a simple debug.
Insert these two lines into the code.

System.out.println(e.getActionCommand());
System.out.println(chgGrdBtn.getText());



put them at the very top of your actionPerformed-method to see if they really match. If they don't, then you got a simple solution to your problem.

Further, you can always insert lines like that to check that you actually have the values in your variables that you think are there. Often something else has ended up there by mistake and you can usually deduce where things went bad.

Good luck, post again if you find the problem or if you don't. :)
Was This Post Helpful? 1
  • +
  • -

#5 kill99  Icon User is offline

  • D.I.C Head
  • PipPip
  • Group: Members
  • Posts: 53
  • Joined: 03-May 08


Dream Kudos: 0

Posted 05 April 2009 - 05:22 AM

Change Grade
Change Grade
Change Grade
Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException
: String index out of range: 0

it prompt this what this means ?
Was This Post Helpful? 0
  • +
  • -

#6 Gloin  Icon User is offline

  • Expert Schmexpert...
  • Icon
  • Group: Mentors
  • Posts: 4,130
  • Joined: 04-August 08


Dream Kudos: 75

Posted 05 April 2009 - 05:39 AM

The exception is actually due to the System.out.println's. They cause the program to retreive what's in the event object e twice but only the first time there's something in e. Since the second time there's nothing in e, an exception is thrown.

You can remove those two lines again because obviously they are equal and that means you do get inside the if but something crashes inside.

This post has been edited by Gloin: 05 April 2009 - 05:40 AM

Was This Post Helpful? 0
  • +
  • -

#7 Gloin  Icon User is offline

  • Expert Schmexpert...
  • Icon
  • Group: Mentors
  • Posts: 4,130
  • Joined: 04-August 08


Dream Kudos: 75

Posted 05 April 2009 - 05:48 AM

Try to insert System.out.println's to see what values are in all variables in that if-statement coz something is probably not containing what you think.

This post has been edited by Gloin: 05 April 2009 - 05:51 AM

Was This Post Helpful? 0
  • +
  • -

#8 kill99  Icon User is offline

  • D.I.C Head
  • PipPip
  • Group: Members
  • Posts: 53
  • Joined: 03-May 08


Dream Kudos: 0

Posted 05 April 2009 - 06:03 AM

ok will try it out to solve out the problem
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month