Ramal
Ramal

Reputation: 51

How to call second class when button pressed?

Iam making a simple program where the user enters name and description. If the user presses OK, the program will write the result to the file. Basically, i have 3 classes. I want to call my class2 from class1 and implement the method. I know how to do it in only one class but i would like to know this way too. Thanks in advance.

The problem is that the inputs cannot be added to the file. Maybe iam not calling the file name properly:

   if (result == JOptionPane.OK_OPTION){

class2 ad = new class2(this);
   }

Below are my 3 classes:

Main

public class mainclass {
    public static void main(String[] args) {
        class1 a = new class1();
    }
}

Class1

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

public class class1{

final JTextField field1 = new JTextField(10);
final JTextField field2 = new JTextField(10);
JPanel panel = new JPanel();

public  class1() {

        panel.add(new JLabel("Name:"));
        panel.add(field1);
        panel.setLayout(new GridLayout(5,2));
        panel.add(new JLabel("Description:"));
        panel.add(field2);

        int result = JOptionPane.showConfirmDialog(null, panel,"Enter Information", JOptionPane.OK_CANCEL_OPTION);

        if (result == JOptionPane.OK_OPTION) {
            class2 ad = new class2 ();
        }
    }
}

Class2

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

 public class class2 {

    class1 a;

    public class2(class1 a) {

        this.a = a;

        a = new class1();

        BufferedWriter writer = null;

        try {

            writer = new BufferedWriter( new FileWriter("file.txt", true));

            String add1 = a.field1.getText();
            String add2 = a.field2.getText();

            writer.write(add1);
            writer.write("\t");
            writer.write(add2);
            writer.write("\t");

        } catch ( IOException e) {

        } finally {
            try {
            if ( writer != null)
                writer.close( );
            } catch ( IOException e) {
            }
        }       
    }
}

Upvotes: 2

Views: 1215

Answers (2)

Bernhard Barker
Bernhard Barker

Reputation: 55589

This is a problem.

The constructor of class1 creates an instance of class2 here:

class2 ad = new class2();

Which calls the constructor of class2.

Which creates an instance of class1 here:

a = new class1();

Which calls the constructor of class1.

Which prompts you again.

So you only get to the rest of the constructor of class2 after the second prompt (if you cancel).

Upvotes: 2

trashgod
trashgod

Reputation: 205775

The line a = new class1() in class2 creates a different instance than the one created in mainclass. Instead, pass a reference to class1 to your class2 constructor.

if (result == JOptionPane.OK_OPTION) {
    class2 ad = new class2(this);
}
...
public class2(class1 a) {
    //a = new class1();
    this.a = a;
    ...
}

Upvotes: 2

Related Questions