📄 BaglantiliCombo.java .java dosyası
⬇️ İndir
import java.awt.Font;
import java.awt.event.*;
import javax.swing.*;
public class BaglantiliCombo {
public BaglantiliCombo() {
JFrame frame = new JFrame("Baglantili JComboBox Ornegi String[] ile Java Blackboard 060");
frame.setLayout(null);
String[] diziSehir = { "Şehir Seçiniz", "İstanbul", "Ankara", "Antalya", "Muğla"};
String[] istanbulIlceler = { "İlçe seçiniz.", "Ümraniye", "Kadıköy", "Üsküdar", "Eminönü", "Beşiktaş"};
String[] ankaraIlceler = { "İlçe seçiniz.", "Gölbaşi", "Altındağ", "Beypazarı", "Keçiören", "Mamak" };
Object[] antalyaIlceler = { "İlçe seçiniz.", "Muratpaşa", "Kepez", "Kemer", "Konyaaltı", "Manavgat" };
String[] muglaIlceler = { "İlçe seçiniz.", "Bodrum", "Datça", "Dalaman", "Fethiye", "Milas", "Marmaris" };
JLabel labelBaslik = new JLabel("Bağlantılı ComboBox ÖRNEK");
labelBaslik.setBounds(100, 50, 200, 30);
JLabel labelSehir = new JLabel("Şehir : ");
labelSehir.setBounds(100, 100, 50, 30);
JComboBox comboSehir = new JComboBox(diziSehir);
comboSehir.setBounds(150, 100, 150,30);
JLabel labelIlce = new JLabel("İlçe : ");
labelIlce.setBounds(100, 150, 50, 30);
JComboBox comboIlce = new JComboBox();
comboIlce.setBounds(150, 150, 150,30);
JButton butonGoster = new JButton("Göster");
butonGoster.setBounds(100, 200, 200, 30);
JTextArea areaSecilenler = new JTextArea();
areaSecilenler.setBounds(100, 250, 200, 200);
JScrollPane areaPane = new JScrollPane(areaSecilenler);
areaPane.setBounds(100, 250, 200, 200);
comboSehir.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (comboSehir.getSelectedItem().equals("İstanbul")) {
comboIlce.setFont(new Font("Ubuntu", Font.BOLD, 17));
comboIlce.setModel(new DefaultComboBoxModel(istanbulIlceler));
} else if ( comboSehir.getSelectedItem().equals("Ankara")) {
comboIlce.setFont(new Font("Ubuntu", Font.ITALIC, 17));
comboIlce.setModel(new DefaultComboBoxModel<String>(ankaraIlceler));
} else if ( comboSehir.getSelectedItem().equals("Antalya")) {
comboIlce.setFont(new Font("Ubuntu", Font.PLAIN, 17));
comboIlce.setModel(new DefaultComboBoxModel<Object>(antalyaIlceler)); // <String> yazarsak hata aliriz!!!
} else if ( comboSehir.getSelectedItem().equals("Muğla")) {
comboIlce.setFont(new Font("Helvetica", Font.PLAIN, 17));
comboIlce.setModel(new DefaultComboBoxModel<Object>(muglaIlceler)); // Object Stringín abisi oldugu için burada sorunsuz çalisir.
}
}
});
butonGoster.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String sehirAdi = (String) comboSehir.getSelectedItem();
comboSehir.setSelectedIndex(0);
String ilceAdi = (String) comboIlce.getSelectedItem();
comboIlce.setSelectedIndex(0);
// String squareSymbol = new String(Character.toChars(0x25A0)); // ■ -> https://unicode-table.com/en/25A0/
String squareSymbol = "\u25A0"; // ■ -> https://unicode-table.com/en/25A0/
areaSecilenler.append(squareSymbol+squareSymbol+squareSymbol+"\nSeçilen şehir ve ilçe bilgisi:\nŞehir : "+ sehirAdi + "\nİlçe : " + ilceAdi + "\n");
// areaSecilenler.setText(squareSymbol+squareSymbol+squareSymbol+"\nSeçilen şehir ve ilçe bilgisi:\nŞehir : "+ sehirAdi + "\nİlçe : " + ilceAdi + "\n");
}
});
frame.add(labelBaslik);
frame.add(labelSehir);
frame.add(comboSehir);
frame.add(labelIlce);
frame.add(comboIlce);
frame.add(butonGoster);
frame.add(areaPane);
frame.setSize(400, 500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}