Heyy;
我正在使用java中的hibernate开发一个基于swing的小应用程序.我想从数据库coloumn填充组合框.我怎么能这样做?
我不知道在(下initComponents,buttonActionPerformd)我需要做的.
为了使用jbutton保存我,它的代码在这里:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int idd=Integer.parseInt(jTextField1.getText());
String name=jTextField2.getText();
String description=jTextField3.getText();
Session session = null;
SessionFactory sessionFactory = new Configuration().configure()
.buildSessionFactory();
session = sessionFactory.openSession();
Transaction transaction = session.getTransaction();
try {
ContactGroup con = new ContactGroup();
con.setId(idd);
con.setGroupName(name);
con.setGroupDescription(description);
transaction.begin();
session.save(con);
transaction.commit();
} catch (Exception e) {
e.printStackTrace();
}
finally{
session.close();
}
}
Run Code Online (Sandbox Code Playgroud) 我是 Java 新手,所以这个问题对某些人来说可能很愚蠢。我将 Eclipse 与摆动窗口构建器一起使用,但我不知道如何使我的 Jframe 工作。
我想要实现的目标很简单。
我希望这个字符串出现在标签或其他东西中(也可能复制到剪贴板或一些)。
我希望有人能帮助我在这个任务中走得更远。
jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
Run Code Online (Sandbox Code Playgroud)
我想知道,如何在运行时在comboBox中添加元素?
所以我用的JComboBox是ArrayList:
protected JComboBox<String> jcb;
protected ArrayList<String> favourites;
favourites.add("Favourites");
favourites.add("-0.21 + 0.77");
favourites.add("-0.16 + -0.89");
jcb = new JComboBox(favourites.toArray());
Run Code Online (Sandbox Code Playgroud)
这工作正常,我可以选择每个选项,并执行我需要做的选定的语句.但是,当我希望更新它时JComboBox,它不会在我的GUI上更新.在另一种方法中我称之为:
favourites.add("10 + 4");
jcb.revalidate();
jcb.repaint();
Run Code Online (Sandbox Code Playgroud)
我已经测试ArrayList过已经更新(见下文),但它没有在我的GUI上显示任何建议?谢谢,
for (String s : favourites)
System.out.println(s);
Run Code Online (Sandbox Code Playgroud) 我有一个ComboBox我有8个项目,其中我想要显示所有但在某个条件下,用户应该只能选择前两个,所以我已经编程,如果条件为真,用户选择任何其他选项,然后显示消息框显示"You cannot choose this",然后自动选择默认值.到现在为止还挺好.
但现在的问题是用户无法通过查看JComboBox可以选择的选项来确定,所以我想要做的是,如果条件为真,那么除了前两个选项之外的所有选项都应该被禁用(或者灰色或其他东西),以便用户可以知道你无法选择它,如果他们仍然这样做,那么我的消息框应该出现.
我尝试了什么:我试着查看这个,但我无法弄清楚问题中做了什么(它的答案对我没有用),我也尝试了其他选择,但没有成功.
注意:我正在使用Netbeans GUI来创建所有内容,我正在编写的代码已经开启JComboBoxActionPerformed,我是一个新手,所以我无法弄清楚自己,为此道歉
我想让 JComboBox 有圆角。我带着这个代码:
public class BPosCmbBox extends JCboEdc {
public BPosCmbBox() {
super();
setBorder(new RoundBorder());
}
@Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
super.paint(g); //To change body of generated methods, choose Tools | Templates.
}
}
public class RoundBorder extends AbstractBorder {
Color bgColor = new Color(0, 0, 0, 220);
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
((Graphics2D) g).setColor(bgColor);
((Graphics2D) g).drawRoundRect(x, y, width …Run Code Online (Sandbox Code Playgroud) 这个目的主要是为了美观,我已经在 JTextArea 上做过类似的事情,但我无法弄清楚或无法从 JComboBox 的弹出窗口中访问垂直滚动条。我已经通过将其witdh设置为0来删除显示在顶部的箭头。
setUI(new BasicComboBoxUI(){
protected JButton createArrowButton(){
return new JButton(){
@Override public int getWidth() {
return 0;
}
@Override
public void setFocusable(boolean focusable) {
super.setFocusable(false);
}
};
}
});
Run Code Online (Sandbox Code Playgroud)
这是 JScrollPane 内的 JTextArea,没有箭头和更细的条
getVerticalScrollBar().setPreferredSize(new Dimension(10, 0));
getVerticalScrollBar().setUI(new BasicScrollBarUI(){
@Override
protected JButton createDecreaseButton(int orientation) {
return createZeroButton();
}
@Override
protected JButton createIncreaseButton(int orientation) {
return createZeroButton();
}
private JButton createZeroButton() {
JButton jbutton = new JButton();
jbutton.setPreferredSize(new Dimension(0, 0));
jbutton.setMinimumSize(new Dimension(0, 0));
jbutton.setMaximumSize(new Dimension(0, 0));
return …Run Code Online (Sandbox Code Playgroud) 
我想使JComboBox组件显示String名称,而不是引用.但是,我不知道如何做到这一点.
下面显示了我的代码:
public class Properties extends JPanel implements ItemListener {
private static final long serialVersionUID = -8555733808183623384L;
private static final Dimension SIZE = new Dimension(130, 80);
private JComboBox<Category> tileCategory;
public Properties() {
tileCategory = new JComboBox<Category>();
tileCategory.setPreferredSize(SIZE);
tileCategory.addItemListener(this);
this.setLayout(new GridLayout(16, 1));
loadCategory();
}
private void loadCategory() {
//Obtains a HashMap of Strings from somewhere else. All of this is constant, so they
//aren't modified at runtime.
HashMap<Integer, String> map = EditorConstants.getInstance().getCategoryList();
DefaultComboBoxModel<Category> model = (DefaultComboBoxModel<Category>) this.tileCategory.getModel(); …Run Code Online (Sandbox Code Playgroud) 如果我实例化Swing的JCombobox类并将String [] [] - 项添加到它 - 除了我收到以下警告之外没有问题:
JComboBox是原始类型.应该对泛型类型JComboBox的引用进行参数化.
好吧 - 我以下面的方式参数化对象
private JComboBox <String[][]> myComboBox = new JComboBox <String[][]> ();
Run Code Online (Sandbox Code Playgroud)
它起初看起来很好,因为警告消失了,但是当我想要从String [] [] - 对象添加项时我收到错误
myComboBox.addItem(this.stringList[i][1]);
Run Code Online (Sandbox Code Playgroud)
错误信息:
*the method addItem(String[][]) in the type JComboBox <String[][]> is not applicable for the arguments (String).*
Run Code Online (Sandbox Code Playgroud)
我做错了什么,如何解决?
顺便说一句 - 如果你有时间回答更多 - 使用rawtypes有危险/缺点吗?
有没有简单的方法将a的起始索引设置JComboBox为"1"或"2"?如果你启动你的应用程序,索引正常设置为"0",但我想从索引"1"开始.
编辑:
JComboBox variableBox_1 = new JComboBox();
for (int i = 0; i < dataModel.getVariableNames().size(); i++) {
variableBox_1.addItem(dataModel.getVariableNames().get(i));
}
JPanel comBoxPanel1 = new JPanel(new BorderLayout());
JLabel comBoxLabel1 = new JLabel("X:");
comBoxPanel1.add(variableBox_1, BorderLayout.CENTER);
comBoxPanel1.add(comBoxLabel1, BorderLayout.WEST);
optionPanel.add(comBoxPanel1);
variableBox_1.addActionListener((ActionEvent e) -> {
sp.setVariableNumberX(variableBox_1.getSelectedIndex());
hg1.setVariableNumber(variableBox_1.getSelectedIndex());
sp.setXvariableText(dataModel.getVariableNames().get(variableBox_1.getSelectedIndex()));
});
Run Code Online (Sandbox Code Playgroud)