Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java上按字典顺序读取类属性(成员)?_Java_Lexicographic_Propertydescriptor - Fatal编程技术网

如何在java上按字典顺序读取类属性(成员)?

如何在java上按字典顺序读取类属性(成员)?,java,lexicographic,propertydescriptor,Java,Lexicographic,Propertydescriptor,我正在尝试使用swt构建输入对话框。 我想阅读我的类属性,并按照指定的顺序甚至字典顺序创建对话框窗口。我将在linkedhahmap\treemap中保留我的类属性 例如: public class MazeProperties { /** Maze Name. */ private String MazeName; /** Number of rows in maze. */ private int x; /** The Floors. */

我正在尝试使用swt构建输入对话框。 我想阅读我的类属性,并按照指定的顺序甚至字典顺序创建对话框窗口。我将在linkedhahmap\treemap中保留我的类属性

例如:

public class MazeProperties {


    /** Maze Name. */
    private String MazeName;


    /** Number of rows in maze. */
    private int x;

    /** The Floors. */
    private int y;

    /** Number of columns in maze. */
    private int z;

    public MazeProperties(String mazeName, int rows, int floors, int columns) {
        MazeName = mazeName;
        Rows = rows;
        Floors = floors;
        Columns = columns;

}

如果要以确定性、可预测的顺序将键映射到值,则应将它们存储在
树映射中,注意键类实现
比较器。如果没有,则必须实现自己的
比较器
,并将其作为参数传递给
树映射
构造函数


在您的情况下,如果希望
PropertyDescriptor
作为键,则必须实现自己的
Comparator
来比较
PropertyDescriptor
对象。

代码中的双
**
语法有什么作用?我看起来不像是标准的。这是我的javadoc插件的错误,不使用**来处理它,它只是普通的成员。顺便说一句,这是java而不是javascript。请编辑您的帖子以更正此问题
public class ClassInputDialog extends Dialog{

    /** The shell. */
    Shell shell;

    /**     the generic class. */
    private Class<?> template;

    /**     Property Descriptors give me the ability to see what properties the class contains - and has generic functionalities for setters and getters for fields!. */
    PropertyDescriptor[] descs;
    /** I wanna have a robust connection between a property to a text box - that way upon pressing OK I could know what class property was written in it.
     * 
     */
    HashMap<PropertyDescriptor,Text> txtMap=new LinkedHashMap<PropertyDescriptor,Text>();
    //TreeMap<String,Text> txtMapOrderName=new TreeMap<String,Text>();
    //Map<PropertyDescriptor, Text> txtMap = Collections.synchronizedMap(new LinkedHashMap<PropertyDescriptor, Text>());
    /** The bonus demanded that this dialog will support all given classes
     *  but what happens when a class has an enum? a whole new story with combo-boxes and once again I wanna have a connection between the class field enum to the String that was selected in the form.
     * 
     */
    HashMap<PropertyDescriptor,String> enumMap=new   **HashMap<PropertyDescriptor,String>();**

      /**   This is the reference of the instance I will return.
     * 
     */
    private Object input;

      /**   Ct'r for people that don't know a thing about SWT.
     * @param parent - Shell
     * @param template - The Class to create form to
     */
    public ClassInputDialog(Shell parent,Class<?> template) {
            this(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL,template);
          }

      /**
     *  Ct'r with SWT style.
     *
     * @param parent - Shell
     * @param style - SWT style
     * @param template - The Class to create form to
     */
    public ClassInputDialog(Shell parent, int style,Class<?> template) {
        super(parent, style);
        this.template=template;
        descs=PropertyUtils.getPropertyDescriptors(template);
        setText("Set Properties");
      }


      /**
     * Gets the input.
     *
     * @return the input
     */
    public Object getInput() {
        return input;
      }

      /**
     * Sets the input.
     *
     * @param input the new input
     */
    public void setInput(Object input) {
        this.input = input;
      }

      /**   Here the window layout is set and the main loop event happens. When window closes User's input is returned.
     * @return The user's input
     */
    public Object open() {
        this.shell = new Shell(getParent(), getStyle());
        shell.setText(getText());
        createContents(shell);
        shell.pack();
        shell.open();
        Display display = getParent().getDisplay();
        while (!shell.isDisposed()) {
          if (!display.readAndDispatch()) {
            display.sleep();
          }
        }
        //display.dispose();
        return input;
      }

      /**   Creates Window content and layout - sets Labels, Text boxes and combo boxes nicely.
     * @param shell - window's parent
     */
    private void createContents(final Shell shell) {
        shell.setLayout(new GridLayout(2, true));
        for(**PropertyDescriptor propDesc: descs**)
            if(!propDesc.getName().equals("class"))
            {
                if(!propDesc.getPropertyType().isEnum())
                {
                    Label label = new Label(shell, SWT.NONE);
                    label.setText(propDesc.getName());
                    GridData data = new GridData();
                    data.horizontalSpan = 2;
                    label.setLayoutData(data);

                    final Text text = new Text(shell, SWT.BORDER);
                    data = new GridData(GridData.FILL_HORIZONTAL);
                    data.horizontalSpan = 2;
                    text.setLayoutData(data);
                    txtMap.put(propDesc, text);
                   // txtMapOrderName.put(propDesc.getDisplayName(), text);


                    System.out.println(propDesc.getDisplayName());
                }
                else
                {
                    Label label = new Label(shell, SWT.NONE);
                    label.setText(propDesc.getName());
                    GridData data = new GridData();
                    data.horizontalSpan = 2;
                    label.setLayoutData(data);

                    final Combo combo = new Combo(shell, SWT.DROP_DOWN);

                    String[] toCombo=new String[propDesc.getPropertyType().getEnumConstants().length];
                    for(int i=0;i<propDesc.getPropertyType().getEnumConstants().length;i++)
                        toCombo[i]=propDesc.getPropertyType().getEnumConstants()[i].toString();
                    combo.setItems(toCombo);
                    data = new GridData(GridData.FILL_HORIZONTAL);
                    data.horizontalSpan = 2;
                    combo.setLayoutData(data);
                    combo.addSelectionListener(new SelectionListener() {

                        @Override
                        public void widgetSelected(SelectionEvent arg0) {
                            enumMap.put(propDesc, combo.getText());
                        }

                        @Override
                        public void widgetDefaultSelected(SelectionEvent arg0) {


                        }
                    });

                }

            }
y
mazeName
x
z