7

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!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user1383359
  • 2,673
  • 2
  • 25
  • 32
  • You mean like going through the hierarchy of Component's? – Guillaume Polet May 10 '12 at 21:52
  • 2
    You are in for some dire disappointment and frustration. Everything is terribly more difficult in Swing than what you are accustomed to in Javascript. You'll also discover that there's no event capture/bubbling -- it gets dispatched only to the listener attached directly to the event source. – Marko Topolnik May 10 '12 at 21:52
  • *"(HTML) ..don't know the name of the model"* See [`HTMLDocument`](http://docs.oracle.com/javase/7/docs/api/javax/swing/text/html/HTMLDocument.html) – Andrew Thompson May 10 '12 at 21:54
  • isn't this.add() the same as adding a node in javascript? – Hawken May 10 '12 at 21:54
  • @Marko: lol, this is what I thought. About two days ago, I created a basic JFrame, got its Graphics2D environment, and just started drawing stuff to it -- but I was hoping someone else had already solved this problem. :-) – user1383359 May 10 '12 at 21:56
  • @MarkoTopolnik, this is not true about being dispatched only to the listener. You can have the even starting from the window/frame, tracked down as you please. Not that it would help but it's totally possible. – bestsss May 10 '12 at 22:07
  • @bestsss I was kind of hoping for someone to come in and alleviate my words :) So only the bubbling phase is missing. – Marko Topolnik May 10 '12 at 22:08
  • @Marko, you can do that too but i just cant see how it'd be useful. Swing is (mostly) MVC designated, you have full hierarchy to navigate if you want to. For an HTML dev Swing would have some learning curve to climb. When I started I had already experience w/ WinAPI (not32 even), delphi and pure AWT, so swing was sort of easier, yet it still riddled with some odd design choices. – bestsss May 10 '12 at 22:13

2 Answers2

7

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.

Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • I'd suggest ot use revalidate() + repaint(), in all cases, sure there could be discusion for what JComponent required and for why not, just to avoiding that revalidate() + repaint() +1 – mKorbel May 11 '12 at 04:00
  • You can get a listing with `Ctrl+Shift+F1`, like they show [here](http://stackoverflow.com/questions/6671021). – Catalina Island May 11 '12 at 12:23
3

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

ykaganovich
  • 14,736
  • 8
  • 59
  • 96