School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
You're Browsing As A Guest! Register Now...
Become an Expert!

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



ActionListeners

ActionListeners small problem Rate Topic: -----

#1 circuspeanuts  Icon User is offline

  • D.I.C Head
  • PipPip

Reputation: -1
  • View blog
  • Posts: 224
  • Joined: 11-April 08


Dream Kudos: 0

Posted 15 November 2008 - 09:29 AM

ok, so first my code:

person class

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

public class Person extends JFrame{
   
	Scanner scanner = new Scanner(System.in);
	
	private
			int iChoice; 


void SetChoiceConv( ) {
		String Choice = scanner.next();
		iChoice = Integer.parseInt(Choice);		
	}

int GetChoice( int iiChoice ) {
	
	iiChoice = iChoice;
	
	switch(iiChoice) {
		case 1:
			int tempYears = 0;
			Instructor oInstructor;
			oInstructor = new Instructor();
			oInstructor.setInstructorInformation();
			oInstructor.GetInstructorInformation(tempYears);
			break;
		case 2:
			int tempSYears = 0;
			Student oStudent;
			oStudent = new Student();
			oStudent.setStudentInformation();
			oStudent.GetStudentInformation(tempSYears);
			break;
		case 3:
			System.out.print("Goodbye");
			break;
	}
	
 return 0;   
}


	public static void main(String[] args) {

		Layout frame = new Layout();
		frame.setTitle("Information");
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(360, 360);
		frame.setVisible(true);
		
		
   int iiChoice = 0;
	//Person oPerson;
	//oPerson = new Person();		
	
   // JFrame frame = new JFrame("Please select from one of the following:");
	

	/*JButton jbtI = new JButton("Instructor");
	frame.add(jbtI);
	
	frame.setSize(200,200);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setLocationRelativeTo(null);
	frame.setVisible(true);
	
	JButton jbtS = new JButton("Student");
	frame.add(jbtS);
	
	frame.setSize(200,200);
	frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
	frame.setLocationRelativeTo(jbtI);
	frame.setVisible(true);
	
	System.out.print("Please select from one of the following:\n");
	System.out.print("\t 1. Instructor \n\n \t 2. Student \n\n \t 3. Exit");
	oPerson.SetChoiceConv();
	oPerson.GetChoice(iiChoice);	
	*/
	}
	
}


my student class:
public class Student extends Person{

	 private 
			int syears;
void setStudentInformation () {
	
	System.out.print("You have chosen a student\n\n");
	System.out.print("How long have you been a student?\n\n");
	String StudentYears = scanner.next();
	syears = Integer.parseInt(StudentYears);
	System.out.print("So you've been an Student for " + syears + " years...");
	
}

int GetStudentInformation ( int tempSYears ) {
	
	tempSYears = syears;
	return syears;
}
}



my instructor class:
public class Instructor extends Person{

	private 
			int iyears;
void setInstructorInformation () {
	
	System.out.print("You have chosen an instructor\n\n");
	System.out.print("How long have you been an instructor?\n\n");
	String InstructorYears = scanner.next();
	iyears = Integer.parseInt(InstructorYears);
	System.out.print("So you've been an instructor for " + iyears + " years...");
	
}

int GetInstructorInformation ( int tempYears ) {
	
	tempYears = iyears;
	return iyears;
}
 }



my layout class:
import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.*;

public class Layout extends JFrame {
	public Layout () {

		 setLayout( new FlowLayout( FlowLayout.LEFT, 10, 20));

		 add( new JLabel("Please enter the correct information:							"));
		 add( new JLabel("First Name"));
		 add( new JTextField(8));
 
		 add( new JLabel("Last Name"));
		 add( new JTextField(8));
		 
		 add( new JCheckBox("Instructor"));
		 add( new JCheckBox("Student"));
		 JButton jbtOK = new JButton("OK");
		 add (jbtOK);
		 JButton jbtEXIT = new JButton("Exit");
		 add (jbtEXIT);
		 
		 jbtOK.addActionListener( new ActionListener() {
		  public void actionPerformed( ActionEvent e) {
			  System.out.println("You have pressed the OK button");
		  }   
		 });
		 
		 jbtEXIT.addActionListener( new ActionListener() {
		  public void actionPerformed( ActionEvent e) {
			 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		  }   
		 });
	}
}




ok, so my questions are this:

1. can someone look over my code and give me advice on how to make it simpler?

2. I'm having trouble with my action listener. My exit button when I click it it recognizes that I have clicked it, but it does not exit the application

3. my OK button I want to bring up the set and get functions of the instructor and student respectively based on whichever checkbox they may have selected.

keep in mind that this has all been written in netbeans 6.1
Was This Post Helpful? 0
  • +
  • -


#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • Icon

Reputation: 1021
  • View blog
  • Posts: 7,754
  • Joined: 18-April 07


Dream Kudos: 0

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

Re: ActionListeners

Posted 15 November 2008 - 09:55 AM

