I've spent many fruitless hours trying to create what I consider to be a very simple GUI. One thing that makes it simple is that I don't even expect it to be resizeable. I want it to display just the way I've laid it out. Simple as that.
There's a JFrame containing two Jpanels of equal width, one above the other. I've got the JPanels behaving themselves finally, and I can slide them around without their enclosed components mysteriously shifting.
But I can't for the life of me get the JFrame to nicely enclose the JPanels. I'm attaching a screen shot showing the layout and the inspector, and another showing how it previews
(Notice that it's chopping off the bottom edge.)
Is there any way to work in a "WYSIWYG mode" in the GUI builder? I don't care about (in fact, I dread) resizeability, at all at this point. I would have thought Absolute Layout would be the right choice for this, but there's still something wrong. (I need an elementary solution, folks -- please don't suggest GridBagLayout!! ;)
EDIT: By restoring some defaults, mainly for MaximumSize, to the JFrame, I got rid of the clipping problem. The right edge was still off by one pixel, and I managed to find which of five (five!) width properties was controlling that. (bounds, max size, min size, preferred size, and width - changing min size fixed that.)
I've already found that you can't completely avoid tweaking properties (e.g. to override "snapping" and achieve pixel-precise positioning). I clearly made trouble for myself at some point by playing with some properties I shouldn't have. The question remains: for non-resizable, WYSIWYG GUI design, is Absolute Layout a reliable choice; and, what properties should I avoid editing?
Is the JFrame showing any better when you actually run than preview? I think this is an OS X specific NB behavior. I did not see this in Win32. For now, you may try adding a JLabel ( spaces as the text content) at the bottom to make sure that space is drawn when the actual JFrame runs.
Related
Just began GUI programming a few days ago and I am wondering how I know what dimensions to give my window and the objects inside of it. I know in my head what I want them to look like but I am just not sure how to know the measurements. Any tips?
Modern UI programming does not normally involve setting exact measurements of components in a window. You need to read a basic tutorial on Java Layout Managers; the basic idea is that you use the layout manager to arrange things so that they are lined up the way you want and stretch the way you want when the window's size is changed by the user. You put the components in the places that achieves the overall topological shape that you want, and you don't set a specific size.
Good luck.
We have developed a huge application using Java Swings, this is well exceuting and running on all systems, but the problem is the resolution , if the resolution is 1260/768 it works well means all the components including the scrollbar will be visible, even application will fit to the width and height of the screen, but when its below 1280/768 its not fitting the screen, what i do is manually change the system resolution to 1280/768 and also wrote program which will change the resolution, but the problem is most systems does not support more than 1024/768,on old systems its max VGA Cards-1024/768.
What is the way to resolve this?Which layout manager to change?
Update
Our application will be going live in next 5 days, so need something much quicker, tried with FlowLayout but it will not be good UI.
Or how to resize components when maximized or minimized? how is it implememted?
The answer basically depends on how your GUI is designed.
In some cases, a FlowLayout will allow components to wrap around.
JScrollPane wrappers can be added around sections to make them independently scrollable. Along this line of thought, the entire current GUI could be placed in a JScrollPane and set never to be less than 1280x768 such that scrollbars will appear on smaller displays.
JTabbedPanel could also be used to stack sections of the GUI which are not commonly used in unison.
The smaller resolution could use a smaller and especially a more narrow font. It is a huge task to substitute hard coordinates with scaled ones; something like Scale.x(80). But it is a "dumb" dependable solution. If you still can use a smaller font (Arial Narrow?).
Mind, smaller resolution is often displayed on the same physical size monitor. Or with today's tablets tininess is acceptable.
I have just completed an application for my final year project and I need to create the interface for it now. The application will not include many different screens, just one introduction screen with a simple tutorial and the main screen with 5 JPanel and a JMenu. I have each part of the application providing its own JPanel, and the GUI I am about to make should put all those panels together and provide the intro.
What I want to ask is how I can properly set the sizes of different
components so that they are displayed the same on different screen sizes.(not getting really close to each other on small screens / big blank spaces on larger screens)
Should I manually set their preferred sized based on some percentage of the screen dimensions ?(e.g. 20% * width,40% * height) Or there is some other way to do it ?
Also, having one week ahead to complete this part, would it be any benefit to try and learn some library like MigLayout? I read a lot that is easier to use than standard Swing.
p.s
The JPanels include trees,textAreas,toolBar, buttons,checkboxes,comboboxes and textfields. Each one of those panels are quite simple to make.
The answer to this type of question is pretty application dependent, simply because what is 'reasonable' depends on the application and user expectations, but...
If you want the content of the frame to dictate the size of each frame, you can just call frame.pack() and an appropriate size will be guessed at based on the size requirements of the frame's children.
However, if it makes more sense to let the screen size dictate the frame dimensions, you can use Toolkit.getScreenSize() to get the screen size and do your positioning based on what you find.
I'm perplexed by the "snapping" behavior of NetBeans GUI Builder - those dotted lines (and the magnetic force they exert) as I move components about within a container. Is it determined by the Layout Manager? Or by container properties such as Insets? I wouldn't have expected the Absolute Layout to have any "preference" about alignment, yet even it seems to have some notion of a "grid" toward which it pulls my components. How or where does one change this?
You have to use a work around to do this.
If you do...
...setLayout(null);
Then you will reduce the snapping to 10 by 10 pixels.
If you want to get better than that, you have to do something like this:
http://forums.netbeans.org/post-37209.html
I am working on a project for an online class I made the mistake of taking this summer and I need to build a gui to show how the huffman code algorithm works. The algorithm part is easy, its not very complicated. However im unsure what the best way to draw the tree(forrest) at each step. It would have to start out as just n nodes (with chars in them) on the screen and then you would press a "next" button and it would pick the two lowest nodes weighted (based on character frequency) characters and make them children of a new node (with just a weight - no char) and then update the screen/panel.
I have made swing gui before, my skills are nothing special but I know my way around. However im stuck on this implementation. I have a couple hundred lines of code written right now, but it doesnt work and I think its bad anyway, so I want to "start over" and plan it out better. So Id just like some advice on the data structure to keep track of the nodes and how to draw them on the screen.
I was using an ArrayList of JPanels as nodes and trying to draw them to a null layout. Im sure this is awful and id like to know a better way. Possibly GridBagLayout?
NOTE: don't say JTree.
A good option is to just use a library for drawing trees/graphs. I've had good success with Visual Library in the past.
Another possibility is Prefuse
Instead of wrestling with the different Swing layouts you could just do custom 2D drawing. See for a simple enter link description hereexample here on how to get started.
Use an image of a tree (only one instance) and an array or other data structure to contain the "data" that the algorithm uses. Think about how you can use the data to determine where the image should be painted. Use the repaint() after the algorithm runs.
[Next] --> Algorithm runs --> Update using repaint();
So you have a single frame, a single panel and a single BufferedImage object.
The trick will come in when you have to get slightly mathematical to know at what co-ordinates a node should be painted.
The layout of your components are insignificant, as you're not adding any components to the container, just painting image data onto it.
We all had that what you have once ;-)
First of all, never use Null layout because then you make sizes static and your application will not work on other resolutions like desired.
Best layoutmanager to use: GridBagLayout !
Why ? very flexible and you can get all components exactly on the place you want, discarding the resolution. Its harder to set up but better result eventually.