Access GUI components from another class

前端 未结 5 2049
遇见更好的自我
遇见更好的自我 2020-12-06 15:34

I\'m new to Java and I\'ve hit a brick wall. I want to access GUI components (that have been created in one class) from another class. I am creating a new GUI class from one

相关标签:
5条回答
  • 2020-12-06 15:42

    First respect encapsulation rules. Make your fields private. Next you want to have getters for the fields you need to access.

    public class GUI {
        private JTextField field = new JTextField();
    
        public GUI() {
            // pass this instance of GUI to other class
            SomeListener listener = new SomeListener(GUI.this);
        }
    
        public JTextField getTextField() {
            return field;
        }
    }
    

    Then you'll want to pass your GUI to whatever class needs to access the text field. Say an ActionListener class. Use constructor injection (or "pass reference") for the passing of the GUI class. When you do this, the GUI being referenced in the SomeListener is the same one, and you don't ever create a new one (which will not reference the same instance you need).

    public class SomeListener implements ActionListener {
        private GUI gui;
        private JTextField field;
    
        public SomeListener(GUI gui) {
            this.gui = gui;
            this.field = gui.getTextField();
        }
    }
    

    Though the above may work, it may be unnecesary. First think about what exactly it is you want to do with the text field. If some some action that can be performed in the GUI class, but you just need to access something in the class to perform it, you could just implement an interface with a method that needs to perform something. Something like this

    public interface Performable {
        public void someMethod();
    }
    
    public class GUI implements Performable {
        private JTextField field = ..
    
        public GUI() {
            SomeListener listener = new SomeListener(GUI.this);
        }
    
        @Override
        public void someMethod() {
             field.setText("Hello");
        }
    }
    
    public class SomeListener implements ActionListener {
        private Performable perf;
    
        public SomeListener(Performable perf) {
            this.perf = perf;
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            perf.someMethod();
        }
    }
    
    0 讨论(0)
  • 2020-12-06 15:46

    Several approaches are possible:

    • The identifier gui is a reference to your GUI instance. You can pass gui to whatever class needs it, as long as you respect the event dispatch thread. Add public accessor methods to GUI as required.

    • Declarations such as JTextArea textAreaClients have package-private accessibility. They can be referenced form other classes in the same package.

    • Arrange for your text areas to receive events from another class using a PropertyChangeListener, as shown here.

    0 讨论(0)
  • 2020-12-06 15:55

    here i add a simple solution hope it works good,

    Form A

    controls

    Textfield : txtusername

        FormB fb = new FormB();
        fb.loginreset(txtusername); //only textfield name no other attributes
    

    Form B
    to access FormA's control

     public void ResetTextbox(JTextField jf)
     {
         jf.setText(null); // or you can set or get any text
     }
    
    0 讨论(0)
  • 2020-12-06 16:08

    The best option to access that text areas is creating a get method for them. Something like this:

    public JTextArea getTextAreaClients(){
        return this.textAreaClients;
    }
    

    And the same for the other one.So to access it from another class:

    GUI gui = new GUI();
    gui.getTextAreaClients();
    

    Anyway you will need a reference for the gui object at any class in which you want to use it, or a reference of an object from the class in which you create it.

    EDIT ---------------------------------------

    To get the text area from GUI to Server you could do something like this inside of Create-Server.

    GUI gui = new GUI();
    Server server = new Server();
    
    server.setTextAreaClients(gui.getTextAreaClients());
    

    For this you should include a JTextArea field inside of Server and the setTextAreaClients method that will look like this:

    JTextArea clients;
    
    public void setTextAreaClients(JTextArea clients){
        this.clients = clients;
    }
    

    So in this way you will have a reference to the JTextArea from gui.

    0 讨论(0)
  • 2020-12-06 16:08

    There is actually no need to use a class that implements ActionListener.

    It works without, what might be easier to implement:

    public class SomeActionListener {
    
    private Gui gui;
    private JButton button1;
    
        public SomeActionListener(Gui gui){
    
            this.gui  = gui;
            this.button1 = gui.getButton();
            this.button1.addActionListener(l -> System.out.println("one"));
        }
    
    }
    

    and then, like others have elaborated before me in this topic:

    public class GUI {
        private JButton button = new JButton();
    
        public GUI() {
            // pass this instance of GUI to other class
            SomeActionListener listener = new SomeActionListener(GUI.this);
        }
    
        public JButton getButton() {
            return button;
        }
    }
    
    0 讨论(0)
提交回复
热议问题