标题:下拉框不能动态更新
只看楼主
mahayu
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:122
专家分:160
注 册:2007-8-3
 问题点数:0 回复次数:0 
下拉框不能动态更新
package com.One_Work;

/**
 * 工作日志
 */
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.ResultSet;
import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;

import com.OA.Dao.ResultSetTableModel;
import com.OA.Dao.dbsql;

public class myJournal extends JInternalFrame {
    private JTable table_show;
    private JTextField Ttitle;
    private JTextArea Tcontent;
    private JTextField Tdate;
    private JComboBox Jclass;
    private ResultSetTableModel model;
    private ResultSet rs;
    private dbsql db;
    private JPanel panel_show;
    private JScrollPane scrollPane_show;

    public myJournal(String title, boolean a, boolean b, boolean c, boolean d) {
        super(title, a, b, c, d);
        getContentPane().setLayout(null);
        setBounds(100, 100, 800, 600);
        this.resizable = false;// 大小不可变

        /**
         * 编辑
         */
        final JPanel pane_edit = new JPanel();
        pane_edit.setLayout(null);
        pane_edit.setBounds(10, 340, 770, 215);
        getContentPane().add(pane_edit);

        Border etched2 = BorderFactory.createEtchedBorder();
        Border titled2 = BorderFactory.createTitledBorder(etched2, "日志编辑");
        pane_edit.setBorder(titled2);

        final JLabel label = new JLabel();
        label.setText("日志类型:");
        label.setBounds(10, 31, 66, 18);
        pane_edit.add(label);

        Jclass = new JComboBox();
        /*Jclass.setAutoscrolls(true);
        Jclass.setModel(new DefaultComboBoxModel(
                new String[] { "工作日志", "个人日志" }));*/
        Jclass.addItem("个人日志");
        Jclass.addItem("工作日志");
        Jclass.addItem("qt");

        Jclass.setBounds(71, 28, 85, 25);
        pane_edit.add(Jclass);

        final JLabel label_1 = new JLabel();
        label_1.setText("日   期:");
        label_1.setBounds(180, 31, 66, 18);
        pane_edit.add(label_1);

        Tdate = new JTextField();
        Tdate.setBounds(229, 29, 137, 22);
        pane_edit.add(Tdate);

        final JScrollPane scrollPane_1 = new JScrollPane();
        scrollPane_1.setBounds(10, 55, 750, 114);
        pane_edit.add(scrollPane_1);

        Tcontent = new JTextArea();
        scrollPane_1.setViewportView(Tcontent);

        final JButton button = new JButton();
        button.addActionListener(new ActionListener() {
            public void actionPerformed(final ActionEvent e) {
                add();
            }
        });
        button.setText("添加");
        button.setBounds(71, 175, 106, 28);
        pane_edit.add(button);

        final JButton button_1 = new JButton();
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(final ActionEvent e) {
                edit();
            }
        });
        button_1.setText("修改");
        button_1.setBounds(235, 175, 106, 28);
        pane_edit.add(button_1);

        final JButton button_2 = new JButton();
        button_2.setText("删除");
        button_2.setBounds(409, 175, 106, 28);
        pane_edit.add(button_2);

        final JButton button_3 = new JButton();
        button_3.setText("查询");
        button_3.setBounds(586, 175, 106, 28);
        pane_edit.add(button_3);

        Ttitle = new JTextField();
        Ttitle.setBounds(466, 29, 206, 22);
        pane_edit.add(Ttitle);

        final JLabel label_2 = new JLabel();
        label_2.setText("标   题:");
        label_2.setBounds(394, 31, 66, 18);
        pane_edit.add(label_2);

        tableshow(); // 显示日志
    }

    private void tableshow() {
        db = new dbsql();
        db.connect();
        String sql = "Select JId,分类=Jlb,标题=Jtitle,内容=Jcontent,创建日期=Jdate from Journal order by JID desc";
        rs = db.query(sql);

        model = new ResultSetTableModel(rs);

        panel_show = new JPanel();
        panel_show.setLayout(new BorderLayout());
        panel_show.setBounds(10, 10, 770, 324);
        getContentPane().add(panel_show);

        Border etched = BorderFactory.createEtchedBorder();
        Border titled = BorderFactory.createTitledBorder(etched, "日志浏览");
        panel_show.setBorder(titled);

        scrollPane_show = new JScrollPane();
        panel_show.add(scrollPane_show, BorderLayout.CENTER);

        table_show = new JTable(model);
        table_show.addMouseListener(new MouseAdapter() {
            public void mouseClicked(final MouseEvent arg0) {
                int row = table_show.getSelectedRow(); // 获得当前选中的行号
                Jclass.setSelectedItem(model.getValueAt(row, 1));                 Ttitle.setText(model.getValueAt(row, 1).toString()); // 标题
                Tcontent.setText(model.getValueAt(row, 2).toString()); // 内容
                Tdate.setText(model.getValueAt(row, 3).toString()); // 日期
            }
        });
        scrollPane_show.setViewportView(table_show);
    }

    /**
     * 新增日志
     */
    private void add() {
        String Clb = (String) Jclass.getSelectedItem();
        String title = Ttitle.getText();
        String d = Tdate.getText();
        String content = Tcontent.getText();
        dbsql db = new dbsql();
        db.connect();
        String sql = "insert into Journal (Jtitle,Jlb,Jdate,Jcontent) values('"
                + title + "','" + Clb + "','" + d + "','" + content + "')";
        db.update(sql);
        db.close();
        getContentPane().remove(panel_show); // 移除当前面板
        tableshow(); // 调用显示日志
    }

}

红色部分就是不能更新的下拉框,我在调试模式能检测到其值是变化的,但为何就是不能显示到下拉框中?
搜索更多相关主题的帖子: import awt javax swing 动态 
2008-03-19 15:07



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-204204-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.969174 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved