user1320453
user1320453

Reputation: 61

Reference Parent UI element in Java

I'm new to Java and have an issue I'm stuck with. I have created a class with a Swing UI in it which contains a jTextArea called 'jTextAreaConsole'.:

public class converterUI extends javax.swing.JFrame {

This class imports another class called 'dbConverter':

package my.converterui;
import dbToExcel.dbConverter;

dbConverter is a simple class with one method:

public class dbConverter extends common{

public static void convert(String sourceDB, String sourceQry, String destination, String objectName){
    dbConverter converter = new dbConverter();
    Connection con = converter.getConnection(sourceDB);
    String sql = sourceQry;
    ResultSet result = converter.runQuery1(con,sql);
    converter.writeOut(result, objectName, destination);
    closeConnection(con);
}
public static void main(String[] args) {
}
}

the runQuery and writeOut methods are detailed in the 'common' class extended by this class. What I want to do is in the common class reference the jTextAreaConsole object to append text to it. I've already tried using super.jTextAreaConsole.append(str) and this runs but doesn't do anything.

Edit: Basic example here:

package myproject;

public class MyProject extends mainForm{

    public static void main(String[] args) {
        mainForm.main(null);
    }

    public void clickAction(){
        passText();
    }
}


package myproject;
class mainForm extends javax.swing.JFrame {

    public void passText(){
        jTextArea1.append("This is a test");
    }

    public mainForm() {
        initComponents();
    }



    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jButton1.setText("Click me");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 318, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(42, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(162, 162, 162))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addComponent(jButton1)
                .addGap(27, 27, 27)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(25, Short.MAX_VALUE))
        );

        pack();
    }                       

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

    }                                        

      public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new mainForm().setVisible(true);
            }
        });
    }


    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;

}

Thanks,

Upvotes: 1

Views: 231

Answers (1)

user1803551
user1803551

Reputation: 13427

Depending on your design, you have 2 choices:

  1. Create an instance of the MyProject, let's call it mp. Then in your actionPerformed (or your jButton1ActionPerformed, still not sure why you need it) call mp.clickAction().
  2. Make clickAction static, then call MyProject.clickAction().

The fact that MyProject extends mainForm (which should be renamed to MainForm) already tells me that you're probably doing something wrong in your design.

Edit:

Here, I wrote the code for you. Just don't be surprised if you later need to expand on it and it's not easy to do because the design is bad.

import java.awt.EventQueue;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;

import static javax.swing.GroupLayout.*;
import static javax.swing.GroupLayout.Alignment.*;

public class MyProject {

    private static MainForm gui = new MainForm();

    public static void main(String[] args) {

        EventQueue.invokeLater(() -> gui.setVisible(true));
    }

    public static void clickAction() {

        gui.passText();
    }
}

class MainForm extends JFrame {

    private JTextArea jTextArea1 = new JTextArea();

    public MainForm() {

        initComponents();
    }

    private void initComponents() {

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JScrollPane jScrollPane1 = new JScrollPane();
        JButton jButton1 = new JButton();

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jButton1.setText("Click me");
        jButton1.addActionListener(e -> MyProject.clickAction());

        //@formatter:off
        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addComponent(jScrollPane1, PREFERRED_SIZE, 318, PREFERRED_SIZE)
                .addContainerGap(42, Short.MAX_VALUE))
            .addGroup(TRAILING, layout.createSequentialGroup()
                .addContainerGap(DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(162, 162, 162))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addComponent(jButton1)
                .addGap(27, 27, 27)
                .addComponent(jScrollPane1, PREFERRED_SIZE, 179, PREFERRED_SIZE)
                .addContainerGap(25, Short.MAX_VALUE))
        );
        //@formatter:on

        pack();
    }

    public void passText() {

        jTextArea1.append("This is a test");
    }
}

What I did:

  • MyProjext is not a subclass of MainForm anymore because it doesn't make sense for it to be.
  • The program has just 1 main now because it should have only 1 main. I chose MyProjext to be the entry point.
  • MyProjext holds a reference to the GUI (MainForm) so it can invoke its methods. In my point 1 I suggested the other way around: have the GUI hold a reference to MyProjext. It still can be a better choice depending on the design.
  • MyProjext's clickAction is now static as I suggested in my point 2. Again, it's a blind design choice.
  • I made the code more readable because the GUI-builder code is not as friendly. I suggest that after you finish learning the basics of OO (not only relevant to Java) you learn how to write a GUI and not use a builder.

Upvotes: 1

Related Questions