1

Lets say I have:

class BlaBla{
int x;
int y; 

public void actionPerformed(ActionEvent e){

int m1;
int m2;
// algorhythm where i get values for m1 and m2
}
}

How can i assign values of m1 to x and m2 to y?

Because i want to use values of m1 and m2 in another class? in another class where i Have paint method and i would like to use them as coordinates for painting.

Thanks

my action button will parse XML file. than i need four variables for paint method, which is in another class

public void actionPerformed(ActionEvent e){
        try{


        JFileChooser fch = new JFileChooser();
        int i = fch.showOpenDialog(null);
        if(i==JFileChooser.APPROVE_OPTION){
            File f = fch.getSelectedFile();

            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(f);
            doc.getDocumentElement().normalize();
            NodeList nList = doc.getElementsByTagName("line");

            // NodeList nList = doc.getElementsByTagName("point");

            for (int temp = 0; temp < nList.getLength(); temp++) {

                Node nNode = nList.item(temp);

                if (nNode.getNodeType() == Node.ELEMENT_NODE) {




                    Element eElement = (Element) nNode;


                    Element line = (Element) nList.item(0);

                    Element point1 = (Element) line.getElementsByTagName("point").item(0);
                    Element point2 = (Element) line.getElementsByTagName("point").item(1);

                     int m1 = Integer.parseInt(point1.getElementsByTagName("X").item(0).getTextContent());
                     int n1 = Integer.parseInt(point1.getElementsByTagName("Y").item(0).getTextContent());
                     int m2 = Integer.parseInt(point2.getElementsByTagName("X").item(0).getTextContent());
                     int n2 = Integer.parseInt(point2.getElementsByTagName("Y").item(0).getTextContent());

my main class is:

public class Line {
public static void main(String[] args){
        JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setTitle("Test");
    frame.setLayout(new FlowLayout());
final JPanel pnl = new JPanel(){
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            Vyber v = new Vyber();
            g.drawLine(x1, y1, x2, y2); //values from method from another class
            }
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
DRastislav
  • 1,892
  • 3
  • 26
  • 40

2 Answers2

2

In order to get information from one class to another, I would avoid using local variables and instead set class fields, like so:

public class SomeClass {
  Point p1;
  Point p2;

  public void actionPerformed(ActionEvent e) {

    // some code....

    int m1 = Integer.parseInt(point1.getElementsByTagName("X").item(0).getTextContent());
    int n1 = Integer.parseInt(point1.getElementsByTagName("Y").item(0).getTextContent());
    int m2 = Integer.parseInt(point2.getElementsByTagName("X").item(0).getTextContent());
    int n2 = Integer.parseInt(point2.getElementsByTagName("Y").item(0).getTextContent());

    p1 = new Point(m1, n1);
    p2 = new Point(m2, n2);
  }

  public Point getP1() {
    return p1;
  }

  public Point getP2() {
    return p2;
  }

}

Then any class that has a valid reference to the correct instance of SomeClass can simply call the getter method when it needs to obtain a current copy of p1 and p2. Having a valid reference is key since you can't re-create another SomeClass instance and expect that its fields will hold the values of another SomeClass instance that is being used by your program.

The trickier part comes if you want to get the information i an event-driven way, if you want the p1 and p2 Points, after the actionPerformed has occurred. If this is so, I'd use a PropertyChangeSupport instance to allow listeners to be notified if these fields are changed. For example, please look at this answer to see how this could possibly be done.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

You need an instance of your class, I will use the Point class which already exists to illustrate.

Lets say I have a JTextField and I want to add an ActionListener to it, this will work:

private void addListener(final JTextField textField, final Point point) {
    textField.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            final double m1 = point.getX();
            final double m2 = point.getY();
        }
    });
}

The issue you will have is that in this example the Point must be final as you cannot refer to non-final method parameters in anonymous inner classes.

You could also declare your class as a class variable, something like this

//somewhere in your class
private Point point;

//somewhere else in your class
private void addListener(final JTextField textField) {
    textField.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            final double m1 = point.getX();
            final double m2 = point.getY();
        }
    });
}
Boris the Spider
  • 59,842
  • 6
  • 106
  • 166