gpt4 book ai didi

java - JCheckBox 自动选择第一个选项

转载 作者:行者123 更新时间:2023-12-01 10:36:45 26 4
gpt4 key购买 nike

我正在开发一个系统,一旦选择了 JComboBox 的一个字段, ,更改 JButton 的文本,从连接到离线,具体取决于 JComboBox 中写入的字段的状态。 .

所有这些都已完成,但我有一个问题。

当我运行程序时,系统自动进入事件addActionListener ,并不是说我曾经选择过 JComboBox 的字段?

有人可以帮我吗?

根据答案进行编辑

...下面的代码块被写入在comboBox.addActionListener 之前执行的方法中。编写这段代码是因为我需要动态地填充 JComboBox 的元素...

private void actionOfSearch(){

table.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent me){

if(ipAvailable){
blockSetRemoteUnit = false;
timer = new Timer(10000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
//extracted_values is a Vector<Stirng>
if(typeOfConnection.equals("ABC") || typeOfConnection.equals("DEF"))
extracted_values = new E_ConnectionABC_DEF(resourceSelected, credential).result;
else{
try {
extracted_values = new E_ConnectionOther(resourceSelected, credential).result;
} catch (InterruptedException e1) { e1.printStackTrace(); }
}
int size = extracted_values.elementAt(0).size();
String stringOfNameLabel = "";
String stringValueOfResource = "";
JLabel nameResource = null;
JProgressBar progressBar = null;
subPanelOfinfoServer.removeAll();
subPanelOfinfoServer.repaint();
subPanelOfinfoServer.revalidate();
if(!blockSetRemoteUnit)
comboBox.addItem(" ");
for(int i=0;i<size;i++){
stringOfNameLabel = extracted_values.elementAt(1).elementAt(i);
//Here I popolate the fields of JComboBox, and how you can see,
//I can Change the fields, every time that I selected a different Row
//of the JTable.
if(!blockSetRemoteUnit && stringOfNameLabel.length()==2 && stringOfNameLabel.contains(":"))
comboBox.addItem(stringOfNameLabel);
nameResource = new JLabel(stringOfNameLabel);
nameResource.setHorizontalAlignment(SwingConstants.CENTER);
stringValueOfResource = extracted_values.elementAt(0).elementAt(i);
progressBar = new JProgressBar();
progressBar.setValue(Integer.parseInt(stringValueOfResource));

subPanelOfinfoServer.add(nameResource);
subPanelOfinfoServer.add(progressBar);
}
blockSetRemoteUnit = true;
}
});
timer.setRepeats(true);
timer.setCoalesce(true);
timer.setInitialDelay(0);
timer.start();
}
else{
logger.warn("[JTABLE-SelectRow]ATTENTION! " + resourceSelected + " is not Available");
JOptionPane.showMessageDialog(null, "IP " + resourceSelected + " is not Available");
}
}});
}

现在,在其他 Java 方法中(调用之后),您会找到以下代码:

private JPanel connectResource(){
JPanel remoteResource = new JPanel();
JPanel panelSouth = new JPanel();
//jstMpd is a Vector<Vector<String>>
jstMpd = getAlreadyMapped();
remoteResource.setLayout(new BorderLayout());

final JButton ResourceConnect = new JButton();

comboBox.addActionListener (new ActionListener () {
public void actionPerformed(ActionEvent e) {
Object lettereReadComboBox = comboBox.getSelectedItem();
final String letter = lettereReadComboBox.toString();

try {
alreadyMapped = ctrIfalreadyMapped(ResourceSelected, letter, typeOfConnection, jstMpd);
} catch (IOException e1) {
} catch (InterruptedException e1) {
}

if(alreadyMapped)
ResourceConnect.setIcon(new ImageIcon(path + "src/img/Disconnect.png"));
else
ResourceConnect.setIcon(new ImageIcon(path + "src/img/Drive-Network-Connected.png"));

ResourceConnect.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent ae) {
try {
alreadyMapped = ctrIfalreadyMapped(ResourceSelected, letter, typeOfConnection, jstMpd);
} catch (IOException | InterruptedException e1) {
}
if(alreadyMapped){
try {
String localLetterMounted = deleteElement(ResourceSelected, letter, jstMpd);
if(localLetterMounted!=""){
new E_Mapped_Drives().unmountResource(localLetterMounted);
ResourceConnect.setIcon(new ImageIcon(path + "src/img/Drive-Network-Connected.png"));
ResourceConnect.setActionCommand("!alreadyMapped");
}
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "You didn't selected any Letter of Remote Resource!\n Please Re-Try");
}
}
else{
String letterGetted = "";
String letterMounted= "";
if(typeOfConnection.equals("RDP") || typeOfConnection.equals("VNC")){
try {
letterGetted = letter;
letterMounted = new E_Mapped_Drives().mountResourceWindows(ResourceSelected, letterGetted, credential);
jstMpd.addElement(new Vector<String>(Arrays.asList(letterGetted, ResourceSelected,typeOfConnection,letterMounted)));
ResourceConnect.setIcon(new ImageIcon(path + "src/img/Disconnect.png"));
} catch (IOException e) {
logger.warn("[BOTTONE-ResourceCONNECT][IOException] You didn't selected any letter!");
JOptionPane.showMessageDialog(null, "You didn't selected any Letter of Remote Resource!\n Please Re-Try");
}
}
else{
try {
letterGetted = new E_Mapped_Drives().mountResourceLinux(ResourceSelected, credential);//, comboBox.getSelectedItem(), credential);
ResourceConnect.setIcon(new ImageIcon(path + "src/img/Disconnect.png"));
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "You didn't selected any Letter of Remote Resource!\n Please Re-Try");
}
}
}
}
});
}
});

panelSouth.setBackground(getColorBackground());
remoteResource.add(comboBox, BorderLayout.NORTH);
remoteResource.add(ResourceConnect, BorderLayout.CENTER);
remoteResource.add(panelSouth, BorderLayout.SOUTH);
return remoteResource;
}

最佳答案

可能您正在创建操作监听器,然后再添加项目执行 JComboBox,或者在注册操作监听器后以编程方式选择组合框的某些值。将所有项目添加到 JComboBox 后创建操作监听器。

发生这种情况是因为如果组合框没有项目,当您添加项目时,它就会成为选定的值,从而触发操作监听器。

关于java - JCheckBox 自动选择第一个选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34659716/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com