Java GUI: Document Object Model

HTML has a document object model that Javascript can then manipulate / move.

When I create a GUI in Swing, the model seems very different (I don’t know the name of the model), since I create layout managers and insert objects inside them.

My question is: is it possible to somehow manipulate the Java GUis using the DOM?

[For example, I want to be able to remove / add nodes, move children, etc.)

Thanks!

+6
source share
2 answers

For Swing components, it all starts with a JFrame set (you can also have JWindow and JDialog, but usually you have at least one root frame). Most likely, all you care about is the contentPane of this JFrame (but you could also take care of its owners, etc.).

So, from JFrame you can get the content panel as follows:

Container contentPane = frame.getContentPane(); 

From there, you can begin to descend the component tree using:

 Component[] children = contentPane.getComponents(); 

From a child, you can get a parent with:

 Container parent = child.getParent(); 

To add a component to a container:

 container.add(someComponent); container.validate(); 

To remove a component from the container:

 container.remove(someComponent); container.validate(); 

To move a component from one container to another, simply remove it from one and add it to another.

I am not sure if this answers your question. It would be easier if you could post real-life examples of what you are trying to do.

+7
source

I am not sure if this concerns your problems, but there are XML tools Java UI toolkits .

+3
source

Source: https://habr.com/ru/post/915444/


All Articles