怎么才能时JComboBox的选项 随数据库的数据改变

掰po 2014-11-24 05:43:49
我建立一个JComboBox 初始化的时候已经 把数据绑定进去 然后再从JTextField中输入数据 通过JButton把JTextField中的值插入到JComboBox中 怎么才能插到JComboBox原有选项中的最后,并且让其显示出来, 我试过topicComboModel.addElement(topicField.getText().trim()); 在model里面插入 可以插入 但是不会显示 也试过topicCombo.addItem(topicField.getText().trim()); 也是选项不会变化还是原来刚初始化的选项新插入的选项没有显示
代码如下: 我简化了一下:(我所指的问题 在很多注释的地方 插入 button 的监听器中!)


package com.car.test;

import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import java.util.List;
import java.util.ListIterator;

import javax.swing.*;
import javax.swing.border.*;

import com.car.dao.Dao;
import com.car.model.TbVote;
import com.car.util.*;

public class ComBoBoxTest{

public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new ComBoBoxFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}

class ComBoBoxFrame extends JFrame
{
public ComBoBoxFrame()
{
setTitle("ComBoBoxFrameTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

JPanel panel = new ComBoBox();
add(panel);
}

private static final int DEFAULT_WIDTH = 750;
private static final int DEFAULT_HEIGHT = 400;
private static final long serialVersionUID = 1L;
}

//主要内容
class ComBoBox extends JPanel {

public ComBoBox()
{
super();

setSize(WIDTH, HEIGHT);
setBackground(new Color(160, 160, 52));

JPanel inPanel = new InPanel();
JScrollPane scrollPane = new JScrollPane(inPanel);
scrollPane.setPreferredSize(new Dimension(700, 700));
scrollPane.setBorder(BorderFactory.createEmptyBorder());

add(scrollPane);
}

private class InPanel extends JPanel
{
public InPanel()
{
super();

setLayout(null);
setOpaque(false);
setSize(700, 360);

card = new CardLayout();
cardPanel = new JPanel(card);
cardPanel.setOpaque(false);
cardPanel.setBounds(0, 0, 700, 350);

JPanel firstPanel = new FirstPanel();
firstPanel.setName("first");
cardPanel.add(firstPanel, "first");

add(cardPanel);

card.show(cardPanel, "first");
}

private class FirstPanel extends JPanel
{
public FirstPanel()
{
super();

setLayout(null);
setSize(700, 350);
setBackground(new Color(160, 160, 52));

setContent();
ComBoBox.setContent();//初始化topicCombo的选项 方法在最下面 是静态方法
}

private void setContent()
{
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setOpaque(false);
panel.setBounds(25, 5, 650, 355);

Font font = new MyFont().getFont();

topicLabel = new JLabel("主题:", SwingConstants.RIGHT);
topicLabel.setFont(font);
topicLabel.setBounds(0, 0, 75, 25);
topicField = new JTextField(25);
topicField.setBounds(80, 0, 205, 25);
topicInsertButton = new JButton("插入");
topicInsertButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if(topicField.getText() != null && topicField.getText() != "")
{
TbVote vote = new TbVote();
// vote.setAdmiId(LoginFrame.user.getId());
vote.setContentType("乘车投票");
vote.setDurationTime(3);
vote.setEstablishTime(ChangeDateTime.dateToStrLong(new Date()));
vote.setParticipantNumber(0);
vote.setPicture(null);
vote.setTitle(topicField.getText().trim());
if(Dao.addVote(vote))//往数据库里面写选项 接下来是想同时更新topicCombo 让其选项也随着增加新选项
JOptionPane.showMessageDialog(null, "插入成功,请尽快写选项");
//下面的方法我都试过 可以插入 但是topicCombo内的选项没变过
// topicComboModel.removeAllElements();
// topicComboModel.addElement(topicField.getText().trim());
// topicCombo.setModel(topicComboModel);
// topicComboModel.removeAllElements();
// topicComboModel.addElement(topicField.getText().trim());
// topicComboModel.insertElementAt(topicField.getText().trim(), 1);
// topicCombo.setModel(topicComboModel);
// topicCombo.setSelectedItem(topicField.getText().trim());
// topicCombo.addItem(topicField.getText().trim());
// ComBoBox.setContent();
}
}
});
topicInsertButton.setFont(font);
topicInsertButton.setFocusPainted(false);
topicInsertButton.setBackground(Color.GREEN);
topicInsertButton.setBounds(290, 0, 75, 25);
topicCombo = new JComboBox<String>();
topicComboModel = new DefaultComboBoxModel<String>(new String[]{"请选择"});
topicCombo.setModel(topicComboModel);
topicCombo.setFont(font);
topicCombo.setFocusable(false);
topicCombo.setBounds(370, 0, 200, 25);
topicDeleteButton = new JButton("删除");
topicDeleteButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{

}
});
topicDeleteButton.setFont(font);
topicDeleteButton.setFocusPainted(false);
topicDeleteButton.setBackground(Color.RED);
topicDeleteButton.setBounds(575, 0, 75, 25);

