java enums ordering

匿名 (未验证) 提交于 2019-12-03 00:46:02

问题:

Im using java enums to define how to render a modal window with buttons (Vaadin handles the rendering). My problem is that when I run the gui my buttons comes in a randomized order each time. So my question is this, since i use a enum set to hold my buttons, will that be unordered? whats the best way to make it into a ordered list?

My settings enum

public enum MODAL_SETTINGS {     NEW_MODAL_WINDOW("menu.context.new", "400", MODAL_BUTTON.SAVE, MODAL_BUTTON.CANCEL),     EDIT_MODAL_WINDOW("menu.context.modify","400", MODAL_BUTTON.UPDATE, MODAL_BUTTON.CANCEL),     DELETE_MODAL_WINDOW("menu.context.delete", "250", false, MODAL_BUTTON.DELETE, MODAL_BUTTON.CANCEL);      private EnumSet<MODAL_BUTTON> buttons;     private String caption;     private String width;     private boolean isResizable = true;      private MODAL_SETTINGS(String caption, String width, MODAL_BUTTON... buttons){         this.setCaption(caption);         this.setWidth(width);         this.buttons = EnumSet.copyOf(Arrays.asList(buttons));     }      private MODAL_SETTINGS(String caption, String width, boolean isResizable, MODAL_BUTTON... buttons){         this.setCaption(caption);         this.setWidth(width);         this.isResizable = isResizable;         this.buttons = EnumSet.copyOf(Arrays.asList(buttons));     }      public EnumSet<MODAL_BUTTON> getButtons(){         return buttons;     }      @Override     public String toString(){         String s = super.toString();         s=s.replaceAll("_", ".");         return s;     }      public void setCaption(String caption) {         this.caption = caption;     }      public String getCaption() {         return caption;     }      public void setWidth(String width) {         this.width = width;     }      public String getWidth() {         return width;     }      public boolean isResizable() {         return isResizable;     } } 

My buttons enum

public enum MODAL_BUTTON {     SAVE, UPDATE, CANCEL, DELETE; } 

回答1:

Use Enum.values() instead of an EnumSet:

Note that each enum type has a static values method that returns an array containing all of the values of the enum type in the order they are declared. This method is commonly used in combination with the for-each loop to iterate over the values of an enumerated type.

Source: Enums in the Java 1.5 documentation



回答2:

According to the documentation for Enumset, the iterator should return the Enum constants in the order in which they were declared.

The iterator returned by the iterator method traverses the elements in their natural order (the order in which the enum constants are declared). The returned iterator is weakly consistent: it will never throw ConcurrentModificationException and it may or may not show the effects of any modifications to the set that occur while the iteration is in progress.

Although it can be something about with your UI thread accessing the Enums in different orders.



回答3:

From EnumSet documentation:

The iterator returned by the iteratormethod traverses the elements in their natural order (the order in which the enum constants are declared).

So, your problem is somewhere else.



回答4:

You must have something in the place where you are actually assiging the buttons, which changes their order. Try to give us a SSCCE.

Check out the sample code below where each time you run it the order of buttons will be the same for all rows.

import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities;  public class EnumeOrderButtonsTest {     public static void main(String[] args)     {         SwingUtilities.invokeLater(new Runnable()         {             @Override             public void run()             {                 JPanel p = new JPanel(new GridLayout(0, MODAL_BUTTON.values().length));                 int noOfRows = 10;                 for(int i = 0; i < noOfRows; i++)                     for(MODAL_BUTTON mb : MODAL_BUTTON.values())                         p.add(new JButton(mb.name()));                  JFrame f = new JFrame();                 f.setContentPane(p);                 f.setSize(800, 600);                 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                 f.setVisible(true);             }         });     }      public enum MODAL_BUTTON     {         SAVE, UPDATE, CANCEL, DELETE;     } } 


文章来源: java enums ordering
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!