Programming Basics SQL HTML CSS JavaScript Python C++ Java JavaFX Swing Problem Solving English English Conversations Computer Fundamentals Learn Typing

Swingطريقة جعل المستخدم يقوم بإضافة أو حذف عناصر في JComboBox و إظهار عدد جميع العناصر الموجودة فيه

المثال التالي يعلمك طريقة جعل المستخدم يقوم بإضافة أو حذف عناصر في كائن الـ JComboBox. بالإضافة إلى إظهار عدد جميع العناصر الموجودة فيه.


مثال

Main.java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Main {
// هنا قمنا بإنشاء النافذة و جميع الأشياء التي سنضعها فيها
static JFrame frame = new JFrame("JComboBox demo");
static JComboBox comboBox = new JComboBox();
static JButton addButton = new JButton("Add Item");
static JButton removeButton = new JButton("Remove Selected Item");
static JButton removeAllButton = new JButton("Remove All Items");
static JLabel counterLabel = new JLabel("Total items = 0");
static JTextField textField = new JTextField();
public static void main(String[] args) {
// frame هنا قمنا بتحديد أماكن الأشياء التي سنضيفها في الـ
textField.setBounds(40, 40, 110, 30);
addButton.setBounds(170, 40, 120, 30);
removeButton.setBounds(40, 80, 250, 30);
removeAllButton.setBounds(40, 120, 250, 30);
comboBox.setBounds(310, 40, 140, 30);
counterLabel.setBounds(310, 80, 140, 30);
// frame هنا قمنا بإضافة جميع الأشياء التي قمنا بتعريفها سابقاً في الـ
frame.add(textField);
frame.add(addButton);
frame.add(removeButton);
frame.add(removeAllButton);
frame.add(comboBox);
frame.add(counterLabel);
// frame هنا قمنا بتحديد خصائص الـ
frame.setSize(500, 250);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.setVisible(true);
// addButton هنا نضع الأوامر التي نريد تنفيذها عند النقر على الـ
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
// إذا كان مربع النص غير فارغ و قام المستخدم بالنقر على الزر, عندها سيتم إضافته
if( !textField.getText().equals("") )
{
comboBox.addItem(textField.getText());
counterLabel.setText("Total Items = "+ comboBox.getItemCount());
}
}
});
// removeButton هنا نضع الأوامر التي نريد تنفيذها عند النقر على الـ
removeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
// سيتم حذف العنصر الحالي بشرط وجود عنصر واحد على الأقل فيها
if( comboBox.getItemCount() > 0 )
{
comboBox.removeItemAt(comboBox.getSelectedIndex());
counterLabel.setText("Total Items = "+ comboBox.getItemCount());
}
}
});
// removeAllButton هنا نضع الأوامر التي نريد تنفيذها عند النقر على الـ
removeAllButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
// عند النقر على هذا الزر, سيتم حذف جميع العناصر
comboBox.removeAllItems();
counterLabel.setText("Total Items = "+ comboBox.getItemCount());
}
});
}
}
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JLabel; import javax.swing.JTextField; public class Main { // هنا قمنا بإنشاء النافذة و جميع الأشياء التي سنضعها فيها static JFrame frame = new JFrame("JComboBox demo"); static JComboBox comboBox = new JComboBox(); static JButton addButton = new JButton("Add Item"); static JButton removeButton = new JButton("Remove Selected Item"); static JButton removeAllButton = new JButton("Remove All Items"); static JLabel counterLabel = new JLabel("Total items = 0"); static JTextField textField = new JTextField(); public static void main(String[] args) { // frame هنا قمنا بتحديد أماكن الأشياء التي سنضيفها في الـ textField.setBounds(40, 40, 110, 30); addButton.setBounds(170, 40, 120, 30); removeButton.setBounds(40, 80, 250, 30); removeAllButton.setBounds(40, 120, 250, 30); comboBox.setBounds(310, 40, 140, 30); counterLabel.setBounds(310, 80, 140, 30); // frame هنا قمنا بإضافة جميع الأشياء التي قمنا بتعريفها سابقاً في الـ frame.add(textField); frame.add(addButton); frame.add(removeButton); frame.add(removeAllButton); frame.add(comboBox); frame.add(counterLabel); // frame هنا قمنا بتحديد خصائص الـ frame.setSize(500, 250); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(null); frame.setVisible(true); // addButton هنا نضع الأوامر التي نريد تنفيذها عند النقر على الـ addButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // إذا كان مربع النص غير فارغ و قام المستخدم بالنقر على الزر, عندها سيتم إضافته if( !textField.getText().equals("") ) { comboBox.addItem(textField.getText()); counterLabel.setText("Total Items = "+ comboBox.getItemCount()); } } }); // removeButton هنا نضع الأوامر التي نريد تنفيذها عند النقر على الـ removeButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // سيتم حذف العنصر الحالي بشرط وجود عنصر واحد على الأقل فيها if( comboBox.getItemCount() > 0 ) { comboBox.removeItemAt(comboBox.getSelectedIndex()); counterLabel.setText("Total Items = "+ comboBox.getItemCount()); } } }); // removeAllButton هنا نضع الأوامر التي نريد تنفيذها عند النقر على الـ removeAllButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // عند النقر على هذا الزر, سيتم حذف جميع العناصر comboBox.removeAllItems(); counterLabel.setText("Total Items = "+ comboBox.getItemCount()); } }); } }

ستظهر لك النافذة التالية عند التشغيل. قم بتجربته بإضافة و حذف بعض العناصر.

طريقة إضافة عناصر في ال JComboBox