Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java GUI: Document Object Model

HTML has a Document Object Model, which Javascript can then manipulate / move around.

When I create GUIs in Swing -- the model appears to be very difference (I don't know the name of the model), since I'm creating layout managers, and sticking objects inside of them.

My question: is there someway to manipulate Java GUis in a DOM like manner?

[For example, I want to be able to delete / add nodes, move childs around, etc ...]

Thanks!

like image 956
user1383359 Avatar asked May 10 '12 21:05

user1383359


People also ask

Which is better JavaFX or Swing?

In short, Swing and JavaFX are both GUI toolkits for Java programs. Swing is the old standard toolkit that features a bigger library of GUI elements and mature IDE support. JavaFX is the newer standard with a smaller library, more consistent updates, and consistent MVC support.

What is Java document object?

Document Object Model is a commendation of the World Wide Web Consortium. It explains an interface that enables programs to access and modify the style, structure, and contents of XML documents. XML parsers that support DOM implement this interface.

Is there a GUI for Java?

In Java applications, the components that comprise a GUI (Graphical User Interface) are stored in containers called forms. The Java language provides a set of user interface components from which GUI forms can be built.

What is DOM in UI?

What is the DOM? The Document Object Model (DOM) is a programming interface for web documents. It represents the page so that programs can change the document structure, style, and content. The DOM represents the document as nodes and objects; that way, programming languages can interact with the page.


2 Answers

For Swing components, everything starts from a set of JFrame's (you can also have JWindow's and JDialog's, but you usually have at least one root frame). Most likely, all you care about is the contentPane of that JFrame (but you could care also about its ownedWindows, etc...).

So from the JFrame, you can get the content pane as following:

Container contentPane = frame.getContentPane();

From there, you can start going down the Tree of components, using:

Component[] children = contentPane.getComponents();

From a child, you can get its parent with:

Container parent = child.getParent();

To add a component to a container:

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

To remove a component from a container:

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

To move a component from one Container to another, simply remove it from one and add it to the other.

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

like image 61
Guillaume Polet Avatar answered Nov 06 '22 04:11

Guillaume Polet


I'm not sure this addresses your concerns but there are XML-driven Java UI toolkits.

like image 43
ykaganovich Avatar answered Nov 06 '22 06:11

ykaganovich