How to achieve absolute positioning in Javafx (FXML) - java

Ok lets just assume I wanted to have the following layout:
<GridPane>
<Button></Button>
<Button></Button>
<GridPane/>
<HBox>
<Label></Label>
<HBox/>
Now, in HTML I would simply apply position: absolute via css and one of the two containers would detache from the normal document flow allowing it to float around without pushing its siblings aside. I am trying to achieve something similar but im on it for hours already.
According to the Oracle documentation this can be achieved with either StackPane, which I dont want because I want the other container unaffected) or I could use a raw Pane node which somehow acts crazy because well, its a base class. The documentation about the pane also says:
This class may be used directly in cases where absolute positioning of children
and well of children doesnt sound good...as in my case it has to be the direct container.
So my question is: how can I achieve absolute positioning without hacking around with too much math?
Could localToScene() be what I want?
Transforms a bounds from the local coordinate space of this Node into the coordinate space of its scene.

My impression is that you are still thinking in HTML-terms and not JavaFX-Terms. For example in JavaFX there is no "normal document flow". Everything has to be put into some layout container and this container determines how things are layed out and there is nothing wrong with using a Pane directly if you have to. Although I am not sure what your actual use-case is, I think you might want to have a look at the AnchorPane.

Related

GridPane 9x9 with TextField for Sudoku

