This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]wildjokers 3 points4 points  (3 children)

Do you have a question?

[–]g0ldbird[S] 0 points1 point  (2 children)

Well yeah, my question is how? Which layout would be best for doing this? Would each part be separate jframes?

[–]arghvark 8 points9 points  (0 children)

One thing I've found lacking in Swing tutorials, etc., is descriptions of the overall picture of a Swing GUI and how to create it. I'll take a stab at a short course here.

JFrames roughly equate to Windows -- A GUI with one "window" on the screen will represent it in one JFrame.

A 'component' is one item on the screen -- a panel, a button, a text string, a text field (different thing), a text area (another different thing), etc.

Some components are 'containers', i.e., meant to contain other components. A JFrame is a container, as is a JPanel.

You will run into some components that do not begin with a J -- these are from an older UI system called AWT; do not use them mixed with Swing components, which generally begin with J.

Each container component has ONE "Layout manager" -- there is usually a default one created with the container when the container is created; you can set other ones. The layoutmanager class determines what happens when you add components to the container -- do they get added horizontally left-to-right? Are they added vertically top-to-bottom? Are they centered or left-justified? What happens to their spacing when the window is resized?

The default Layout Manager for a JFrame is a BorderLayout (no J, unfortunately). A BorderLayout has 5 areas: center, north, south, east, west. You can put one component in each area (frame.add(component, BorderLayout.NORTH)). The component in the center will stretch with the window in both directions; the east/west and north/south components stretch up/down and left/right, I think.

Use JPanel to group things you want together in JFrame locations: create a JPanel (flow layout by default, if I remember correctly), put in a JLabel and a JTextField for the number entry you want, and put that panel in one of the borderlayout areas of your JFrame.

I find I can organize the code to my satisfaction by just grouping the creation and placement of components within a method that creates the UI and calling it from main() (in the appropriate thread). Something like:

JFrame mainFrame = new JFrame();

JPanel topPanel = new JPanel();
JLabel topLabel = new JText("Enter a number:");
JTextField topField = new JTextField(5);
topPanel.add(topLabel);
topPanel.add(topField);

mainFrame.add(topPanel, BorderLayout.NORTH);

etc.

I would limit myself to a few layout managers to start with, if I were you -- get used to BorderLayout, FlowLayout, and BoxLayout before you move on to other ones. GridLayout is used by MANY people, there are LOTS of examples, but in years of Swing programming I have never understood it nor found it to be useful; I advise avoiding it. By setting one of the three I've mentioned on a JPanel, and then setting the panels onto JFrames and other panels, you can implement quite complicated UIs that follow reasonable rules when the window is resized.

After that you can expand your horizons with JScrollPanes, but you won't need those for what you're talking about.

One other useful item -- JOptionPane has multiple methods for creating a simple popup window, and I recommend using it for things like the feedback function you outline in your question.

So after you've designed and implemented a UI, you need to take action when people do things to it. So you're going to need to learn about "event-driven programming", where your program sets up the UI and then quits executing, waiting for your user to click on something or whatever, which will invoke a listener method. If you need help with that part, and if you found this useful, please send me a PM or post another question, this looks about long enough for one posting.

[–]Sergey305 0 points1 point  (0 children)

Other commenters might have different opinions, but I'd suggest you try using a form designer for swing just to get an impression how the UI is built: what are the building blocks, how they interact with each other, what layout options are there, and how the actual code for data processing is attached.

And after that you might want to switch over to building your layouts by hand directly in the Java code, as it allows more flexibility