简体   繁体   中英

Make JFrame read-only

Is there any way to convert a JFrame to read-only, similarly to Collections.unmodifiableList() ?

I need to pass it as parameter to many classes, but I don't want them to close the window or do anything but consulting. Also I would need some custom methods to be available (because I'm extending JFrame).

(I don't mean disable all components, I mean you can't write on the object if you have a reference to it).

No, there is no way to do that. The proper solution is not to extend JFrame at all. In general, you should never extend component classes to do things you can already do just by calling their public methods; routine usage of a class like JFrame does not constitute a new type of JFrame, any more than putting different people in a Volkswagen qualifies it as a new type of car.

Instead, you should wrap the JFrame in a class which extends nothing , and hide the JFrame in it. Then you can expose only the methods you want to expose:

public class MyAppWindow {
    private final JFrame frame;

    private final JComponent frameContents;

    public MyAppWindow() {
        this.frame = new JFrame("My App");
        this.frameContents = new JPanel();
        // Set up frameContents here...
    }

    public void show() {
        frame.setVisible(true);
    }

    public void hide() {
        frame.setVisible(false);
    }

    public void addField(Component field,
                         String labelText) {

        JLabel label = new JLabel(labelText);
        label.setLabelFor(field);

        // Create layout constraints for label here...

        frameContents.add(label, constraints);

        // Create layout constraints for field here...

        frameContents.add(field, constraints);
    }
}

Technically, a class with no extends clause will inherit from Object.

Possible solutions:

  1. Make the components it holds "read-only", one way by making them all individually non-focusable.
  2. I suppose you could recursively iterate through the container/component tree making all non-focusable
  3. Cover the JFrame with a glasspane that blocks user input.

As a side recommendation:

Also I would need some custom methods to be available (because I'm extending JFrame).

You may be painting yourself in a corner by having your class extend JFrame, forcing you to create and display JFrames, when often more flexibility is called for. In fact, I would venture that most of the Swing GUI code that I've created and that I've seen does not extend JFrame, and in fact it is rare that you'll ever want to do this. More commonly your GUI classes will be geared towards creating JPanels, which can then be placed into JFrames or JDialogs, or JTabbedPanes, or swapped via CardLayouts, wherever needed. This will greatly increase the flexibility of your GUI coding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM