1

I have a JComboBox which is has values No & Yes. This is how I store it into database:

jdto.setPlacement("Yes".equals(comboPlace.getSelectedItem()));

then pass the jdto into my create method. Selecting yes gives value true. I am struggling to work in reverse logic now.

I want to be able to automatically fill combobox with Yes or No depending on true/false value from database.

atm I have the following

fieldPlace.setSelectedItem(jdto.getPlacement());

I tried if else statement with string but was unable to do so.

can someone demonstrate how to achieve this.

this is getPlacement()

public Boolean getPlacement() {
        return placement;
    }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
MooHa
  • 819
  • 3
  • 11
  • 23

3 Answers3

2
  • add Item to the DefaultComboBoxModel, can be intialized automatically from proper array implemented in JComboBox API

  • you can to use setSelectedItem or to hardcode setSelectedIndex

for example

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();
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
2

Store the booleans in the model, not their string representation - custom rendering of the items is the task of a ... custom renderer.

 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....);   
     }
 }
kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • ok, but the problem is that it is always coming back from sqlite as false despite it is being true. mine getter and setter methods are boolean and not string – MooHa Feb 28 '13 at 14:06
1

It looks like your JComboBox model contains instances of String, so write an auxiliary method that translates:

public String getPlacementString() {
    if (getPlacement()) {
        return "Yes";
    } else {
        return "No";
    }
}

Then use that method to setSelectedItem():

fieldPlace.setSelectedItem(jdto.getPlacementString());
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Hi, I am saving it like 'jdto.setPlacement("Yes".equals(fieldPlace.getSelectedItem()));' and it sets true if i click yes, but on the retrieval side its always coming as false even if it is true – MooHa Feb 28 '13 at 12:03
  • Are you committing the transaction? Does the database show the update? – trashgod Feb 28 '13 at 12:15
  • slightly disappointed ... mapping of items to a visual representation is the task of a renderer, not some auxiliary method (except when the latter is used by the custom renderer :) – kleopatra Feb 28 '13 at 12:19
  • @kleopatra: Absolutely right; this is a hack/patch. +1 to your (better) approach; an `enum` makes a good combo model entry. for [example](http://stackoverflow.com/a/8260663/230513). – trashgod Feb 28 '13 at 12:21