I'm trying to use a GridPane for a Sudoku 9x9 using FXML.
I'm fairly new in this field so I wanted to ask you guys before proceeding.
I've created a GridPane 9x9 and for now I'm using TextField in each node to display numbers and enabling the user to write a new number themselves.
My question:
Is it okay to create the 81 TextField's in pure java code or is
there a way to do it efficiently using FXML so I don't split my view
(Model-View-Controller) setup up?
Thanks in advance and sorry if my question is unclear, I'm having a hard time explaining it tbh.
Fxml is not the only (and in my opinion not the best way) to use the model view controller pattern in javafx.
The View basically describes how the application / windows / sections look. That can be done with pure fxml, but very often it won't be enough. Especially when you want / need dynamically built content (which in your case you do not really need). So you COULD just add those 9 grid rows and 9 grid columns and add TextFields into them using FXML.
However this seems very ineffective, since you could use loops and would remove a lot of time wasters while programming.
You can do that by either using a mix of fxml and code:
First load the fxml in your view class and then add to it using code.
Or you could use pure code (my preferred way, since i do not like using fxml if I can't do it in fxml only - and since that is often the case for me i just skip it).
The Model represents your data. Changes in that data will change the view. This is done with observers and binds in the view class.
The Controller manipulates data. Most commonly this is done using user input, but could also be achieved using other events.
However: Keep in mind that MVC is only a very loose pattern and does not define a clear way to do it, since it is not tailored to a specific language / framework. While there are tutorials out there that try to teach how to implement the MVC pattern in JavaFX they all interpret MVC differently and as a result the implementation is also vastly different.
I hope my description of how to structure the View in JavaFX using three different approaches helps you. If not please comment and I'll try to elaborate. :)

GWT CSS, Layout and LayoutPanel?

I'm trying to build a slick-looking GWT panel (my first one) and am stuck trying to decide how to handle its layout.
According to the Layout Javadoc:
Helper class for laying out a container element and its children.
And the LayoutPanel Javadoc:
A panel that lays its children out in arbitrary layers using the Layout class.
So it seems like you can:
Extend a ComplexPanel and use a Layout to manage its layout; or
Use a LayoutPanel which seems to have this functionality built-in; or
Just add elements to an HTMLPanel and use CSS to perform all the positioning/layout
I'm not 100% sure, but this feels like you have these options (and possibly others) because they offer benefits over each other in different circumstances, or because the GWT API has changed a lot and newer layout methods have been added over time.
If the former is the case, then security and performance are my only priorities, so I ask: is one method more performant and/or secure than the others? And if the latter is the case, then which method is GWT's newest and most encouraged? It would seem to me that the most control would be to leave everything for the CSS and not use Layout/LayoutPanel at all!
LayoutPanel uses absolute positioning, and it also provides size to its children. I like to use it for my outmost layout container, as it occupies the entire browser window and resizes its children when that browser window is resized.
For inside widgets (e.g. menus, main area) many people like to use VerticalPanel and HorizontalPanel (or, even LayoutPanel) for their simplicity. I avoid using these panels as they offer rigid, table-based layout. I prefer to use either HTMLPanel or FlowPanel, and achieve the desired layout through CSS.
Note that I would not use "old" or "new" attributes when discussing the pros and cons of different panels. They all translate into standard HTML. If you are comfortable with CSS (a lot of developers aren't), then you should use CSS as much as possible, because this is what browsers are optimized to do, and because you can build more fluid, consistent and future-proof (i.e. much easier to change) layouts with it.
What kind of app are you building?
Do you need 'user-positioned splitters' between panels?
Do you need separate scrolling panels?
If not, stick with CSS.
Start with an HTML/CSS mockup and then convert that to a GWT UiBinder file. Particularly when it comes to responsive web design, you can get a HUGE HEAD START by starting with an off-the-shelf CSS framework like Twitter Bootstrap.

Visualizing multiple layouts in jung

I am creating a RadialTreeLayout of a graph using JUNG. Now I want to see one more RadialTreeLayout of another graph (with same type of vertices and edges) as a part of the main layout.
The problem is not of uniting the two graphs (as explained here) but actually of visualizing a similar layout in the main window.
Few ways I thought of doing that, but not sure if they are feasible or not. e.g. to create one such node which actually is the layout (and can be seen with zooming in) or when one clicks on that node, another layout appears in a separate window.
Or are there existing ways to do that in JUNG already. Any help/suggestions would be appreciated.
Check out the demos. I don't remember offhand which one, but at least one demonstrates visualization of multiple graphs.

Java swing design : static vs instance

I have designed an html document editor and I wanted some help in resolving a design problem in it. The problem is as follows -
The document editor consists of a surface (JFrame) and a menu bar. Inside the surface I have three panels - a tool panel, text panel and status panel (which are all extensions of JPanel). The text panel has the customized text pane (extension of JTextPane). Now the problem is that - there are lots of scenarios where the menus and the widgets items on the tool panel need access to the underlying document model of the JTextPane. E.g. to implement list/paragraph dragging functionality, the ruler needs to know the position of the caret inside the document model, so that I can mark the paragraph whose left inset I need to increase.
The way I have organized my design right now is that - surface is a singleton, so to access the html document model inside the JTextPane you need to code your way through the following maze -
Give me surface -> go to text panel -> go to text pane -> go to document of the text pane
Another alternative to make the reference of the document inside the text pane static, so that it can be accessed directly.
TextPane.getTextDocument ()
Also the html document inside the Text Pane I am using is not customized right now. So I am just using the default HTMLDocument returned by the text pane. Though in future I may have to replace it with a more customized extension of HTMLDocument (e.g. to implement cusom tags)
I am somewhat of a design novice. Can some design guru throw some light/insight into this?
I think if you get 10 responses you'll get 11 answers. :-)
First, there is no need for a Singleton. In this scenario, it's just a way of cheating with a global variable. And what happens when you want to have two or three text documents open?
Do your menus have ready access to the tool panel? Since toolpanel is a sibling of the textpanel, and in a 1-1 relationship (I think?) it is somewhat "reasonable" for toolpanel to know about textdocument (have a link it) and a convenience method, getTextDocument(). And it seems reasonable for your widgets to know about their immediate parent, the tool panel.
That would be my way of approaching the problem - what links between objects "make sense". You want as little coupling as possible, but there are places where you do need coupling. Maybe I have misunderstood your problem, or maybe you think a lot differently than me, in which case you should do it another way. Also, you are making a best guess as to how your code might evolve in the future. Good luck with that! :-)
One big question for any solution - does the text document change? (e.g., is there a File->New menu or a File->Open menu?). If so, that needs to be considered. In my proposal, the JFrame, on a File->Open, would create the text document and then change the link to it in the ToolBar.
p.s. MVC purists - please add appropriate models and controllers! I just talked about the graphical components to keep things simpler...
p.p.s. Examples of "thinking ahead" for reasonable enhancements
Would this handle multiple JFrames open at once? Yes.
Would this handle a single JFrame, but with multiple documents (say, a tabbed interface for the text document)? Almost. The method has to be a little smarter to know which of the many documents is active, but you can easily imagine doing that.
How about when I need to hold a lot more info about the dicument, like the name (so Save gives a good default), last time saved, is it "dirty", is it HTML or XHTML or whatever... At this point, you'll probably want to add one more model layer. But you can sortof imagine, with one more layer of indirection (one more link) this being o.k. So it depends on how close you are to implementing #3 how you want to design.

NetBeans should be named "Mexican Jumping Beans"

I have been trying to set up a Java form in NetBeans with 15 - 20 visual components (buttons, textfields, etc.) and I have been using the Free Design layout paradigm on the MAC.
According to what I've read, the Free Design layout gives me various alignment guides, but does not try to force my alignments to specific row and column delimiters. However, I'm finding that when I do this, the width of my form may arbitrarily change, or some of the components I've already placed will move around radically when I make even small adjustments to other component positions.
Is there some way to anchor all these components, once placed, or is there a better layout paradigm that gives me the freedom to place components where I wish to?
IMHO, Matisse works best when you know what you want before you start. It doesn't seem to do so well with iterative changes.
Here are a few rules I follow when using Matisse in freeform mode. They don't make it wonderful, just less painful:
Build top-to-bottom and left-to-right. Most jumping happens when you go back an try to insert something.
Build it in one pass.
When you make progress, save it. There are conditions where Matisse will drop its undo list. Don't count on Ctrl-Z to bail you out. I use a local mercurial repo to track my changes.
Keep it small. The more elements, the more likely it is to blow up. Build it out of smaller components. For example, if you have a date field with a button to open a calendar
make that a component.
Add the component to the palette,
use that in the larger component.
Maybe it's time to look into other LayoutManagers, like GridBag or something else that will give you what you want. The built-in choice by NetBeans sounds like a poor one for your needs.
If you choose a null LayoutManager you'll get absolute positioning.
But along with it comes absolute responsibility: No help in repositioning any elements.
I've found Using GroupLayout directly works really well. It can be confusing at first because horizontal and vertical layout are done separately, but once you get used to it it's not that difficult. You can definitely get things to align, and stick together when resizing. It's far better then using nested panels, GridBags, and that kind of thing.
MigLayout might work too, but GroupLayout is included in the JDK.
You may want to look at the absolute layout. It allows you to put the component exactly where you want it, without all the jumping around.
GridBag Layout is my favorite, and specially in Matisse (Netbeans GUI). You have like a wizard to graphically manage all the properties.
Will take you few examples to get things perfect as you want, but when you do, you will never look back.
Take a look at Sun Tutorials GridBag Layout
I strongly, strongly recommend MigLayout (And getting away from GUI layout tools). Any time you think you are saving by using a GUI tool quickly evaporates the second you run into layout manager behavior. Real UI's are coded, not built with drag and drop.
I found that it took me about 3 hours of fiddling to get used to MigLayout - after that break in period, I found it to be incredibly intuitive and powerful.

Categories