Thursday 24 January 2013
0 comments

MENGAMBIL STRING DARI TEXT FIELD DAN COMBO BOX DI JAVA


Tutorial kali ini adalah tentang pengambilan sebuah String kemudian menaruhnya ke dalam sebuah Label dan sebagainya....
Ini agar kita dapat memahami tentang pengambilan string baik dari text field, combo box, ataupun dari radio group. berikut scrin shotnya:








Untuk mencobanya silahkan anda copas source code dibawah ini:


package cobalage;

import javax.swing.JOptionPane;

/**
 *
 * @author INDRA SASAK
 */
public class Latihanku extends javax.swing.JFrame {

    /** Creates new form Latihanku */
    public Latihanku() {
        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")
    // 
    private void initComponents() {

        teks1 = new javax.swing.JLabel();
        tf2 = new javax.swing.JTextField();
        tf1 = new javax.swing.JTextField();
        tf3 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        combo = new javax.swing.JComboBox();
        pilihan1 = new javax.swing.JRadioButton();
        pilihan2 = new javax.swing.JRadioButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Aplikasi Latihan Indra Al Sasak");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });
        getContentPane().setLayout(null);

        teks1.setText("Text dari label yang sudah diganti");
        getContentPane().add(teks1);
        teks1.setBounds(10, 21, 162, 14);

        tf2.setText("jTextField1");
        getContentPane().add(tf2);
        tf2.setBounds(200, 18, 59, 20);

        tf1.setText("jTextField2");
        getContentPane().add(tf1);
        tf1.setBounds(10, 46, 172, 20);

        tf3.setText("jTextField3");
        getContentPane().add(tf3);
        tf3.setBounds(200, 44, 59, 20);

        jButton1.setText("Ganti");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(269, 17, 70, 23);

        jButton2.setText("Ganti");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2);
        jButton2.setBounds(269, 43, 70, 23);

        combo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Coba Saja 1", "Coba Saja 2", "Coba Saja 3", "Coba Saja 4" }));
        getContentPane().add(combo);
        combo.setBounds(10, 72, 172, 22);

        pilihan1.setText("Laki-laki");
        pilihan1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pilihan1ActionPerformed(evt);
            }
        });
        getContentPane().add(pilihan1);
        pilihan1.setBounds(10, 102, 90, 23);

        pilihan2.setText("Perempuan");
        pilihan2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pilihan2ActionPerformed(evt);
            }
        });
        getContentPane().add(pilihan2);
        pilihan2.setBounds(100, 100, 100, 23);

        jButton3.setText("Ambil 1");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton3);
        jButton3.setBounds(269, 72, 80, 23);

        jButton4.setText("Ambil 2");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton4);
        jButton4.setBounds(269, 98, 80, 23);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-373)/2, (screenSize.height-172)/2, 373, 172);
    }// 

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        teks1.setText(tf2.getText());
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        tf1.setText(tf3.getText());
    }

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        JOptionPane.showMessageDialog(null, combo.getSelectedItem());
    }

    private void pilihan1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        pilihan1.setSelected(true);
        pilihan2.setSelected(false);
    }

    private void pilihan2ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        pilihan1.setSelected(false);
        pilihan2.setSelected(true);
    }

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        if(pilihan1.isSelected()) JOptionPane.showMessageDialog(null, "Pilihan 1 Laki-laki dipilih");
        if(pilihan2.isSelected()) JOptionPane.showMessageDialog(null, "Pilihan 2 Perempuan dipilih");
    }

    private void formWindowActivated(java.awt.event.WindowEvent evt) {
        // TODO add your handling code here:
        tf1.setText("");
        tf2.setText("");
        tf3.setText("");
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Latihanku().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JComboBox combo;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JRadioButton pilihan1;
    private javax.swing.JRadioButton pilihan2;
    private javax.swing.JLabel teks1;
    private javax.swing.JTextField tf1;
    private javax.swing.JTextField tf2;
    private javax.swing.JTextField tf3;
    // End of variables declaration

}


Semoga bermanfaat...
Jangan lupa kasih komentarnya yach....


0 comments:

Post a Comment

 
Toggle Footer
Top