3

内のデータを更新できませんJComboBox

にアイテムを追加するボタン「Create」があります。ActionListenerJComboBox

ただし、変更はGUIに反映されません。まだ新しく追加されたアイテムが表示されません。

repaint()助けにはなりません。

更新:(ほぼ)完全なGUIコードは次のとおりです。

public class Main extends JFrame implements ActionListener
{
    static Connection conn;
    static PreparedStatement ps = null;
    static ResultSet res;

    static Statement sta;

private final static int ITERATION_NUMBER = 1000;

public void GUI () throws SQLException {
    setBounds(0, 0, 320, 240);
    addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent we){
        close(ps);
        close(res);
        close(conn);
        System.exit(0);
        }
    });
    setMinimumSize(new Dimension(320, 240));
    setResizable(false);

    this.setTitle("Accounts");

    JPanel panel = new JPanel();
    GridLayout2 GL = new GridLayout2(4,3);
    GL.setHgap(10);
    panel.setLayout(GL);

    Font font = new Font("Serif", Font.BOLD, 20);
    Font font2 = new Font("Courier New", Font.BOLD, 16);

    JLabel label1 = new JLabel("Username");
    JLabel label2 = new JLabel("Password");
    JLabel label3 = new JLabel("Controls");

    label1.setFont(font2);
    label2.setFont(font2);
    label3.setFont(font2);

    final JTextField username = new JTextField();
    final JTextField password1 = new JPasswordField();
    final JTextField password2 = new JPasswordField();

    final JComboBox userBox1 = new JComboBox();
    final JComboBox userBox2 = new JComboBox();

    JButton create = new JButton("CREATE");

    create.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)
        {
            try {
                createUser(conn, username.getText(), password1.getText());
userBox1.addItem(username.getText());
                userBox2.addItem(username.getText());
            } catch (NoSuchAlgorithmException
                    | UnsupportedEncodingException | SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    });



    userBox1.removeAllItems();
    userBox2.removeAllItems();

    res = (ResultSet) sta.executeQuery("SELECT LOGIN FROM ACCOUNTS");

    String temp;

    for (int i=0; res.next(); i++) {
        temp = (String)res.getString("LOGIN");
        userBox1.addItem(temp);
        userBox2.addItem(temp);
    }

    panel.add(label1);
    panel.add(label2);
    panel.add(label3);

    panel.add(username);
    panel.add(password1);
    panel.add(create);

    panel.add(userBox1);
    panel.add(password2);
    panel.add(modify);

    panel.add(userBox2);
    panel.add(new JLabel(""));
    panel.add(delete);

    add(panel);

    setVisible(true);
}

解決策: password1.setText( "");を追加します。「createUser」が問題を解決した直後!それは奇妙です、多分それはどういうわけかGUIをリフレッシュしました...

4

4 に答える 4

7
  • に追加ComboBoxModelする必要がありますJComboBox

  • そこにadd/ remove/modify値、

  • API に実装されたイベントは、JComboBoxコード行を追加せずにビュー ( ) を更新します

  • すべての更新は、Event Dispatch Thread

編集

JComboBox を既に表示されている GUI に追加する場合は、質問を読み間違えた可能性があります (最後のコード行として、1 つのコンテナーに対して 1 回だけ成功する) を呼び出す必要があります。

myContainer.revalidate() // for JFrame up to Java7 is there only validate()
myContainer.repaint()

(すみません@timashew)

于 2012-06-05T20:52:02.887 に答える
3
static class TestFrame extends JFrame implements ActionListener
{
    private JComboBox testBox = new JComboBox();
    private JButton testButton = new JButton();
    int c = 0;

    public TestFrame()
    {
        testBox = new JComboBox();
        testButton = new JButton("Click Me!");
        testButton.addActionListener(this);

        JPanel panel = new JPanel(new GridLayout(2,1));
        panel.add(testBox);
        panel.add(testButton);
        this.add(panel);
        pack();
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        testBox.addItem("test" + c++);
    }
}

このテスト ケースは機能します。クリックされたコンポーネントにリスナーを追加しましたか?

于 2012-06-05T20:58:57.157 に答える
-3

このコードは、ボタン クリック イベントで jframeform を更新するためのボタン クリック イベントです。

new room().show();  //room() is a jframeform
new room().setVisible(false);
于 2013-09-26T05:39:03.693 に答える