tony1812
Posts: 350
Joined: Sat Jul 15, 2017 12:40 pm
Location: Boston MA.

How to link java program logic to UI in netbeans

Fri Oct 19, 2018 2:30 pm

Hello. I just started self-taught Java on netbeans. I failed to understand how a program logic is related to the gui. In the code below, I want the user be able to click on a jbutton object, then the code will send a char '1' to the HC-05 serial port. The structure on netbeans is like this attached screen shot:
code for the logic is

Code: Select all

/*
 * 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 test:

import arduino.*;

public class Test {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Arduino  a = new Arduino();
        a.setPortDescription("/dev/cu.HC-05-1-DevB"); // ("/dev/cu.usbmodem1D1330");  
       
        a.openConnection();
        a.serialWrite('1');
        a.closeConnection();
    }
    
}
And code for the UI:

Code: Select all

/*
 * 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.test;

/**
 *
 * @author dick
 */
public class Test_UI extends javax.swing.JFrame {

    /**
     * Creates new form Test_UI
     */
    public Test_UI() {
        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() {

        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("jButton1");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(155, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(150, 150, 150))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(116, 116, 116)
                .addComponent(jButton1)
                .addContainerGap(140, Short.MAX_VALUE))
        );

        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()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

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

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus 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 ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Test_UI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Test_UI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Test_UI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Test_UI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

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

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration                   
}
How do I link them? If someone can show my the code to link them will be very helpful. Thanks.
Attachments
java.png
java.png (15.03 KiB) Viewed 609 times

fbe
Posts: 347
Joined: Thu Aug 17, 2017 9:08 pm

Re: How to link java program logic to UI in netbeans

Sat Oct 20, 2018 7:43 pm

You need an object that implements the java.awt.ActionListener interface. E.g.:

Code: Select all

public class Test_UI extends javax.swing.JFrame
        implements java.awt.event.ActionListener {
        ...

    private void initComponents() {
    ...
        jButton1.setActionCommand("test command");
        jButton1.addActionListener(this);
    } 
        
    /**
     * java.awt.event.ActionListener implementation
     * @param e 
     */
    @Override
    public void actionPerformed(java.awt.event.ActionEvent e) {
        if ("test command".equals(e.getActionCommand())) {
            System.exit(0);
        }
    }
    ...
}
How to Use Buttons, Check Boxes, and Radio Buttons https://docs.oracle.com/javase/tutorial ... utton.html

Return to “Java”