limonik
limonik

Reputation: 499

Combobox Get Data User-Friendly Way and Set them into Number

I am implementing the GUI of my project using the Swing library with the Java language. I want to get the interval from the user with a user friendly text like "every 15 minutes" but in storage data I have to save it as a number in seconds. You can check my GUI:

enter image description here.

I know that I can solve this by writing an Item Listener for a combo box with an if else for every possible item, but I want to learn if there is a more practical way to do it? Because I have to use 4 combo boxes in my program.

Thank you.

Upvotes: 1

Views: 66

Answers (1)

Freek de Bruijn
Freek de Bruijn

Reputation: 3622

You can use a custom class for the items in the JComboBox. From The Java Tutorials - How to Use Combo Boxes:

"The default renderer knows how to render strings and icons. If you put other objects in a combo box, the default renderer calls the toString method to provide a string to display."

If you use a custom class that implements the toString method, you can add more data (like the duration of an interval in seconds) to the items. For example (using Java 8):

// Class ComboBoxTextAndNumber:

import java.awt.event.ItemEvent;
import java.util.*;
import java.util.Vector;
import javax.swing.*;

public class ComboBoxTextAndNumber {
    public static void main(final String[] arguments) {
        SwingUtilities.invokeLater(
                () -> new ComboBoxTextAndNumber().createAndShowGui()
        );
    }

    private void createAndShowGui() {
        final JFrame frame = new JFrame("Stack Overflow");
        frame.setBounds(100, 100, 800, 600);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        final Vector<Interval> intervals = new Vector<>(Arrays.asList(
                new Interval("every 5 years", 5 * 365 * 24 * 60 * 60),
                new Interval("every day", 24 * 60 * 60))
        );

        final JPanel panel = new JPanel();
        final JComboBox<Interval> comboBox = new JComboBox<>(intervals);
        panel.add(comboBox);
        frame.getContentPane().add(panel);

        comboBox.addItemListener(
                itemEvent -> {
                    if (itemEvent.getStateChange() == ItemEvent.SELECTED) {
                        final Interval interval = (Interval) comboBox.getSelectedItem();
                        System.out.println(interval.getSeconds());
                    }
                }
        );

        frame.setVisible(true);
    }
}


// Class Interval:

public class Interval {
    private final String description;
    private final long seconds;

    public Interval(final String description, final long seconds) {
        this.description = description;
        this.seconds = seconds;
    }

    public String getDescription() {
        return description;
    }

    public long getSeconds() {
        return seconds;
    }

    @Override
    public String toString() {
        return getDescription();
    }
}

Upvotes: 1

Related Questions