Java 动态添加jcheckbox

Java 动态添加jcheckbox,java,swing,jcheckbox,Java,Swing,Jcheckbox,是否有一种方法可以在Java中动态添加JCheckBox类型的项,就像在JComboBox中一样,我们使用addItem方法?如果您想将多个项添加到另一个组件中,类似的方法可能会很有效: List<Component> myList = new Arraylist<Component>() //List for storage Item myItem = new Item(); //New component myList.add(myItem); //Store a

是否有一种方法可以在Java中动态添加
JCheckBox
类型的项,就像在
JComboBox
中一样,我们使用
addItem
方法?

如果您想将多个项添加到另一个组件中,类似的方法可能会很有效:

List<Component>  myList = new Arraylist<Component>() //List for storage
Item myItem = new Item(); //New component
myList.add(myItem);  //Store all the components to add in the list

for(int i = 0; i < myList.size; i++){
myjCheckBox.add(myList[i]); //Add all items from list to jCheckBox
}
List myList=new Arraylist()//存储列表
项目myItem=新项目()//新组件
myList.add(myItem)//存储要添加到列表中的所有组件
对于(int i=0;i
上面的示例使用jCheckBox中继承的方法,应该能够提供您所需要的

希望有帮助

注意,您可能会对渲染组件使用实际的复选框,但这要短几行

import java.awt.*;
import javax.imageio.ImageIO;
import javax.swing.*;

class JCheckList {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                JPanel gui = new JPanel(new BorderLayout());

                JLabel l = new JLabel("Ctrl/shift click to select multiple");
                gui.add(l, BorderLayout.PAGE_START);

                JList<String> list = new JList<String>(
                        ImageIO.getReaderFileSuffixes());
                list.setCellRenderer(new CheckListCellRenderer());
                gui.add(list, BorderLayout.CENTER);

                JOptionPane.showMessageDialog(null, gui);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}

class CheckListCellRenderer extends DefaultListCellRenderer {

    String checked = new String(Character.toChars(9745));
    String unchecked = new String(Character.toChars(9746));

    @Override
    public Component getListCellRendererComponent(
            JList list,
            Object value,
            int index,
            boolean isSelected,
            boolean cellHasFocus) {
        Component c = super.getListCellRendererComponent(
                list,value,index,isSelected,cellHasFocus);
        if (c instanceof JLabel) {
            JLabel l = (JLabel)c;
            String s = (isSelected ? checked : unchecked) + (String)value;
            l.setText(s);
        }

        return c;
    }
}
import java.awt.*;
导入javax.imageio.imageio;
导入javax.swing.*;
类检查列表{
公共静态void main(字符串[]args){
Runnable r=新的Runnable(){
@凌驾
公开募捐{
jpanelgui=newjpanel(newborderlayout());
JLabel=新JLabel(“Ctrl/shift单击选择多个”);
添加(l,BorderLayout.PAGE_START);
JList list=新JList(
getReaderFileSuffix());
setCellRenderer(新的CheckListCellRenderer());
添加(列表、边框布局、中心);
showMessageDialog(null,gui);
}
};
//应在EDT上创建和更新Swing GUI
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency
SwingUtilities.invokeLater(r);
}
}
类CheckListCellRenderer扩展了DefaultListCellRenderer{
选中的字符串=新字符串(Character.toChars(9745));
未选中的字符串=新字符串(Character.toChars(9746));
@凌驾
公共组件GetListCellRenderComponent(
JList列表,
对象值,
整数索引,
他当选了,,
布尔单元(聚焦){
组件c=super.getListCellRenderComponent(
列表、值、索引、isSelected、cellHasFocus);
if(JLabel的c实例){
JLabel=(JLabel)c;
字符串s=(isSelected?选中:未选中)+(字符串)值;
l、 setText(s);
}
返回c;
}
}

为什么要将“项”添加到JCheckbox?您可能是指将按钮添加到按钮组?有一些技巧可以“在运行时添加组件”。显示了如何动态添加标签。“…因为我想选择多个选项。”使用
JList
JList
可以,但我不知道是否有可能添加记号”。这可以在渲染组件中完成。是否还有一些方法可以考虑在JCheckbox中选择的项?您的意思是将从JCheckbox中选择的项也存储到列表中吗?是的,我想存储在我的JCheckbox中选择的项并在我的程序上重用您需要添加一个侦听器,我建议你从这里开始:)