Coretanku

Tempatku Berbagi…

Interaksi antar JFrame

oke-oke… kali ini daku pengen share tentang gimana toh caranya melakukan hubungan atau interaksi antar JFrame??,,

untuk melakukan hubungan antar JFrame dapat menggunakan method getter, method ini digunakan untuk mengambil objek yang akan digunakan,, misalnya kaya’ gini ni,, ada dua buah JFrame, JFrame yang kedua ada sebuah JTextField, jadi untuk mengakses JTextField pada JFrame kedua dapat menggunakan method getter tersebut.

public JTextField getjTextFieldFormDua() {
return jTextFieldFormDua;
}

Dari method itu dapat dipanggil dari JFrame pertama, untuk memanggil method itu JFrame kedua diberi modifier final, jadi seperti ini

private final FormKedua formKedua = new FormKedua();

kemudian baru dipanggil, jika ingin memberi input pada JTextField di JFrame kedua dapat dimenggunakan method setText yang dipunyai oleh objek tersebut,sintaknya seperti ini ni

formKedua.getjTextFieldFormDua().setText(“Hello, Feri”);

kira-kira seperti itu,, untuk lebih jelasnya dapat melalui contoh program seperti ini:

Buat File FormPertama.java, isi seperti pada kode dibawah:

/*
 * FormPertama.java
 *
 * Created on May 8, 2010, 7:55:21 PM
 */

package feri.iskandar.relasiframe;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;

/**
 *
 * @author pheerry
 */
public class FormPertama extends javax.swing.JFrame {

    private final FormKedua formKedua=new FormKedua();
    /** Creates new form FormPertama */
    public FormPertama() {
        initComponents();
        btnSend.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                formKedua.getTextResponKedua().setText(textHello.getText());
                formKedua.setVisible(true);
            }
        });

        formKedua.getBtnSend().addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                textResponSatu.setText(formKedua.getTextHelloDua().getText());
                setFocusable(true);
            }
        });
        setTitle("FORM PERTAMA");
    }

    public JTextField getTextResponSatu() {
        return textResponSatu;
    }

    /** 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() {

        btnSend = new javax.swing.JButton();
        textHello = new javax.swing.JTextField();
        textResponSatu = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        btnSend.setText("Send");

        jLabel1.setText("Respon Form kedua");

        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(btnSend, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textHello, javax.swing.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE)
                    .addComponent(textResponSatu, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE)
                    .addComponent(jLabel1))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(textHello, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnSend)
                .addGap(18, 18, 18)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                .addComponent(textResponSatu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

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

    // Variables declaration - do not modify
    private javax.swing.JButton btnSend;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField textHello;
    private javax.swing.JTextField textResponSatu;
    // End of variables declaration

}

oke kemudian buat file Jframe kedua “FormKedua.java”

/*
 * FormKedua.java
 *
 * Created on May 8, 2010, 7:55:38 PM
 */

package feri.iskandar.relasiframe;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JTextField;

/**
 *
 * @author pheerry
 */
public class FormKedua extends javax.swing.JFrame {

    /** Creates new form FormKedua */
    public FormKedua() {
        initComponents();
        setTitle("FORM KEDUA");
    }

    public JTextField getTextResponKedua() {
        return textResponKedua;
    }

    public JButton getBtnSend() {
        return btnSend;
    }

    public JTextField getTextHelloDua() {
        return textHelloDua;
    }

    /** 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() {

        textResponKedua = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        textHelloDua = new javax.swing.JTextField();
        btnSend = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Respon Form pertama");

        btnSend.setText("Send");

        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(textResponKedua, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 340, Short.MAX_VALUE)
                    .addComponent(textHelloDua, javax.swing.GroupLayout.DEFAULT_SIZE, 340, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addComponent(btnSend, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(textHelloDua, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSend)
                .addGap(22, 22, 22)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textResponKedua, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

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

    // Variables declaration - do not modify
    private javax.swing.JButton btnSend;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField textHelloDua;
    private javax.swing.JTextField textResponKedua;
    // End of variables declaration

}

Oke itu udah, kemudian buat main programnya, “Main.java”


package feri.iskandar.relasiframe;

import javax.swing.SwingUtilities;

/**
 *
 * @author pheerry
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                new FormPertama().setVisible(true);
            }
        });
    }

}

Udah itu, kemudian jalankan file Main.java maka hasilnya akan seperti program berikut ini ni..

Nah, kira-kira seperti itu tuh,, ok sekian dulu ea, semoga bermanfaat. kalo mau download di di url ini ni:

Download Project

Advertisements

2 responses to “Interaksi antar JFrame

  1. ckakakak... May 8, 2010 at 4:05 pm

    nek tak coba di projectq koq gak dadi yo???

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: