Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 306,817 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,691 people online right now. Registration is fast and FREE... Join Now!




ActionListeners

 

ActionListeners, small problem

circuspeanuts

15 Nov, 2008 - 09:29 AM
Post #1

D.I.C Head
**

Joined: 11 Apr, 2008
Posts: 173

ok, so first my code:

person class

CODE

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

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

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

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


User is offlineProfile CardPM
+Quote Post


Martyr2

RE: ActionListeners

15 Nov, 2008 - 09:55 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 7,305



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

My Contributions
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" decap.gif
User is online!Profile CardPM
+Quote Post

circuspeanuts

RE: ActionListeners

15 Nov, 2008 - 10:09 AM
Post #3

D.I.C Head
**

Joined: 11 Apr, 2008
Posts: 173

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


User is offlineProfile CardPM
+Quote Post

BigAnt

RE: ActionListeners

15 Nov, 2008 - 10:17 AM
Post #4

May Your Swords Stay Sharp
Group Icon

Joined: 16 Aug, 2008
Posts: 2,391



Thanked: 100 times
Dream Kudos: 75
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

circuspeanuts

RE: ActionListeners

15 Nov, 2008 - 10:31 AM
Post #5

D.I.C Head
**

Joined: 11 Apr, 2008
Posts: 173

CODE

         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...
User is offlineProfile CardPM
+Quote Post

KYA

RE: ActionListeners

15 Nov, 2008 - 11:55 AM
Post #6

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,489



Thanked: 507 times
Dream Kudos: 2875
Expert In: C, C++, Java

My Contributions
The radio buttons are added to the radio group but you still add the radio buttons individually to the form/panel w/e
User is online!Profile CardPM
+Quote Post

Martyr2

RE: ActionListeners

15 Nov, 2008 - 12:29 PM
Post #7

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 7,305



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

My Contributions
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).

smile.gif
User is online!Profile CardPM
+Quote Post

circuspeanuts

RE: ActionListeners

15 Nov, 2008 - 01:57 PM
Post #8

D.I.C Head
**

Joined: 11 Apr, 2008
Posts: 173

awesome thanks guys. I'll try it out and tell you if I have any other questions smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/20/09 10:11PM

Live Java Help!

Be Social

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

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month