Results 1 to 4 of 4
  1. #1
    robertbob is offline Member
    Join Date
    May 2010
    Posts
    23
    Rep Power
    0

    Default how to get object from another class

    So I am just experimenting here and so far I have a window that when you click a button, it opens up another window (window2). Inside window2 there is a textarea and a JButton. My hope and goal is that when you enter stuff into the textarea and then click the JButton, it displays it onto a label in window1. What I am really having trouble with is using the same label from window1 in window2 so I can use the setText() method to display the text from the textfield. Anyways I probably confused you even more but here is my code.

    WINDOW 1
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Window1 extends JFrame {
    
        JButton button;
        JLabel label;
    
        public Window1() {
            setLayout(new FlowLayout());
    
            button = new JButton("Click me to open another window");
            add(button);
    
            label = new JLabel("");
            add(label);
    
            event e = new event();
            button.addActionListener(e);
        }
    
        public class event implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                Window2 gui = new Window2();
                gui.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
                gui.setVisible(true);
                gui.setSize(200,200);
                gui.setLocation(500, 500);
                gui.setTitle("Another window");
            }
        }
    
        public static void main(String args[]) {
    
            Window1 gui = new Window1();
                gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                gui.setVisible(true);
                gui.setSize(400,400);
                gui.setTitle("Another window");
        }
    }
    WINDOW 2
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class Window2 extends JFrame {
    
        JTextField textfield;
        JButton button;
        Window1 label;
    
        public Window2() {
            setLayout(new FlowLayout());
    
            textfield = new JTextField(10);
            add(textfield);
    
            button = new JButton("Click");
            add(button);
    
            event e = new event();
            button.addActionListener(e);
        }
    
        public class event implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                String text = textfield.getText();
    
                //what to do here???
    
            }
        }
    }
    They are in the same package .

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    10

    Default

    The secret is to treat GUI classes no different from other classes. It matters little if you want to get an int from another class or a String from a JTextField held in another class, often you would use a public getter method, such as

    Java Code:
    public int getMyIntValue() {
      return myIntValue
    }


    For your second window, I'd give it a public getter field that returns a String containing the text held in the text field:
    Java Code:
       // public "getter" method
       public String textfieldGetText() {
          return textfield.getText();
       }


    I'd also make it a JDialog so that it can be called in a modal fashion, and this will mean changing the constructor a little bit, in particular your call to the super constructor. Please see comments:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Window1 extends JFrame {
    
       JButton button;
       JLabel label;
    
       public Window1() {
          setLayout(new FlowLayout());
    
          button = new JButton("Click me to open another window");
          add(button);
    
          label = new JLabel("");
          add(label);
    
          event e = new event();
          button.addActionListener(e);
       }
    
       public class event implements ActionListener {
          public void actionPerformed(ActionEvent e) {
             Window2 gui = new Window2(Window1.this);  // pass reference to this JFrame
             gui.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
             //gui.setVisible(true);
             gui.setSize(200, 200);
             gui.setLocation(500, 500);
             gui.setVisible(true); // set visible *AFTER* setting size, etc
             
             // extract value held in other textfield
             label.setText(gui.textfieldGetText());
          }
       }
    
       public static void main(String args[]) {
    
          Window1 gui = new Window1();
          gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          gui.setVisible(true);
          gui.setSize(400, 400);
          gui.setTitle("Another window");
       }
    }
    
    // make this a JDialog instead
    class Window2 extends JDialog {
    
       JTextField textfield;
       JButton button;
       // Window1 label; // no need for this!
    
       public Window2(JFrame frame) {
          // the true at the end makes this a *modal* dialog
          super(frame, "Another Window", true);
          setLayout(new FlowLayout());
    
          textfield = new JTextField(10);
          add(textfield);
    
          button = new JButton("Click");
          add(button);
    
          event e = new event();
          button.addActionListener(e);
       }
    
       public class event implements ActionListener {
          public void actionPerformed(ActionEvent e) {
             // simply close this window when button pressed
             dispose();
          }
       }
       
       // public "getter" method
       public String textfieldGetText() {
          return textfield.getText();
       }
    }

  3. #3
    robertbob is offline Member
    Join Date
    May 2010
    Posts
    23
    Rep Power
    0

    Default

    ok thanks !

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    10

Similar Threads

  1. Replies: 3
    Last Post: 03-15-2010, 09:44 PM
  2. object vs class
    By billq in forum New To Java
    Replies: 5
    Last Post: 12-24-2009, 05:56 PM
  3. Passing SWT object to another class
    By Vinaya Lal Shrestha in forum SWT / JFace
    Replies: 0
    Last Post: 04-03-2009, 02:50 PM
  4. Getting name of object class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-05-2007, 06:22 PM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 04:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •