I have this code which saves the value into database from JSpinner

SpinnerListModel monthModel = new SpinnerListModel();
String[] monthStrings = {"1Month","3Month","6Month","1Year"}; //get month names
             monthModel = new SpinnerListModel(monthStrings);
            JSpinner spinner = new JSpinner(monthModel);
statement.setString(28,(String)monthModel.getValue().toString().toUpperCase());

But how to get that value back for Editing?

I tried

spinner.setText((String)monthModel.getValue(),29);

But it says

The method setText(String, int) is undefined for the type JSpinner

How do I get the values from Database?again to JSpinner.

Dani AI

Generated

Short answer: use the spinner's model and call setValue(...) with an object that actually exists in that model. is right that JSpinner#setValue is the right API — but you must pass the exact model entry (or a matching object), not an arbitrary String, otherwise the model will reject it.

A typical pattern when loading from the DB is:

  • read the stored value (String or an index) from the ResultSet,
  • find the matching element in the SpinnerListModel (or use the saved index),
  • call spinner.setValue(matchingObject).

Example (adapt to your column names and variable names):

String saved = rs.getString("duration"); // or rs.getString(28)

SpinnerListModel model = (SpinnerListModel) mySpinner.getModel();
Object match = null;
for (Object item : (List<?>) model.getList()) {
    if (item != null && item.toString().equalsIgnoreCase(saved)) {
        match = item;
        break;
    }
}
if (match != null) {
    mySpinner.setValue(match);
} else {
    // fallback: pick a safe default or handle missing value
    mySpinner.setValue(((List<?>) model.getList()).get(0));
}

Notes and troubleshooting:

  • If you stored an uppercased label in the DB (as your save code did), normalize before matching or use equalsIgnoreCase. SpinnerListModel matches by object equality, so formatting/case must match one of the model items.
  • Storing the list index (int) in the DB is simpler: read the index and do mySpinner.setValue(list.get(index)).
  • Avoid trying to set the editor text directly. If you must edit the text field use the editor (((JSpinner.DefaultEditor)spinner.getEditor()).getTextField().setText(...)) but then call spinner.commitEdit() to push it into the model — still, calling setValue is the cleaner approach.
  • Wrap setValue in try/catch for IllegalArgumentException to detect when a DB value no longer exists in the model.

For your case, , either match the saved uppercase string against the model entries (normalize case), or store an index/value that’s easy to reselect when loading for editing.

I am not very familiar with Jspinners, but here is what found during a quick browse of the API :
http://docs.oracle.com/javase/7/docs/api/javax/swing/JSpinner.html#setValue(java.lang.Object)

All in all I suggest you try the setValue(), on your spinner.

Another suggesetion would be to evalute the IDE that you are using. Some IDEs support code completion which shows you what methods are available to an object.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.