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.
Asked
Active
Viewed 36 times
0

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

user2028015
- 1
- 3
-
1Just add them in the order you want them displayed. If you want more control, you could use a `GridBagLayout` – MadProgrammer Mar 20 '17 at 07:22
-
cant add them in the order I wanted to (because of some limitations). – user2028015 Mar 20 '17 at 07:29
-
Then use a layout manager which allows you to control the positioning, like GridBagLayout – MadProgrammer Mar 20 '17 at 07:32
-
GridBagLayout is used for view positioning in a screen right ? My use case is, I have list of check boxes in a panel, and they will be dynamically enabled/disabled during the runtime. I want all these check boxes to be sorted based on their status dynamically (enabled first and then disabled). – user2028015 Mar 20 '17 at 07:55
-
2Then, you'll probably want a custom layout manager ... and I'd hate to be your end user :P – MadProgrammer Mar 20 '17 at 07:55
-
Do you mean a _model_ for the data that controls the display in the view? – trashgod Mar 20 '17 at 09:26
-
@trashgod : yes, exactly – user2028015 Mar 20 '17 at 09:58
1 Answers
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.