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.
Related
I have gone through lots of tutorials which teach about layouts in Java Swing, but they don't seem to suffice my need. I am creating a solar system GUI using Java Swing, and i want to place the planets in the GUI according to the values i fetch from my micro controller, which are usually float point values. I cannot use the Grid Bag Layout, as to position a label i have to specify grid x and grid y, which cannot be the case since I receive float point values from the micro controller. The best resource i found is to use absolute layout where i can specify the position of the planet by giving mere X and Y Co-ordinates, which will be fetched from the micro controller. The problem I am facing now is that the absolute layout does not have auto re-size feature.
What would be the best possible option to adopt the auto re-size feature in absolute layout?
Swing tutorials were not generally meant for situations like this -- they were meant for people who want to write more normal GUI applications, using buttons, drop-down boxes, check boxes, radio buttons, menus, and have layout that follows currently accepted practices in terms of positioning those on the screens. If any of that applies to the part of your program that is not displaying planets, I encourage you to use what they have to say about it.
But you want to place things according to calculations of your own. I recommend doing that in a panel, calculating the size and position of your objects according to the size of the panel at the point of drawing. When the panel resizes, you will need to trap the event that says it is resizing and redraw. You will need to deal with your own minimums and maximums, etc.
I don't recommend the custom layout manager suggested elsewhere for a couple of reasons. Firstly, it won't save you any work at all -- you are still going to have to write the code that determines positions of things, if you just then draw your own graphic instead of attempting to position a UI element, I think it will actually be less work. And that's the second reason -- layout managers' purpose is to position UI elements within the panel, and the pieces of your solar system don't really have any need to be UI elements, just graphics on the screen.
Good luck.
I've made a concentration-pairs game using netbeans.More details for the game here
http://en.wikipedia.org/wiki/Concentration_(game) .
before the game starts the user has the ability to choose the game version (computer vs human,human vs human),their names and the memory of cards that computer has to remember in a computer vs human version,and also to choose dimensions and jokers.He can choose a specified size and jokers or his own preferred sizes (columns,rows,jokers).
Of course i have a limit for row and columns : 52.But playing with setResizable doesn't help at all when the user uses eg 52x52 or 50x3 as dimensions for the game... it looks horrible (Btw the images are 73x97 pixels).
My components are not buttons they are objects of my own gui class concerning cards (and has a connection with the logic of the card from another package).So i should try gridbaglayout instead of gridlayout? Gridbadlayout can be a real headache cause i've already used it for my radiobuttons.
right now i am using gridlayout but when i had setEnabled(true) so the eg 10 rows would actually fit on screen,the graphic card would stay small and not "fill" the whole component...
http://postimg.org/gallery/53g9ag0i/667f8271/ What i mean here
Should i resize all my components or the limit is too big and i should make it smaller ?
GridLayout is usually fine when you want components that have all the same dimensions. I recommend simply putting your grid of cards inside a JScrollPane.
Alternatively, if having the game area scrollable when the grid dimensions are large is unacceptable, you can take a look at the answer to this question: How to lock aspect ratio of a gridLayout in Java?.
Yet another possibility is extending GridLayout so that it tries to keep fixed aspect ratio for the cards. (For this you obviously need cards that are capable of drawing themselves properly at different sizes).
Why dont you compute the best size? Calculate what the maximum value for cards in a row is. Then estimate the minimal icon size of a card so that anybody can read it. Then you just have to calculate: ScreenDimensions / mininmalCardSize. And then you can adjust the desired size from the user. This is how i would do it. But i would like to use the Null Layout.
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.
what would be a standard/optimal java swing gui (eg. JFrame) dimension when rendering on a user screen? would it be wise to set preferred size to 1024 x 768 or 800 x600 or something similar?
should we set preferred size according to the screen size? or is that not a good route?
In my experience it's usually best not to set preferred sizes but to use layout managers judiciously and to call pack() on the top level window after adding all components, letting the components and the layout managers size themselves.
This really isn't a Java question, but a user interface design question. An application I am currently developing needs at least 1024 by 768 to fit everything properly. But if it didn't need the space, then why make it start at that size? Why not let the user maximize the window if he wants to? A user interface should work on as many different display sizes as possible.
My policy is that the best window size is the smallest one that still lets the user do everything he needs to do with the program.
Now back to Java: When creating a swing app, call pack() after placing all the components into the containers. If for some reason I feel that pack tightens things up too much, then I might add a little to the width or height right after a call to pack.
If you need to learn the dimensions of the display you are running on, use this:
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screensize = toolkit.getScreenSize();
With swing, you can also set the preferred size for components and this will affect how much size they take up after a call to pack().
Big enough so all the components fit in the JFrame and no bigger. Typically, I do not set the frame size in pixels, and just let the components size the frame so I know everything will fit.
I would say it's safe to assume more than 1024x768 resolution, but it depends on how compatible your application needs to be. To be certain you can get the screen size with Toolkit.getDefaultToolkit().getScreenSize();
According to the statistics given by w3schools here: http://www.w3schools.com/browsers/browsers_display.asp , it is safe to assume people have a screen resolution of 1024x768 or more.
If you are building a GUI with a fixed size, I'd recommend staying a bit under this resolution, in order to accomodate users with a double-height taskbar or a lateral taskbar (try to see for yourself what fits, something like 900x700 could be good enough).
If your window is resizeable then I would try different sizes to see what looks nicer. Depending on the layout of your application, too small will look too busy, too large could make visual elements too sparse. Obviously you shouldn't exceed the fixed-size limit above, or the current resolution.
You could also try starting your application fullscreen, this only makes sense for some applications though.
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.