将数据从数据库加载到每个字段的Jcheckbox

时间:2012-11-26 09:56:48

标签: java swing jcombobox jcheckbox multipleselection

我正在使用JFreechart库创建一个分析系统,我希望像MS excel中那样具有像数据透视表功能一样的东西,我想要池化一个特定的数据库字段,这样我就可以让Jcheckbox具有来自的每个不同值的名称数据库。我使用JCombobox实现了这个:

Class.forName("oracle.jdbc.driver.OracleDriver");
        dbcon = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "USERNAME", "PASSWORD");
        Statement st = dbcon.createStatement();
        String combo = "Select DORM_NAME from dormitory_master_table";
        ResultSet res = st.executeQuery(combo);
        Vector v = new Vector();
        while (res.next()) {
            String ids = res.getString("DORM_NAME");
            v.add(ids);


            cboDormitory = new JComboBox(v);

这会将所有宿舍名称放入Jcombobox中,但这对我想做的事情无效,因为我需要能够选择多个对象。我该如何实现呢?

1 个答案:

答案 0 :(得分:3)

澄清您是否希望单个JCheckBox中有多个JComboBox才能在屏幕上显示数百个JCheckbox时进行多项选择?

在这种情况下听起来像是JList

请参阅:

JList将允许从值列表中进行多项选择(直接从orcale截取 - 如何使用列表教程来说明我的意思):

enter image description here

以下是我使用JCheckBox s:

的自定义示例

enter image description here

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class JListTest {

    public JListTest() {
        JFrame frame = new JFrame();
        frame.setTitle("JList Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        initComponents(frame);

        frame.pack();
        frame.setVisible(true);
    }

    private void initComponents(JFrame frame) {
        String[] strs = {"swing", "home", "basic", "metal"};

        final JList checkBoxesJList = new JList(createData(strs));
        checkBoxesJList.setCellRenderer(new CheckListRenderer());
        checkBoxesJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        checkBoxesJList.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                int index = checkBoxesJList.locationToIndex(e.getPoint());
                CheckableItem item = (CheckableItem) checkBoxesJList.getModel().getElementAt(index);
                item.setSelected(!item.isSelected());
                Rectangle rect = checkBoxesJList.getCellBounds(index, index);
                checkBoxesJList.repaint(rect);
            }
        });

        JScrollPane scrollPane = new JScrollPane(checkBoxesJList);
        frame.add(scrollPane, BorderLayout.CENTER);
    }

    private CheckableItem[] createData(String[] strs) {
        int n = strs.length;
        CheckableItem[] items = new CheckableItem[n];
        for (int i = 0; i < n; i++) {
            items[i] = new CheckableItem(strs[i]);
        }
        return items;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JListTest();
            }
        });
    }
}

class CheckListRenderer extends JCheckBox implements ListCellRenderer {

    public CheckListRenderer() {
        setBackground(UIManager.getColor("List.textBackground"));
        setForeground(UIManager.getColor("List.textForeground"));
    }

    @Override
    public Component getListCellRendererComponent(JList list, Object value,
            int index, boolean isSelected, boolean hasFocus) {
        setEnabled(list.isEnabled());
        setSelected(((CheckableItem) value).isSelected());
        setFont(list.getFont());
        setText(value.toString());
        return this;
    }
}

class CheckableItem {

    private String str;
    private boolean isSelected;

    public CheckableItem(String str) {
        this.str = str;
        isSelected = false;
    }

    public void setSelected(boolean b) {
        isSelected = b;
    }

    public boolean isSelected() {
        return isSelected;
    }

    @Override
    public String toString() {
        return str;
    }
}

<强>更新

根据你的评论:

1)将Vector v = new Vector();替换为ArrayList<String> v=new ArrayList<>();

2)现在编辑createData(..)使其类似于:

private CheckableItem[] createData(ArrayList<String> strs) {
    int n = strs.size();
    CheckableItem[] items = new CheckableItem[n];
    for (int i = 0; i < n; i++) {
        items[i] = new CheckableItem(strs.get(i));
    }
    return items;
}

3)只需参考createData(我们称之为ArrayList)致电v

createData(v);