I am using Java Swing and I designed a form page with a few buttons like play, pause, stop & break. They have different sizes, so they seem bad. I want to set size of the buttons to the same size.
How can I do that?
You can use setSize(Dimension d) where you specify the size of each button.
Or you can use different layouts to your application - I am mostly using the GridLayout which works really fine for me - see the tutorial
EDIT:
As mentioned in the comments: Better approach is to use some layout managers to handle the size for you. If you set size of button explicitly via command above, it can cause looking it "not nice" if you resize the window. Layout can do this for you more nicely.
BTW, if you will use
private JFrame application;
application.pack();
prior showing the application to the user, it can "miracelously" cure all "not nice" looking buttons and handles
Related
I'm trying to make all my components to resize properly with JFrame Netbeans, but if I put the auto resizing to my components, it has like a margin between them, and if I try to put them together, it messes up the components , and I need them to be touching each other.
Thanks!
Double-click on the extra space between the components: it should open a dialog where you can change its size.
You can also click the extra space, and use the mouse wheel.
Netbeans form designer is very powerful and handy when you master it, but it requires some effort at first, especially for large user interfaces. If not already done, take some time to follow this tutorial: https://netbeans.apache.org/kb/docs/java/quickstart-gui.html
Also, if your user interface is large, it's better to break it in several smaller panels. You can design each panel independently with the form designer.
I have a task to create a desktop app and I decided to use Swing as I have some experience with it. Also I am using Intllij and I noticed it have a form creator visual interface so I want to use it because its easier.
Samples of my interface is attached here. Its not hard but I dont know which controls to use in order to make something like this. On startup I need to have some text on the right side of the window, then on press on different button on the left to change that text with some other controls like fields labels check boxes etc. Its bit like tabs but I cannot use the JTabbedPane because it become with too different design. Could you advice me which controls to use and how to use them in order to achieve this design ?
Here is the design:
Also I am using Intllij and I noticed it have a form creator visual interface so I want to use it because its easier.
It isn't easier as you end up spending time learning the IDE instead of learning Swing. Any code that is generated will not be maintainable if you ever need to switch to a different IDE.
Learn how to create/maintain the GUI forms manually.
, then on press on different button on the left to change that text with some other controls like fields labels check boxes etc
Start with the standard BorderLayout for the frame. Then you create a panel with your buttons to display on the left. You create a second panel that uses a CardLayout in the CENTER of the BorderLayout. Then when you click a button you swap the panel that is displayed in the CENTER.
Read the section from the Swing tutorial on Layout Managers. There are sections on:
How to Use BorderLayout
How to Use CardLayout
to get your started with working examples.
If you are creating a commercial application(rather then as a leaning experience) consider using JIDE Common Layer as the MultiplePageDialog provides the functionality you seem to be describing:
In this case a series of buttons on the left controlling a panel on the right
I want to create an applet with first form have multiple button, each button takes me to another form to perform a specific task.
How to do that using a Java applet?
There are lots of different ways to do this. The details depend on what exact effect and feel you are after. For example:
A CardLayout in the CENTER controlled by a JList (the menu) in the LINE_START of a BorderLayout.
A JTabbedPane, though that doesn't quite fit the use-case here.
For a blocking component, look to a free floating modal JDialog or a JOptionPane. This encourages the user to concentrate on one 'transaction' (e.g. new|edit|delete) at a time.
Non modal dialogs for when the user can have multiple dialogs/transactions open in parallel.
...
See also
A Visual Guide to Layout Managers
A Visual Guide to Swing Components (Java Look and Feel) (currently an HTTP 401 error here..).
More on using layout managers or combinations of them1, along with layout padding & borders for white space2.
I tried to add image to my login frame in Netbeans, so I try to change the layout from 'free
Design' to 'Null Layout' (like I see in this video: http://www.youtube.com/watch?v=uZFgiqM0udA), and it's work.
But when I run the program the window open in the left side, and not in the regular size -
as you can see in the picture:
What is the problem?
Welcome to the wonderful world of why you should not use null layouts (and why learning to code a UI with a form designer is also a bad idea).
The problem you are facing is based on the fact that Swing (and AWT) were designed to work with layout managers, this is at the core of how the framework works.
When you call pack on a Window, it asks all it's children what size they would like to be and calculates the best size for the Window.
In your case, because you're not using a layout manager, the window is assuming it's default side of 0x0 (plus the frame border).
To fix the issue I suggest two things. Firstly, stop using the form designer until you understand how the UI is constructed and secondly, make use of one or more layout managers.
Take a look at A Visual Guide to Layout Managers and Using layout managers
I've not had the chance to use it, but it might also be worth while to take a look at MigLayout, it comes highly recommended by many of the users on SO
As far as I know, dont use null layout, use LayoutManagers instead, anyway, you're free, I think you're calling pack() method to the frame and you probably didnt set the aize of frame
or you can move on and start by tamming a little the layout manager.
select your frame in the navigator and then, look for this propierties
preferredSize - The size that frame will take as their default
maximumSize - The maximum size of the frame
minimumSize - Minimum size of the frame
Using Absolute Layout fixed mine! For some reason, default size 0*0 was getting executed on run ( but the frame remains in custom resolution), so changing null layout to absolute is calling the custom frame size.
In my Netbeans code I have JPanels and JDialog which are driving me crazy at times. Some of the controllers on these containers decide not to show up or automatically change size even though I have set up both their size and contents within the code and through using the IDE properties. For instance some of my jButtons on a certain JPanel does not show its text label or the sizes of some of my text field change.
Any solution to this would be grately appreciated!
When you create GUI using the NetBeans IDE wizards the Layout manager attached with JPanel and JFrame is GroupLayout and it works as expected. It keep the size of your JPanel and JFrame as you have specified.
Now if you change the LayoutManager of the JPanel or JFrame then you are on your own. You must know the consequences of changing the LayoutManager and update / add the required code to make the code to run as expected.
I will suggest you to keep the default LayoutManager as GroupLayout if you want to get what you see in the NetBeans component designer.
Unfortunately you did not provide any code snippet that can show your problem. But let me assume that you are confused with layout behavior. Typically we use Layout manager and delegate to it the responsibility of placing and re-sizing the graphical elements. Layout manager does it work when the parent element is being painted, i.e. during execution of method paint() that happens asynchronously and may be caused by various events (e.g. changing focus, re-sizing of window etc).
In this case all your attempts to change size of specific element by calling its setSize() could be overridden by layout manager that decides to change size of the same element differently.
So, if my assumption is correct learn to use layout managers and ask more specific questions if you have any difficulties with them.