JPanel questionPanel = new JPanel();
Font titleFont = new MyFont().setSize(16).getFont();
questionPanel.setLayout(null);
questionPanel.setOpaque(false);
questionPanel.setBounds(0, 30, 650, 140);
questionPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "问题", TitledBorder.LEFT, TitledBorder.DEFAULT_POSITION, titleFont));
topicOkLabel = new JLabel("题目:", SwingConstants.RIGHT);
topicOkLabel.setFont(font);
topicOkLabel.setBounds(20, 15, 75, 25);
topicOkCombo = new JComboBox<String>();
topicOkComboModel = new DefaultComboBoxModel<String>(new String[]{"请选择"});
topicOkCombo.setModel(topicOkComboModel);
topicOkCombo.setFont(font);
topicOkCombo.setFocusable(false);
topicOkCombo.setBounds(100, 15, 300, 25);
topicOkCombo.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{

}
});

firstQuestionLabel = new JLabel("A选项:", SwingConstants.RIGHT);
firstQuestionLabel.setFont(font);
firstQuestionLabel.setBounds(5, 45, 60, 25);
firstQuestionField = new JTextField(25);
firstQuestionField.setBounds(70, 45, 250, 25);
secondQuestionLabel = new JLabel("B选项:", SwingConstants.RIGHT);
secondQuestionLabel.setFont(font);
secondQuestionLabel.setBounds(325, 45, 60, 25);
secondQuestionField = new JTextField(25);
secondQuestionField.setBounds(385, 45, 250, 25);
thirdQuestionLabel = new JLabel("C选项:", SwingConstants.RIGHT);
thirdQuestionLabel.setFont(font);
thirdQuestionLabel.setBounds(5, 75, 60, 25);
thirdQuestionField = new JTextField(25);
thirdQuestionField.setBounds(70, 75, 250, 25);
fourthQuestionLabel = new JLabel("D选项:", SwingConstants.RIGHT);
fourthQuestionLabel.setFont(font);
fourthQuestionLabel.setBounds(325, 75, 60, 25);
fourthQuestionField = new JTextField(25);
fourthQuestionField.setBounds(385, 75, 250, 25);
questionConfirmButton = new JButton("确定");
questionConfirmButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{

}
});
questionConfirmButton.setFont(font);
questionConfirmButton.setFocusPainted(false);
questionConfirmButton.setBackground(Color.GREEN);
questionConfirmButton.setBounds(255, 105, 75, 25);
questionPanel.add(topicOkLabel);
questionPanel.add(topicOkCombo);
questionPanel.add(firstQuestionLabel);
questionPanel.add(firstQuestionField);
questionPanel.add(secondQuestionLabel);
questionPanel.add(secondQuestionField);
questionPanel.add(thirdQuestionLabel);
questionPanel.add(thirdQuestionField);
questionPanel.add(fourthQuestionLabel);
questionPanel.add(fourthQuestionField);
questionPanel.add(questionConfirmButton);

panel.add(topicLabel);
panel.add(topicField);
panel.add(topicInsertButton);
panel.add(topicCombo);
panel.add(topicDeleteButton);
panel.add(questionPanel);
add(panel);
}

private JLabel topicLabel;
private JLabel topicOkLabel;
private JLabel firstQuestionLabel;
private JLabel secondQuestionLabel;
private JLabel thirdQuestionLabel;
private JLabel fourthQuestionLabel;
private JTextField topicField;
private JTextField firstQuestionField;
private JTextField secondQuestionField;
private JTextField thirdQuestionField;
private JTextField fourthQuestionField;

private JButton topicInsertButton;
private JButton topicDeleteButton;
private JButton questionConfirmButton;
private static final long serialVersionUID = 1L;
}

private static final long serialVersionUID = 1L;
}

public static void setContent()
{
List<TbVote> votes;
votes = Dao.getVotes("乘车投票");

if(votes.size() > 0)
{
ListIterator<TbVote> voteIter = votes.listIterator();
String[] voteStrs = new String[votes.size() + 1];
int i = 1;
while(voteIter.hasNext())
{
voteStrs[i] = voteIter.next().getTitle();
i++;
}
voteStrs[0] = "请选择";
topicComboModel = new DefaultComboBoxModel<String>(voteStrs);
topicOkComboModel = new DefaultComboBoxModel<String>(voteStrs);
}
topicCombo.setModel(topicComboModel);
topicOkCombo.setModel(topicOkComboModel);
}

public static JComboBox<String> topicCombo;
public static JComboBox<String> topicOkCombo;
public static DefaultComboBoxModel<String> topicComboModel;
public static DefaultComboBoxModel<String> topicOkComboModel;

public static CardLayout card;
public static JPanel cardPanel;
private static final int WIDTH = 700;
private static final int HEIGHT = 360;
private static final long serialVersionUID = 1L;
}
...全文
347 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
掰po 2014-11-25
  • 打赏
  • 举报
回复
引用 1 楼 Holmes_Conan 的回复:
LZ,数据绑定,把数据源的数据绑定到该控件的相关属性!
问题解决了。 我原来数据已绑定,只是我用了静态JComboBox才无法更新 具体原因不知道 有人知道具体原因吗 请评论!晚点再结贴!
掰po 2014-11-24
  • 打赏
  • 举报
回复
引用 1 楼 Holmes_Conan 的回复:
LZ,数据绑定,把数据源的数据绑定到该控件的相关属性!
具体应该怎么做?给多点提示~
福尔摩斯23 2014-11-24
  • 打赏
  • 举报
回复
LZ,数据绑定,把数据源的数据绑定到该控件的相关属性!

50,550

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