0

I have a JPanel containing a list of JCheckBox fields, and I want them to be displayed in a specific order. I was told that a view model can be created to sort those check boxes. I am very new to Swing and don't know how to proceed further. I couldn't exactly find any source in internet. Can someone help me to figure out a way for implementing the above. Thanks in advance.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045

1 Answers1

1

It looks like you want to create a model for the data that controls the display in the view. One approach would be to create a suitable TableModel for the model and use JTable for the view. Your TableCellRenderer can condition the display's color and enabled state; your implementation of Comparable will define the sort order.

In this compete example, class DataModel extends AbstractTableModel to manage a List<Value>, and Value implements Comparable<Value> by forwarding to Double. Your implementation would add an attribute for Enabled and include it in your implementation of the required methods. In outline,

private static class Value implements Comparable<Value> {

    private Boolean selected;
    private Boolean enabled;
    private Double value;

    public Value(Boolean selected, Boolean enabled, Double value) {
        this.selected = selected;
        this.enabled = enabled;
        this.value = value;
    }

    @Override
    public int compareTo(Value v) {…}

    @Override
    public boolean equals(Object v) {…}

    @Override
    public int hashCode() {…}
}

This related example uses an instance of MyObjectManager to manage mutual exclusion among radio button.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045