Block entire swing ui except for one component - “dialog style”

房东的猫 提交于 2019-12-02 06:58:48

问题


[This question is in relation to this question]

Setting: I have this home-crafted "editable label" component which looks like a label, but when you click it, it turns into an editable field, allowing you to edit it. You can hit Esc to cancel, or Shift-Enter to OK your edits - or click the respective buttons beneath the editable field.

The challenge: When you go into this edit-mode, I want the rest of the UI to be completely blocked: You shall either cancel or OK the edit before being able to do anything else. While writing this, I realize the obvious: This is exactly how a dialog operates - but I like my "in-document" edit-label better than opening a dialog.


回答1:


Glass pane might be the way to go. You can easily steal all the events and send some to your custom object. Here is an article that discusses a way to implement A wellbehaved glasspane.




回答2:


This is rough. There is no method in Swing to block all UI except for one component. This is something you will have to build out yourself. Here is the design approach I would use:

  • Build a EditableLabelListener interface defining the method editableStateChanged(EditableLabelEvent)

  • Create a EditableLabelEvent class which extends AWTEvent and adds an editableState property and an isEditable() boolean value.

  • Add methods to your custom component for addEditableLabelListener and removeEditableLabelListener

  • On each panel where you use your component, have your controller class (which may be your panel, depending on your design) implement EditableEventListener and register itself as a listener to the component.

  • When the event is fired, the controller class should check the isEditable method on the event, and enable or disable all other components on the panel as appropriate. When there are multiple editable label components on the same form, you can use the getSource method on the event to determine which one is being used so you can disable the others.




回答3:


In you editable label, when you start editing, use SwingUtilities.getRoot() to get the root of your label, cast it to Container. On the Container you can call getComponents(). Iterate through this array and call setEnabled(false) unless it is the label. Enable them when you're done editing.

One question: why do you need it? If you need dialog-like behavior, use JOptionPane.

BTW, if you remove border from JTextField and setOpaque(false), it will be just as an editable label.




回答4:


You can extend a JDialog, then when you init it,

this.setAlwaysOnTop(true);
this.setModalityType(ModalityType.APPLICATION_MODAL);
this.setVisible(false);
this.setVisible(true);

It will only allow the JDialog to be interacted with and block everything else in the application.



来源:https://stackoverflow.com/questions/3599994/block-entire-swing-ui-except-for-one-component-dialog-style

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