我的JComboBox模型包含了诸如项目item1,item2,item1.我的问题是当我选择第三项(item1)JComboBox并检查getSelectedIndex()它总是返回0.
如果我的模型中的项目相同,我如何获得不同的每个项目的索引?喜欢:
我有一个组合框,你可以在代码中看到从表中获取值.所以在我点击确定后,表格的值会发生变化.如何在不关闭和打开jframe的情况下看到compobox的这些新值?我今天做了很多关于java.awt.EventQueue.invokeLater和其他工作人员的研究,但我不能使它工作,我是java新手和编程通用.所以这是代码:
public class Compo extends JFrame implements ActionListener
{//start of class Compo
//start of variables
private JComboBox<String> CompoBox;
private String array[];
private JButton okButton;
private JPanel panel;
//end of variables
public Compo ()
{//start of Compo method
super("Example");
panel=new JPanel(null);
//table = new String[3];
array= new String[3];
array[0]="alpha";
array[1]="beta";
array[2]="charlie";
CompoBox= new JComboBox<>(array);
CompoBox.setBounds(50, 70, 100, 20);
panel.add(CompoBox);
okButton=new JButton("ok");
okButton.setBounds(50, 120, 70, 30);
okButton.setActionCommand("ok");
okButton.addActionListener(this);
panel.add(okButton);
add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200, 200);
setVisible(true);
}//end of compo method
@Override
public void actionPerformed(ActionEvent …Run Code Online (Sandbox Code Playgroud) 我有一个弹出窗口,向用户显示设置.如果你点击它外面,它隐藏但如果你点击其中它仍然可见.
处理此行为的事件处理程序获取Component(被单击)并通过component.getParent()递归使用我可以检查它是否是我的设置面板的子项.到目前为止这已经奏效了.
但是我刚刚JComboBox在该面板中添加了一个,结果是"可选项弹出"(它有一个名字?)组合框在点击时显示的不是组合框的孩子.尝试在组合框中选择某些内容会隐藏我的设置面板.
使用NetBeans调试器我可以看到它的类型BasicComboPopup$1(是一个匿名类?),但它既不是ComboPopup,JPopupMenu也不是BasicComboPopup.
我需要一种方法来识别被点击的"组合框弹出窗口"的所有者/父组合框.
我试图使用此代码将当前日期和第二天的日期放在JComboBox中
private void dateCombo(){
Calendar cal = new GregorianCalendar();
int month =cal.get(Calendar.MONTH);
int year =cal.get(Calendar.YEAR);
int day =cal.get(Calendar.DAY_OF_MONTH);
cmb_date.addItem(+year+"-"+(month+1)+"-"+day);
cmb_date.addItem(+year+"-"+(month+1)+"-"+(day+1));
}
Run Code Online (Sandbox Code Playgroud)
但它以"yyyy-md"格式显示日期,我希望它以'yyyy-mm-dd'格式显示.
我想我可以用
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
txt_date.setText(sdf.format(date));
Run Code Online (Sandbox Code Playgroud)
以'yyyy-mm-dd'格式获取当前日期,但如何处理第二天的日期?
我正在为一些课程制作剧院座位预订系统,但是在创建JComboBox时遇到了麻烦,以帮助用户选择197套的座位.
座位由"座位"对象表示,这些对象仅仅是诸如"isBooked"(布尔)之类的一些变量的集合.座椅排列成多个座椅阵列,每个阵列代表一排座椅,例如A [],B [] ......
对于座位的预订,由于它们具有不同的价格,因此需要按行分开座位,但是JComboBox将被用作选择要取消预订的座位的方式,因此仅需要完整的座位列表.
我可以轻松地将单个数组添加到JComboBox并使其正常工作但尝试将更多数组添加到JComboBox中的列表失败.
如何将多个数组添加到JComboBox?即A [1],A [2],A [3] ...... A [14],B [1],B [2] ......
我对Java不是很有经验,如果这是一个愚蠢的问题,对不起.经过过去几天的大量研究,我尝试过使用DefaultComboBoxModel类,但显然没有正确使用它.这是我最近尝试解决我的问题:
DefaultComboBoxModel model = new DefaultComboBoxModel();
model.addElement(A);
model.addElement(B);
model.addElement(C);
model.addElement(D);
model.addElement(E);
model.addElement(F);
model.addElement(G);
model.addElement(H);
model.addElement(J);
model.addElement(K);
model.addElement(L);
JComboBox seatCombobox = new JComboBox();
seatCombobox.setModel(model);
unbookSeatWindow.buttonsPanel.add(seatCombobox);
Run Code Online (Sandbox Code Playgroud)
所有帮助将不胜感激.
我有一个JComboBox有12个不同的选择,根据选择的内容,我希望问题(JLabel)更改匹配选择.我已经尝试了一个if语句来查看所选内容以及它是否与应该选择的内容相匹配,然后问题会相应地改变,但JLabel在某种情况下从未真正改变过.
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Window extends JFrame{
private static final long serialVersionUID = 1L;
public Window(){
super("Area Finder v1.0");
BufferedImage image = null;
try {
image = ImageIO.read(getClass().getClassLoader().getResource("images/areafinder.png"));
} catch (IOException e) {
e.printStackTrace();
}
super.setIconImage(image);
setSize(400, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JLabel Instr = new JLabel("What would kind of area would you like to find?");
String[] areaChoices = {"Circle", "Square", "Rectangle", "Triangle", "Trapezoid", "Parallelogram", …Run Code Online (Sandbox Code Playgroud) 我使用DefaultComboBoxModel和自定义Item填充组合框.每个项目都包含一个ID和一个名称.我正在遍历一个表,并根据选择,我想从组合框中删除选定的元素.对于我想删除的项目,我有我正在迭代的表中的ID和名称.我尝试使用removeItem接受一个对象.我将ID和Name传递给我的自定义Item构造函数,但这似乎不起作用.谁能告诉我这里缺少什么?
填充组合框的代码:
Vector<Object> companyList = new Vector<Object>();
while(rs.next()){
companyList.addElement(new Item(rs.getInt(1),rs.getString(2)));
}
DefaultComboBoxModel cmod = new DefaultComboBoxModel(companyList);
companyName.setModel(cmod);
Run Code Online (Sandbox Code Playgroud)
自定义项代码:
class Item
{
private int id;
private String name;
public Item(int id, String name)
{
this.id = id;
this.name = name;
}
public int getId()
{
return id;
}
public String getName()
{
return name;
}
public String toString()
{
return name;
}
}
Run Code Online (Sandbox Code Playgroud)
删除Item的代码(本例中为硬编码):
companyName.removeItem(new Item(50002,"ALLIED WASTE SYSTEMS"));
Run Code Online (Sandbox Code Playgroud)
removeItem说它接受一个Object所以我不确定为什么这不起作用.任何帮助,将不胜感激!
您好,我正在使用一个Combobox,当我触发IteamStateChage事件时,该操作调用了两次.但如果我打了两次电话,我就无法让我想做的事情成为可能.
那么有没有办法只调用一次IteamStatechange evet.当我将iteam改为Jcombobox时.我只需要一次itemStatechange的动作.并且仅使用项目状态更改.![此处屏幕截图相同.] [1]
请帮帮我.并提前感谢你.
我创建JTable了包含有关员工的信息.在这个JTable中,我添加了名为"Qualifications"的列.此列由JComboBox(每行的不同内容)表示.例如:
Row 1 | JComboBox(){"Programmer","Web"}
Row 2 | JComboBox(){"Writer","Editor"}
Run Code Online (Sandbox Code Playgroud)
该JComboBox内容从拍摄List<String> employees[row].getQualification().
问题是第1行和第2行中的所选项目是"程序员",但"程序员"不应出现在第2行中.只有当我点击时JComboBox,才会出现正确的列表,即第2行 - {"Writer","编辑"}.
TableColumn column = table.getColumnModel().getColumn(5);
column.setCellRenderer(getRenderer());
private TableCellRenderer getRenderer() {
return new TableCellRenderer() {
private JComboBox<String> box = new JComboBox<String>();
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
for (String q : employees[row].getQualification())
box.addItem(q);
box.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
box.setForeground(isSelected ? table.getSelectionForeground() : table.getForeground());
return box;
}
};
}
Run Code Online (Sandbox Code Playgroud) 我创建了一个类来保存comboxbox的内容并加载它们.我使用ObjectOutput并直接保存模型.问题是,我得到一个例外:
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: javax.swing.SpringLayout
Run Code Online (Sandbox Code Playgroud)
我真的不明白为什么会发生这种情况,因为我没有在任何代码中使用SpringLayout(我可以告诉).我甚至设置了一个断点,stream.writeOutput(getModel());并且getModel()肯定会返回a DefaultComboboxModel,所以再次没有提到Spring Layout.
然后我编译了我从这里获得示例的代码,它工作正常.为什么会这样?
class MemComboBox extends JComboBox
{
public static final int MAX_MEM_LEN = 30;
public MemComboBox()
{
super();
setEditable(true);
}
public void add(String item)
{
removeItem(item);
insertItemAt(item, 0);
setSelectedItem(item);
if (getItemCount() > MAX_MEM_LEN)
removeItemAt(getItemCount() - 1);
}
public void load(String fName)
{
try
{
if (getItemCount() > 0)
removeAllItems();
File f = new File(fName);
if (!f.exists())
return;
FileInputStream fStream = new FileInputStream(f);
ObjectInput …Run Code Online (Sandbox Code Playgroud)