Java send data to another class

There are not that much conventions.

RIP SirClive Sinclair, I possibly might never be typing here were it not for his vision and products.
:star2: :star2: :star2:

To answer my own question and, heaven forbid, someone should read this in the future expecting a resolution.

my test project aim was to set the text in a textarea from another class in order to simulate the process I wish to use in basically every app I will likely write.

this is two very simple classes that do exactly that:-

MAIN CLASS (mostly Netbeans generated code that is folded in the IDE)

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package my.TestGUI;

/**
 *
 * @author doj
 */
public class TestGUI extends javax.swing.JFrame
{

    /** Creates new form TestGUI */
    public TestGUI()//CONSTRUCTOR
    {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents()
    {

        jScrollPane1 = new javax.swing.JScrollPane();
        TA_Debug = new javax.swing.JTextArea();
        Btn_Press = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        TA_Debug.setColumns(20);
        TA_Debug.setRows(5);
        TA_Debug.setName(""); // NOI18N
        jScrollPane1.setViewportView(TA_Debug);

        Btn_Press.setText("Press");
        Btn_Press.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                Btn_PressActionPerformed(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()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 687, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(Btn_Press)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 331, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Btn_Press)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        

    private void Btn_PressActionPerformed(java.awt.event.ActionEvent evt)                                          
    {                                              
        //this calls the TEXAREA handling class 'TA' where the 'debug' TEXTAREA
        //is directly written from that class.
        //this is possible because 'TA_debug' is declared PUBLIC STATIC in this
        //class allowing it to be referenced externally.
        TA.debug_setText("test123");//start a new screen message
        TA.debug_add_cr(1);//add CARRIAGE RETURN
        TA.debug_append("5678");//append some text to the line after CR
        
    }                                         

    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {
        /* Set the look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try
        {
            for(javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
            {
                if("System".equals(info.getName()))//use "Nimbus" for the default Java, "System" for more Mac like appearance
                {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        }
        catch(ClassNotFoundException ex)
        {
            java.util.logging.Logger.getLogger(TestGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        catch(InstantiationException ex)
        {
            java.util.logging.Logger.getLogger(TestGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        catch(IllegalAccessException ex)
        {
            java.util.logging.Logger.getLogger(TestGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        catch(javax.swing.UnsupportedLookAndFeelException ex)
        {
            java.util.logging.Logger.getLogger(TestGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new TestGUI().setVisible(true);
            }
        });
        
        //public 
        
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton Btn_Press;
    public static javax.swing.JTextArea TA_Debug;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration                   

}

the SECOND CLASS called ‘TA’ short for TEXTAREA:-

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package my.TestGUI;

/**
 *
 * @author doj
 */
//
//
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//HANDLE CODE RELATED TO ANY TEXTAREA IN THE GUI.
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
public class TA
{
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    //TA_debug.
    //handle any external calls to this TEXTAREA.
    //
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    //SET TEXT.
    public static void debug_setText(String s)                                          
    {                                              
        TestGUI.TA_Debug.setText(s);
    } 
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    //
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    //APPEND TEXT.
    public static void debug_append(String s)                                          
    {                                              
        TestGUI.TA_Debug.append(s);
    } 
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    //
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    //ADD CARRIAGE RETURN.
    //this will add CARRIAGE RETURN to the TEXTAREA.
    //'cr_num' will add a NUMBER of CR to the TEXTAREA
    public static void debug_add_cr(int cr_num)                                          
    {                                              
        for(int i = 0; i < cr_num; i++)
        {
            TestGUI.TA_Debug.append("\n");
        }
    } 
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    //
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    
}

this does what I had hopes so I am very pleased!

but I did notice something, the stupid Java people use ‘append’ to add text to the textarea, surely thats really confusing and it should be renamed ‘addSomeMoreTextToTheExistingTextThatsInThisTexarea’, or is that a little verbose?

thanks to all who contributed.

1 Like

whipped up a sample that, I think, addresses this

Note java likes to use interfaces a lot and they are handy as heck so thats the approach I took (there ARE most definitely different ways to tackle this depending on what you want to do)

the sample is SIMPLE

EDIT : sample updated to remove use of deprected API :stuck_out_tongue:

Thanks Norm, great update and thank you for the work, looking forward to the other update you are working on and I will add a post about how it adds to my learning.

Reading the Java docs it is appending the existing text and so why write something else?

Hi Thorsten,

I have not explained my problem well, so it is misunderstood here, by me.

I now have some more information to help my understanding and that means the post here is not actually much use because the code might work but its not at all useful in a real project I have now found.