contoh memanggil text dari JComboBox

package javaapplication16;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Main extends JFrame{
    String [] ahay ={"laki-laki", "perempuan"};
    MyActionListener goblok = new MyActionListener();{
    }
        ActionListener aa = new ActionListener() {
        private Component panel;
        public void actionPerformed(ActionEvent actionEvent) {
        System.out.println("coba");
      }
    };
    ActionListener actionListener = new ActionListener() {
        private Component panel;
        public void actionPerformed(ActionEvent actionEvent) {
    JDialog.setDefaultLookAndFeelDecorated(true);
    int response = JOptionPane.showConfirmDialog(null, "apakah anda yakin ingin keluar ?", "OOppzz",
        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
    if (response == JOptionPane.NO_OPTION) {
      System.out.println("No button clicked");
    } else if (response == JOptionPane.YES_OPTION) {
        System.exit(0);
    } else if (response == JOptionPane.CLOSED_OPTION) {
      
    }
      }
    };
    private Label lblNama = new Label("Nama Anda:");
    private Label lblKA = new Label("Kota Anda:");
    private Label lblEmail = new Label("Email:");
    private Label lblPs = new Label("password:");
    private Label lblJK = new Label("Saya Seorang:");
    private JButton btnOK = new JButton("OK");
    private JButton btnCancel = new JButton("Quit");

    private TextField txtNama = new TextField();
    private TextField txtKA = new TextField();
    private TextField txtEmail = new TextField();
    private JPasswordField txtPs = new JPasswordField();
    private JComboBox kl = new JComboBox(ahay);

    public Main(){
        super("i-Team Of Fatihul Ulum");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocation(100,100);
        setSize(400,200);

        lblNama.setAlignment(Label.RIGHT);
        lblKA.setAlignment(Label.RIGHT);
        lblEmail.setAlignment(Label.RIGHT);
        lblPs.setAlignment(Label.RIGHT);
        lblJK.setAlignment(Label.RIGHT);

        JPanel pan = new JPanel();
        pan.setLayout(new GridLayout(6,3));
        pan.add(this.lblNama);
        pan.add(this.txtNama);
        pan.add(this.lblKA);
        pan.add(this.txtKA);
        pan.add(this.lblEmail);
        pan.add(this.txtEmail);
        pan.add(this.lblPs);
        pan.add(this.txtPs);
        pan.add(this.lblJK);
        pan.add(kl);
        kl.addActionListener(goblok);
        pan.add(btnOK);
        this.btnOK.addActionListener(aa);
        pan.add(btnCancel);
        this.btnCancel.addActionListener(actionListener);
        pan.setBorder(BorderFactory.createEmptyBorder(10,25,15, 5));
        this.add(pan);
       setVisible(true);
    }
    public static void main(String [] args){
        new Main();
    }
}
class MyActionListener implements ActionListener {
  Object oldItem;
    private Component panel;
  public void actionPerformed(ActionEvent evt) {
         JComboBox cb = (JComboBox) evt.getSource();
        String ffff = cb.getSelectedItem().toString();
      JOptionPane.showMessageDialog(panel, ffff,
     "Error", JOptionPane.ERROR_MESSAGE);
  }
}

Diberdayakan oleh Blogger.
Twitter Delicious Facebook Digg Stumbleupon Favorites More