Skip to content
Advertisement

Is there a way to hide all elements in JFrame

I’m currently working on Java application and I’m using JFrame and inside of JFrame i have some buttons, labels and textfields. I’m in need of hiding all the elements that are in JFrame. I don’t need to hide JFrame itself, only the elements in it so I can display some others. Is there a way to hide them all without manually writing element.setVisibility(false); for every single one of them?

JavaScript

Here is my code. I’m changing the layout of a UI window and need to make it more efficient so I don’t write the same code hundreds of times.

Advertisement

Answer

Add the components you want to control to a common container, then simply hide/show it

JavaScript

I think this will not do the job …

Actually, I think it will, but, you’re missing a number very important, core concepts.

Decouple

You’re operating in a OO language, one of things you’re going to want to focus on is “decoupling” the dependencies your functionality has to other functionality. Ask yourself the simple question. If I had to modify this one part of the system, what ramifications will it have. If you find yourself in a cascade of changes, then your system is to tightly coupled

Single responsibility

You want to isolate functionality, as much as possible, so that it has one single job to do. Rather then using a “god” class (like you have), which tries to do everything, break down your problem into small chunks an isolate the functionality, so that it’s doing as simple a job as it possibly can.

As much as possible, each element shouldn’t be reliant on knowing the current state of the system, beyond what it’s been told, which is part of the first and third points.

Dependency injection

Pass the information that each sub system needs to them, rather than having globally reachable resources. This is really good for testing, but it further reduces the coupling

Code to interface

Make use of interface to describe high level concepts of functionality. For example, the “student details” view doesn’t care about how the student information is structured, obtained or managed, it only cares about presenting some information maintained by the interface (or contract). This will make it easier to make core changes to the underlying implementation without adversely effecting all those parts of the system which want to use it, as they are only relying on the “contract” defined by the interface

Example…

Now, typically, I use a CardLayout, but a CardLayout isn’t as dynamic as something like this problem might need.

So, instead, I rolled my own “navigation” workflow. This allows me more control over the information which needs to be injected and allows for a more dynamic presentation – you don’t want to present the student login screen filled in with the student credentials when a student logs out.

JavaScript

This is an incomplete implementation, only intended to provide a jumping off point.

I strongly encourage you to have look at Laying Out Components Within a Container

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement