Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JCombobox基于真/假调整文本_Java_Swing_Jdbc_Boolean_Jcombobox - Fatal编程技术网

Java JCombobox基于真/假调整文本

Java JCombobox基于真/假调整文本,java,swing,jdbc,boolean,jcombobox,Java,Swing,Jdbc,Boolean,Jcombobox,我有一个JComboBox,它有值No&Yes。以下是我将其存储到数据库中的方式: jdto.setPlacement("Yes".equals(comboPlace.getSelectedItem())); 然后将jdto传递到我的create方法中。选择“是”将使值为true。我现在正努力在逆向逻辑中工作 我希望能够根据数据库中的真/假值,自动将组合框填充为“是”或“否” 我有以下几点 fieldPlace.setSelectedItem(jdto.getPlacement()); 我尝

我有一个
JComboBox
,它有值
No
&
Yes
。以下是我将其存储到数据库中的方式:

jdto.setPlacement("Yes".equals(comboPlace.getSelectedItem()));
然后将
jdto
传递到我的create方法中。选择“是”将使值为true。我现在正努力在逆向逻辑中工作

我希望能够根据数据库中的真/假值,自动将组合框填充为“是”或“否”

我有以下几点

fieldPlace.setSelectedItem(jdto.getPlacement());
我尝试使用带有字符串的if-else语句,但无法执行

有人能演示如何实现这一点吗

这是getPlacement()


您的
JComboBox
模型似乎包含
String
的实例,因此请编写一个辅助方法来翻译:

public String getPlacementString() {
    if (getPlacement()) {
        return "Yes";
    } else {
        return "No";
    }
}
然后使用该方法设置selecteditem():


您的
JComboBox
模型似乎包含
String
的实例,因此请编写一个辅助方法来翻译:

public String getPlacementString() {
    if (getPlacement()) {
        return "Yes";
    } else {
        return "No";
    }
}
然后使用该方法设置selecteditem():

  • 将项添加到DefaultComboxModel,可以从JComboxAPI中实现的适当数组自动初始化

  • 您可以使用setSelectedItem或对setSelectedIndex进行硬编码

比如说

import java.awt.event.ActionEvent;
import java.util.Vector;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class ComboBoxBooleanModel {

    private javax.swing.Timer timer = null;
    private Vector<Boolean> comboBoxItems;
    private JComboBox box;

    public ComboBoxBooleanModel() {
        comboBoxItems = new Vector<Boolean>();
        comboBoxItems.add(Boolean.TRUE);
        comboBoxItems.add(Boolean.FALSE);
        box = new JComboBox(comboBoxItems);
        JFrame frame = new JFrame("");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(box);
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                box.setSelectedIndex(1);
            }
        });
        start();
    }

    private void start() {
        timer = new javax.swing.Timer(1250, updateCol());
        timer.start();
    }

    public Action updateCol() {
        return new AbstractAction("text load action") {
            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                if (box.getSelectedItem() == (Boolean) false) {
                    box.setSelectedItem((Boolean) true);
                } else {
                    box.setSelectedItem((Boolean) false);
                }
            }
        };
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                ComboBoxBooleanModel comboBoxModel = new ComboBoxBooleanModel();
            }
        });
    }
}
导入java.awt.event.ActionEvent;
导入java.util.Vector;
导入javax.swing.AbstractAction;
导入javax.swing.Action;
导入javax.swing.JComboBox;
导入javax.swing.JFrame;
导入javax.swing.SwingUtilities;
公共类ComboxBoolean模型{
private javax.swing.Timer=null;
私有向量项;
私人JComboBox;
公共ComboxBooleanModel(){
comboBoxItems=新向量();
comboBoxItems.add(Boolean.TRUE);
comboBoxItems.add(Boolean.FALSE);
box=新的JComboBox(comboBoxItems);
JFrame=新JFrame(“”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
框架。添加(框);
frame.setLocationRelativeTo(空);
frame.pack();
frame.setVisible(true);
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
框。设置所选索引(1);
}
});
start();
}
私有void start(){
timer=newjavax.swing.timer(1250,updateCol());
timer.start();
}
公共行动更新{
返回新的AbstractAction(“文本加载操作”){
私有静态最终长serialVersionUID=1L;
@凌驾
已执行的公共无效操作(操作事件e){
if(box.getSelectedItem()=(布尔值)false){
box.setSelectedItem((布尔值)true);
}否则{
box.setSelectedItem((布尔值)false);
}
}
};
}
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
ComboBoxBooleanModel comboBoxModel=新ComboBoxBooleanModel();
}
});
}
}
  • 将项添加到DefaultComboxModel,可以从JComboxAPI中实现的适当数组自动初始化

  • 您可以使用setSelectedItem或对setSelectedIndex进行硬编码

比如说

import java.awt.event.ActionEvent;
import java.util.Vector;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class ComboBoxBooleanModel {

    private javax.swing.Timer timer = null;
    private Vector<Boolean> comboBoxItems;
    private JComboBox box;

    public ComboBoxBooleanModel() {
        comboBoxItems = new Vector<Boolean>();
        comboBoxItems.add(Boolean.TRUE);
        comboBoxItems.add(Boolean.FALSE);
        box = new JComboBox(comboBoxItems);
        JFrame frame = new JFrame("");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(box);
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                box.setSelectedIndex(1);
            }
        });
        start();
    }

    private void start() {
        timer = new javax.swing.Timer(1250, updateCol());
        timer.start();
    }

    public Action updateCol() {
        return new AbstractAction("text load action") {
            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                if (box.getSelectedItem() == (Boolean) false) {
                    box.setSelectedItem((Boolean) true);
                } else {
                    box.setSelectedItem((Boolean) false);
                }
            }
        };
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                ComboBoxBooleanModel comboBoxModel = new ComboBoxBooleanModel();
            }
        });
    }
}
导入java.awt.event.ActionEvent;
导入java.util.Vector;
导入javax.swing.AbstractAction;
导入javax.swing.Action;
导入javax.swing.JComboBox;
导入javax.swing.JFrame;
导入javax.swing.SwingUtilities;
公共类ComboxBoolean模型{
private javax.swing.Timer=null;
私有向量项;
私人JComboBox;
公共ComboxBooleanModel(){
comboBoxItems=新向量();
comboBoxItems.add(Boolean.TRUE);
comboBoxItems.add(Boolean.FALSE);
box=新的JComboBox(comboBoxItems);
JFrame=新JFrame(“”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
框架。添加(框);
frame.setLocationRelativeTo(空);
frame.pack();
frame.setVisible(true);
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
框。设置所选索引(1);
}
});
start();
}
私有void start(){
timer=newjavax.swing.timer(1250,updateCol());
timer.start();
}
公共行动更新{
返回新的AbstractAction(“文本加载操作”){
私有静态最终长serialVersionUID=1L;
@凌驾
已执行的公共无效操作(操作事件e){
if(box.getSelectedItem()=(布尔值)false){
box.setSelectedItem((布尔值)true);
}否则{
box.setSelectedItem((布尔值)false);
}
}
};
}
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
ComboBoxBooleanModel comboBoxModel=新ComboBoxBooleanModel();
}
});
}
}

将布尔值存储在模型中,而不是它们的字符串表示形式-项目的自定义呈现是。。。自定义渲染器

 public class BooleanListCellRenderer extends DefaultListCellRenderer {

     public Component getListCellRendererComponent( ... Object value, ...) {
          if (Boolean.TRUE.equals(value) { 
             value = "Yes";
          } else if (Boolean.FALSE.equals(value)) {
             value = "No";
          }
          return super.getListCellRendererComponent(... value....);   
     }
 }

将布尔值存储在模型中,而不是它们的字符串表示形式-项目的自定义呈现是。。。自定义渲染器

 public class BooleanListCellRenderer extends DefaultListCellRenderer {

     public Component getListCellRendererComponent( ... Object value, ...) {
          if (Boolean.TRUE.equals(value) { 
             value = "Yes";
          } else if (Boolean.FALSE.equals(value)) {
             value = "No";
          }
          return super.getListCellRendererComponent(... value....);   
     }
 }

您好,我将其保存为'jdto.setPlacement(“Yes.equals(fieldPlace.getSelectedItem());'如果我单击yes,它将设置为true,但在检索端,它总是显示为false,即使它为true,您是否正在提交事务?数据库显示更新了吗?有点失望。。。将项目映射到可视化表示是渲染器的任务,而不是一些辅助方法(除非自定义渲染器使用后者:)@kleopatra:绝对正确;这是一个黑客