0

i want to read a data from a textfield save it on an array in the first class and in the second class i wanted to display this data in a textfield,but my program returns nothing

here the first class

import java.util.ArrayList;
public class Patient extends javax.swing.JFrame {
int emg;
String clas;
String day;
int sex;
String name;
String cls;
int count;
boolean[] Assignbed = new boolean[31];
ArrayList<Integer> list1;
ArrayList list2=new ArrayList();
ArrayList list3;
    /**
     *
     */
    public Patient() {
        this.list3 = new ArrayList<Integer>();
        this.list1 = new ArrayList<Integer>();
        initComponents();
    }
    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    AWellcome a=new AWellcome();
    a.setVisible(true);
    dispose();
    }                                          

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Name();
    }                                                                               

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Level();
    }                                                                                                                                                                   

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    occupation();
    }                                        

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Sex();
    }                                                                                                                      

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Age();
    }                                        

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Payment();
    }                                        

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Reserve();
    }                                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Date();
    }                                                                                                                                                                

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Department();
    }                                        
public ArrayList<Integer> getList() {
       return list1;
   }
public void Department(){
if(jComboBox3.getSelectedIndex()==0){
emg=Integer.parseInt(jTextField20.getText());
list1.add(emg);
}
else if(jComboBox3.getSelectedIndex()==1){
list2.add(jTextField20.getText());
}
else if(jComboBox3.getSelectedIndex()==2){
list3.add(jTextField20.getText());
}
}
public void Date(){
day=jTextField23.getText();
jTextField7.setText(day);
}
public void occupation(){
cls=jTextField44.getText();
jTextField15.setText(cls);
}
public void count(){
jTextField20.setText(Integer.toString(count));
}
public void fulllevel1(){
jTextField25.setText("level1_Full");
}
public void fulllevel2(){
jTextField25.setText("level2_Full");
}
public void fulllevel3(){
jTextField25.setText("Level3_Full");
}
public void Level(){
if(jComboBox2.getSelectedIndex()==0){
jTextField9.setText("Level 1");
}
else if(jComboBox2.getSelectedIndex()==1){
jTextField9.setText("Level 2");
}
else if(jComboBox2.getSelectedIndex()==2){
jTextField9.setText("Level 3");
}
}
public void Age(){
clas=jTextField43.getText();
jTextField13.setText(clas);
}
public void Sex(){
if(jComboBox1.getSelectedIndex()==0){
jTextField17.setText("Male");
}else if(jComboBox1.getSelectedIndex()==1)
{
jTextField17.setText("Female");
}
}
public void Name(){
name=jTextField42.getText();
jTextField11.setText(name);   
}
public void Payment(){
if(jComboBox2.getSelectedIndex()==0){
jTextField19.setText("50 Birr/Day");
}
else if(jComboBox2.getSelectedIndex()==1){
jTextField19.setText("30 Birr/Day");
}
else if(jComboBox2.getSelectedIndex()==2){
jTextField19.setText("20 Birr/Day");
}
}
public void Reserve()
    {
        if ( jComboBox2.getSelectedIndex() == 0 )
        {
            firstlevel();
        }
        else if(jComboBox2.getSelectedIndex()== 1)
        {
            secondlevel();
        }
         else if(jComboBox2.getSelectedIndex()== 2)
        {
            thirdlevel();
        }
    }

 public void firstlevel() // 
    {
        for ( count = 1; count <= 10; count++ )
        {
            if ( Assignbed[count] == false )  // if false, then a bed available for assignment
            {
                Assignbed[count] = true;  // assign bed
                count();
                break;
            }
            else if ( Assignbed[10] == true ) // If assignedbed[10] is true then first level is full
            {
                fulllevel1();

            }
        }
    }   

 public void secondlevel() //
    {
        for ( count = 11; count <= 20; count++ )
        {
            if ( Assignbed[count] == false ) //
            {
                Assignbed[count] = true; //
               count();
                break;
            }
            else if ( Assignbed[20] == true ) // 
            {
                fulllevel2();

            }
        }
    }
  public void thirdlevel() // 
    {
        for ( count = 21; count <= 30; count++ )
        {
            if ( Assignbed[count] == false ) 
            {
               Assignbed[count] = true;
               count();
                break;
            }
            else if ( Assignbed[30] == true ) 
            {
                fulllevel3();

            }
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Patient().setVisible(true);
            }
        });
    }


} 

Second class

import java.util.ArrayList;
public class Staff extends javax.swing.JFrame {
private Patient p;
    public Staff() {
        initComponents();
    }                      

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Department1();
    }                                        
    public ArrayList<Integer> Department1(){
    p=new Patient();
    ArrayList<Integer> list1 = p.getList();  
        return list1;
    } 
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Staff().setVisible(true);
            }
        });
    }

}
3
  • about which list you are talking about Commented Jan 20, 2015 at 18:24
  • list1 in the patient class Commented Jan 20, 2015 at 18:27
  • Please re-format your posted code by giving it proper indentations, usually 4 spaces per block, and making sure that all code on the same block is on the same indentation level. Your cooperation in this would be greatly appreciated and will likely improve your chances of getting a decent and prompt answer. Commented Jan 20, 2015 at 18:31

2 Answers 2

2

Your problem is here:

p=new Patient();  // ***** here *****
ArrayList<Integer> list1 = p.getList(); 

You're creating a new Patient object and expecting it to have the same state as the original Patient object. Don't do this -- instead pass a reference of the original Patient, the one that holds the data of interest, into the new class:

import java.util.ArrayList;

public class Staff extends javax.swing.JFrame {
    private Patient p;

    public Staff(Patient p) {
        this.p = p;
        initComponents();
    }                      

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        Department1();
    }                                        

    public ArrayList<Integer> Department1(){
        // p=new Patient();
        ArrayList<Integer> list1 = p.getList();  
        return list1;
    } 

Whatever you do, ignore anyone telling you to make the list static, please, as by doing this, you would be ignoring all object-oriented programming principles.

Sign up to request clarification or add additional context in comments.

7 Comments

public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Staff().setVisible(true);//shows error in this line } }); }
@siraktadesse: and if you read the error message, and see how I changed the constructor, it will make sense, no? You've got to pass the displayed Patient class into the Staff class's constructor.
so do i have to change it too?
public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Patient().setVisible(true); } }); }
@siraktadesse: Please don't post code in comments since it loses its formatting making it unreadable. Instead, post any new code to the bottom of your original question by editing your question. Also, "shows error" tells us diddly squat. If you need help with an error, show us the complete error. Also, you're not passing in the visualized Patient object into the Staff constructor.
|
0

you can either declare it as static and access it using the simple class name dot variable name, or create get function like:

public List getList(){
    return <yourList>'
}

1 Comment

static is never a solution for this, as it restricts the possibility of having multiple instances of the parent class and is better never suggested or mentioned at all. The use of a accessor is a far better solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.