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. :)
Related
I'm using NetBeans 8 IDE to design a desktop application. How can I transit among different forms (e.g. login page and homepage)? Should I design multiple .java JForms or is it possible to completely delete previous components and bring new ones?
Is there something like hidden tabs that aren't shown to the user but really exist? I am looking for a high performance way to toggle forms.
Were you to have the form constructed programmatically - although this is a bit inefficient - you could use a switch statement in a loop with cases that contain the individual forms, and statements to clear the existing form components. There's also an easy JavaFX navigation solution using a larger-than-viewport area (containing each form in a horizontal series) wherein the forms get the viewport one-by-one, but it looks like you're using swing; sorry if it's of no help in this case.
the java programming offer you to create Java GUI (J2SE) with two options. As we known, there are programmatically(Coding from scratch) or using Java look and feel(Drag & Drop). As my experience, its very easy to create Java GUI using java look and feel rather than programmatically. So, i'm frequently using java look and feel. ;)
Now, i'm know its wrong, because its limit us to create dynamic application. So i wanted start to create java application (J2SE) programmatically. But my question is very simple, how to create this frame programmatically as below :
As we know, its very simple using Java Look & feel (Just drag and drop). How to create space between jLabel1 and JtextField1 as we desired? And how to create leading space(Space below and after) between jTextField1 and jButton1 as we desired?
I know this is very stupid question for java expert, but perhaps this question can help other people(particularly java beginner) to start learning Java Programming and not doing as what i'm doing now. :)
Thanks for your advice, sorry for my bad English and possible duplicate question. :)
In Java, you typically create a hierarchy of JPanels (Swing), each having a suitable LayoutManager (GridLayout, PanelLayout, BoxLayout, GridBagLayout, etc...). See the link below for details;
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
Using layout managers is the best dynamic approach for creating scalable and dynamic UIs, but there are lot's of drag-n-drop utility applications that set layout manager to null and thereby allow for pixel-based exact positioning of your different components. In your case, a JLabel, a JButton, and a JTextField.
Hence, to create the exact UI you shown, go for null-layout and place your component instances on upper-left-corner (x,y) positions relative to your frame's origo (again, upper-left corner).
If you use the Drag-and-Drop interface, it should generate some code for you. Examine this code and use it yourself, making changes as you like. The DnD interface is nothing but a layer on top of the code.
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.
I am developing desktop GUI application using java swing. And I want to show several "subwindow" on the same window(JFrame). And I want the layout is similar to iGoogle such that user can add and remove new subwindow. To be simple, I assume all the "subwindows" have the same size and similar content(all are showing chart). By the way, the maximum number of "subwindow" would not be a huge number. I think it is less than 8.
if there is no drag and drop, can I just use grid layout to
implement it?
if there is drag and drop, what is the easy way to
do it?
Thank You very much.
FYI: iGoogle http://www.google.com.hk/ig
I don't know what iGoogle is, but it sounds like you should be using internal frames. See the section from the Swing tutorial on How to Use Internal Frames.
If all the "sub windows" will be equal size and not draggable, I'd just use a simple layout. seems like a good case to use TableLayout. In a simple case where you know the max amount of slots, you could have 8 corresponding JPanels and add them to Container using the table layout (making sure to revalidate()) as the user requests them. if a user closes one, you just remove it from the container and revalidate. Hope that helps. if you don't need docking functionality, don't even go down that road is my advice.
EDIT:
you could also still implement drag and drop by using your own mouse handlers.
I have the following requirement for a GUI, that the user will have a number of available actions to perform.
Currently, it is implemented a series of JButtons that the user presses.
But the actions are a lot, and in each version more "actions" would be possibly offered.
I am not sure how this is better presented/done in GUIs.
I have read that there are ways to form a gui describing relevant information in an xml file.
I am using Netbeans and swing. Is the xml a good idea, to describe the actions available and for example create the same number of buttons? If yes, how would I start on this?Additionally would a jtoobar be a good idea for the actions? I mean add as many buttons as needed in the Jtoolbar.
I guess this is a general question but I am not experienced in GUIs.
Any help would be appreciated!
Thank you.
There seems to be two separate issues involved:
How to describe and create a large number of actions?
How to represent a large number of actions in GUI?
#Tom's answer is for #1, and #Fabio's for #2.
You can use any textual format for #1, xml, csv, whatever you like. It's indeed a good idea to separate that from code. XML is great for hierarchical data, so it may be overkill if you just need a flat list:
<doc>
<action name="Action 1" id="ACT1" description="blah blah" icon="icon1.gif"/>
<action name="Action 2" id="ACT2" description="yada yada" icon="icon2.gif"/>
...
</doc>
But parsing such a simple XML is basically free, so you may as well just use it. You do not, however, need a full-featured XML GUI toolkit like SwiXML, unless you want to add many other GUI widgets with complex layout to your app.
Note the attributes I have in the above sample. id would map to a unique action command. You can display description and icon (I suppose you use icon already) any way you want. You could also have other properties like mnemonic, accelerator, etc., at which point using XML starts to pay off: you can add arbitrary attributes that you need.
One obvious omission in the XML is the actual actions themselves. I do not think you should put Java code in XML. It defeats the separation of concern. Instead you can define your action code in a generic way (e.g. extend AbstractAction) and map them with the action IDs. If you do use AbstractAction, you can trivially map your attributes to action property keys like Action.NAME, Action.LONG_DESCRIPTION, etc.
Now you have parsed the XML into a list of action objects, and here comes the second question: how do you display them?
JList (per #Fabio) is indeed the most efficient way. It's much more compact than a whole bunch of individual buttons, yet unlike JComboBox you can see many items at once, and you can easily add sort/search/filter.
But list is not very flexible. You could use a custom ListCellRenderer to display icon and tooltip (for description), but you'll start to stretch it when you want to group items.
I think the most flexible way would be a tree table, which allows you to have multi-level hierarchy. You can start with 2 columns, the first column showing action names hierarchically, the second column showing description.
You could put the table in a collapsible panel so that it can be hidden when user wants to focus on the results.
Now w.r.t. JToolBar, you're right that it's standard, however as Fabio's comment pointed out, it's bad usability when you have too many buttons on a toolbar (like M$ Word before ribbon).
What would be a great use of a toolbar, however, is to allow user to place actions of their choice onto the toolbar, like most popular desktop apps do. You could use a "Customize Toolbar" dialog, or simply let user drag-n-drop items from your list or table.
Unfortunately, many of the GUI constructs that I mentioned above are not available in JDK. You can find all of them (tree table, search/filter, collapsible panel, customizable toolbar, etc.) from the excellent commercial JIDE libraries (disclaimer: I don't work for them, though I sometimes wish I do), or you can find FOSS alternatives.
I don't know if i did understand your question well, but it seems to me that the JButton approach isn't the most efficient way to do such thing. Imagine that the number of actions starts to be really big, drawing too much buttons leads to an unintuitive and non-appealing interface. An alternative would be using a JComboBox or a JList, to list all the actions not requiring much space and having a single button "do!" to execute the action.
I hope it was useful ;)
Contrary to popular belief around a decade ago, XML is not usually a good idea. An appropriate language to express this "programming" in is Java.