单击按钮时,我将文本字段(输入)中的项目保存到JComboBox.用户可以给出以小写字母开头的输入,但我想将输入的第一个字母更改为大写.我怎样才能做到这一点?

两个comboBox使用相同的代码,唯一的例外是"combo 1"被设置为可编辑而"Combo 2"不是.两者都选择了"第1项".如您所见,"组合1"在组合文本字段中打印"[Ljava.lang.Object; @ 77905258","组合2"打印所选名称.
"组合1"如何在组合框文本字段中打印相同的文本作为"组合2"?
符合E :(参见附图)如果单击"组合1",您将看到项目1,项目2的列表......但文本字段将显示"[Ljava.lang.Object; @ 77905258"选择项目时(此处为选中的项目1).
如果单击"组合2",您将看到项目1,项目2的列表...当选择"项目1"时,不可编辑的文本字段将显示"项目1".
这是代码:
我正在使用自定义渲染器:
public class MyListRenderer extends JLabel
implements ListCellRenderer{
@Override
public Component getListCellRendererComponent(
JList list, Object value,
int index, boolean isSelected,
boolean cellHasFocus) {
Object[] itemData =(Object[])value;
setText((String)itemData[1]);
return this;
}
}
Run Code Online (Sandbox Code Playgroud)
以下是设置2个组合框列表的代码:
private void iniCombobox() {
cmbMyCombo.addItem(new Object[] {"1", "Item 1"});
cmbMyCombo.addItem(new Object[] {"2", "Item 2"});
cmbMyCombo.addItem(new Object[] {"3", "Item 3"});
cmbMyCombo2.addItem(new Object[] {"1", "Item 1"});
cmbMyCombo2.addItem(new Object[] {"2", "Item 2"});
cmbMyCombo2.addItem(new Object[] {"3", "Item 3"});
cmbMyCombo.setRenderer(new MyListRenderer()); …Run Code Online (Sandbox Code Playgroud) 我需要一些关于使用Java OOP的GUI的帮助,我正在使用Eclipse.
我正在使用Combobox,JLabel和图片创建"精选航空公司"GUI.
第一选择,F16(组合框)加600美元(JLabel)F16.jpg(在组合框之外).
但是在面板内部选择了第二选择F22(组合框)时,JLabel会自动更改加$ 900(JLabel)以及图片到F12.jpg
任何人都可以帮我使用Combobox,JLabel编码,图片
非常感谢!
我已经看到一个较旧的问题,答案是下面的代码,但如果我使用netbeans,我已经设计了我的comboBox.所以我认为(我想象的是Java和netbeans中的新东西!)代码的最后一行应该改变,我在哪里插入这段代码?
BufferedReader input = new BufferedReader(new FileReader(filePath));
List<String> strings = new ArrayList<String>();
try {
String line = null;
while (( line = input.readLine()) != null){
strings.add(line);
}
}
catch (FileNotFoundException e) {
System.err.println("Error, file " + filePath + " didn't exist.");
}
finally {
input.close();
}
String[] lineArray = strings.toArray(new String[]{});
JComboBox comboBox = new JComboBox(lineArray);
Run Code Online (Sandbox Code Playgroud) 我不知道怎么做在特定行中设置一个jcombobox ...现在我对所有行都有这个jcombobox,但我想只在一行中:
JComboBox cc = new JComboBox();
cc.addItem(jComboBox5.getSelectedItem()+"/"+jComboBox6.getSelectedItem()+"/"+jComboBox7.getSelectedItem()+" "+jComboBox1.getSelectedItem()+"."+jComboBox2.getSelectedItem());
jTable1.getColumnModel().getColumn(3).setCellEditor(new DefaultCellEditor(cc));
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setToolTipText("CLICCA PER LE DATE");
jTable1.getColumnModel().getColumn(3).setCellRenderer(renderer);
Run Code Online (Sandbox Code Playgroud) 我正在使用物质L&F并且我已设置JComboBox为可编辑,以便我可以从其弹出窗口中选择我想要的值,或在其编辑器中键入新值.
键入一个新值工作正常,但如果我想从Combo编辑器中删除一个未命名的字母,然后单击Backspace来执行此操作,它会选择编辑器中的字母而不是删除它们.这是一个截图:
我希望Combo编辑器在输入键盘字母或退格键或删除键时像JTextField一样工作,那么有没有办法做到这一点?或者是什么导致了这个?
Is it possible to populate a JComboBox with an int[]? I am writing a code that will use a JComboBox populated with years (in integers).
The code I have written is this:
int[] birthYear = new int[currentYear]; //currentYear is an int variable
int inc=1;
for(int i=0;i<currentYear;i++)
{
birthYear[i]= inc;
inc++;
}
birthYearBox = new JComboBox(birthYear); //this is the line in which the error occurs
Run Code Online (Sandbox Code Playgroud)
I want these to integers in the ComboBox so that I can subtract from them. Do …
当我从组合框中选择任何项目时,我想写一些文本字段.但我不能这样做.
Java代码:
comboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent arg0) {
if(comboBox.getSelectedItem()=="apple") {
tfbf.setText("apple selected");
}
}
});
Run Code Online (Sandbox Code Playgroud) 我的问题是这个问题的反面:有没有办法在JOptionPane showInputDialog(而且没有CANCEL按钮)中只有OK按钮?
对此的一个解决方案是(如果我正确读取)添加任意JPanel,在该实例中是标签.我的问题是我需要在消息窗口中有一个JComboBox对象,并且(以解决Coffee_Table问题的方式相同)让JComboBox似乎删除了取消按钮.如果我用OK_CANCEL_OPTION或QUESTION_MESSAGE替换YES_NO_CANCEL_OPTION并不重要.
我仍然处于学习JOptionPane系列的无意识复制阶段,所以我认为解决方案是显而易见的,我只是不知道它,因为我没有看到任何具体的例子无意识地复制.(这也意味着一旦我学会了如何添加取消按钮,我就需要研究如何访问是否用户点击它.编辑:我不确定我是怎么做的,所以你不要如果你不想,你需要回答它.)
public static void main(String[] args) {
int numCh1 = 1;
String[] moves = {"rock","paper","scissors"};
JComboBox<?> optionList = new JComboBox<Object>(moves);
JOptionPane.showMessageDialog(
null,
optionList,
"Player One: Choose a Move",
JOptionPane.YES_NO_CANCEL_OPTION
);
numCh1 = optionList.getSelectedIndex();
System.out.println(moves[numCh1]);
}
Run Code Online (Sandbox Code Playgroud)
注意:组合框是不可协商的(而不是三个按钮),因为我的实际项目是模拟rps101 ; 我只是认为你不需要看到所有100个动作(或其他任何与此问题无关的动作).
当我按下按钮时,我尝试测试是否可以更改组合框选择的索引,但是如果我的组合框从另一个类添加到我的框架中,它对我来说什么都没有用,请你告诉我一下我想 ?
我创建组合框的类是:
package MyPackage;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
public class AddMyBox {
private JComboBox combobox;
String[] array = {"Select", "1", "2", "3"};
public JComboBox theBox() {
combobox = new JComboBox();
combobox.setModel(new DefaultComboBoxModel(array));
combobox.setBounds(10, 11, 414, 20);
return combobox;
}
}
Run Code Online (Sandbox Code Playgroud)
以及创建框架的类和添加组件的位置:
package MyPackage;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class MyFrame extends JFrame {
public MyFrame() {
getContentPane().setLayout(null);
setVisible(true);
// adding the comboBox from class AddMyBox
AddMyBox getBox = new AddMyBox();
getContentPane().add(getBox.theBox());
JButton btnNewButton = new JButton("New button"); …Run Code Online (Sandbox Code Playgroud)