1. I am not sure why you are extending person from a JFrame and THEN extending Student and Instructor classes from person. This would mean that Student and Instructor are types of JFrames which seems a bit unusual. Typically you would create a person class, extend it to Student and Instructor and then from the Layout Jframe use the three classes. A Student is not a Jframe it is a person object. By correcting that you will have a little be easier time later using the person, student and instructor classes as a single object and not carrying around extra baggage of being a JFrame. Because remember your person class is now carrying around stuff like "setLayout". Why would an instructor be needing setLayout? It is a person for heavens sake!

2. The reason your exit button is not exiting is because you have just told the class to set the Default close operation. This means how to close when it receives commands like clicking the "x" in the top right of the frame. What you need in that function is System.exit(0);

3. Each JCheckbox is a type of "AbstractButton" so it inherits the method isSelected() which will return true if it is clicked or false if it is not. You can use this to check each of the checkboxes are checked and to call the appropriate code. But the way you ask this question sounds like you intend the user to either check or the other and not both... if that is the case, you might consider using radio buttons instead. But anyways, that is how you would do that.

Hope this helps.

"At DIC we be gui fixing and checkbox checkin code ninjas... we also fix sporting events. But shhhhh" :snap:
Was This Post Helpful? 0
  • +
  • -

#3 circuspeanuts  Icon User is offline

  • D.I.C Head
  • PipPip

Reputation: -1
  • View blog
  • Posts: 224
  • Joined: 11-April 08


Dream Kudos: 0

Re: ActionListeners

Posted 15 November 2008 - 10:09 AM

1. I'm not sure I completely understand what you're getting at. am I to understand then that person should not extend JFrame and to have instructor and student extend person and JFrame? or just have them all extends from person which extends from the layout class.

My instructions were to have the three classes with Person being the super class...in order to complete the given operations in my program, dont' I need to have JFrame being extended though?


2. is fixed. Thanks for the help

3. I've decided to use Radio button instead because it makes more sense to have them be able to only select one. Radio buttons have the same params as the check boxes correct?

so my code for that would then be....?

am I to put an if else statement in my OK function saying something along the lines of

if (RBI.isSelected = true) then
insert function methods here
if (RBS.isSelected= true) then
insert function methods here
else
more code
else
more code
Was This Post Helpful? 0
  • +
  • -

#4 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • Icon

Reputation: 101
  • View blog
  • Posts: 2,391
  • Joined: 16-August 08


Dream Kudos: 75

Re: ActionListeners

Posted 15 November 2008 - 10:17 AM

Quote

3. I've decided to use Radio button instead because it makes more sense to have them be able to only select one. Radio buttons have the same params as the check boxes correct?

so my code for that would then be....?

am I to put an if else statement in my OK function saying something along the lines of

if (RBI.isSelected = true) then
insert function methods here
if (RBS.isSelected= true) then
insert function methods here
else
more code
else
more code


Yes, but you also have to add the radio Buttons to a ButtonGroup, in order to allow for only one to be selected at a time.
Was This Post Helpful? 0
  • +
  • -

#5 circuspeanuts  Icon User is offline

  • D.I.C Head
  • PipPip

Reputation: -1
  • View blog
  • Posts: 224
  • Joined: 11-April 08


Dream Kudos: 0

Re: ActionListeners

Posted 15 November 2008 - 10:31 AM

		 ButtonGroup group = new ButtonGroup();
		 JRadioButton jrbS = new JRadioButton("Student");
		 JRadioButton jrbI = new JRadioButton("Instructor");
		 group.add(jrbI);
		 group.add(jrbS);



This is the code that I have for my radio button group now. It's complete. However, as I read through my book I'm not seeing a way to implement my radio button group onto my form unless I make a new form. How do I go about that...
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • while(sad){!sad; awesome();}
  • Icon

Reputation: 782
  • View blog
  • Posts: 13,462
  • Joined: 14-September 07


Dream Kudos: 3000

Expert In: C, C++, Java

Re: ActionListeners

Posted 15 November 2008 - 11:55 AM

The radio buttons are added to the radio group but you still add the radio buttons individually to the form/panel w/e
Was This Post Helpful? 0
  • +
  • -

#7 Martyr2  Icon User is offline

  • Programming Theoretician
  • Icon

Reputation: 1021
  • View blog
  • Posts: 7,754
  • Joined: 18-April 07


Dream Kudos: 0

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

Re: ActionListeners

Posted 15 November 2008 - 12:29 PM

As for the person student instructor thing, yes, person is going to be your base or "super" class. You will extend from it to create your student and instructor. This part is fine, but your person shouldn't be extending JFrame. This is saying that Person is a type of JFrame which it is obviously not. The rest of your inheriting is fine.

KYA is right about adding the buttons to a group (this tells the GUI that the radios are linked) and then you need to add each to the actual frame (because these radios could be on separate sides of the form or you might have special info telling each radio to be at different coordinates).

:)
Was This Post Helpful? 0
  • +
  • -

#8 circuspeanuts  Icon User is offline

  • D.I.C Head
  • PipPip

Reputation: -1
  • View blog
  • Posts: 224
  • Joined: 11-April 08


Dream Kudos: 0

Re: ActionListeners

Posted 15 November 2008 - 01:57 PM

awesome thanks guys. I'll try it out and tell you if I have any other questions :)
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