Hi i am trying to make java desktop application where i am having 4 jbutton i want to set them bottom left corner one by one i am using null layout for setting them but i dont want to use null layout so
how can i achieve this ?
Thanks in advance
ok = new JButton(s);
ok.setBounds(800, 725, 100, 40);
ok.setBackground(Color.red);
ok.setOpaque(true);
ok.setForeground(Color.BLACK);
c.add(ok);
print = new JButton("Print");
print.setBounds(925, 725, 100, 40);
print.setBackground(Color.red);
print.setOpaque(true);
print.setForeground(Color.BLACK);
c.add(print);
next = new JButton("next");
next.setBounds(400, 725, 100, 40);
next.setBackground(Color.red);
next.setOpaque(true);
next.setForeground(Color.BLACK);
c.add(next);
home = new JButton("home");
home.setBounds(500, 725, 100, 40);
home.setForeground(Color.BLACK);
home.setBackground(Color.red);
home.setOpaque(true);
c.add(home);
Sort answer, use a layout manager.
Long answer, pixel perfect layouts are an illusion in modern user interface design. You can't possible predict the combination of hardware and software that is used to render content to the screen and what effect that will have on your components.
Each system could be using different fonts, rendering pipelines, DPI, hardware...and much more, all of which will change the size of the text and other elements on your UI.
Swing was designed to work around the use of layout managers, trying to do with out greatly increases your work load, as you now need to do the work that the layer manager was as well as be able to detect changes that occur around you and the parent components that you've been added to...
Looking at your code, I could suggest FlowLayout or GridBagLayout as a possible starting point
As #MadProgrammer said you really should use layout managers.
But if you insist on working like this use:
c.setLayout(null);
Related
I have just finished a project and ran into a problem I could not find anywhere on the internet so far.
I have several JTextFields on a JPanel on a JFrame. When I open the frame, all text fields are in the right place. If I minimize the frame and open it again, all text fields are moved and smaller.
These are photos of my frame before and after I minimize it.
Before:
After:
panel = new JPanel();
panel.setBounds(0, 0, 400, 175);
panel.setVisible(true);
frame.add(panel);
field0 = new JTextField();
field0.setBounds(10, 55, 120, 20);
field0.setVisible(true);
Frame.panel.add(field0);
field1 = new JTextField();
field1.setBounds(250, 55, 125, 20);
field1.setVisible(true);
Frame.panel.add(field1);
field2 = new JTextField();
field2.setBounds(10, 105, 365, 20);
field2.setVisible(true);
Frame.panel.add(field2);
Swing was designed to be used with layout managers. The job of the layout manager is to set the "size" and "location" of each components based on the preferred size of each component and the rules of the layout manager.
In your code the setBounds(...) code is only temporary. When the frame is resized, the layout manager is invoked and the proper size/location is assigned to all components.
So the solution is to not attempt to set the bounds manually but to use layout managers effectively.
Read the section from the Swing tutorial on Layout Managers for more information and working examples to get you started.
Based on your example picture I would suggest you can use the GridBagLayout. It will allow you to create a GUI using row and columns. You can also have components span several columns. I see the you have 5 rows and 3 columns. Download the working demo code from the tutorial and modify it for your requirements.
all text fields are moved and smaller.
Each Swing component should determine its own preferred size.
When you use:
field0 = new JTextField();
the preferred size is what you see.
The better way to create the text field is to use:
field0 = new JTextField(10);
Now the "10" will allow the text field to determines its preferred size to hold 10 "W" characters.
Also, Swing components are visible by default, so you need need to use setVisible(true) for every component.
my question is simple but I can not find exact solution for this. I try to write a program with Java GUI, I use absolute layout for my program and I set frame bounds(100, 100, 450, 300) . When I run the program and make it full screen, components stays on their places. I want them to replace automatically according to screen size.
All supports are accepted, thank you!
The solution is quite simple. You state, "I use absolute layout for my program and I set frame bounds(100, 100, 450, 300)." Don't do this. Use the layout managers to help you place your components.
For instance, if you used a BorderLayout for the main JPanel, and added the JButton to a new GridLayout(0, 1, 0, 5) using JPanel (1 column, variable rows, 5 spaces between rows), adding this JPanel to main in the BorderLayout.LINE_END position, your problem is likely solved. As a general rule, you should avoid use of null layout and use of setBounds(...) for component placement as this makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain.
I am trying to show a scroll bar next to my text pane but I can't find the reason why it doesn't show.
this.setLayout(null);
editorPane = new JTextPane();
size = editorPane.getPreferredSize();
editorPane.setBounds(17, 12, 533, size.height * 3);
editorPane.setBackground(Color.BLACK);
editorPane.setForeground(Color.WHITE);
//editorPane.setEditable(false);
console = editorPane.getStyledDocument();
scrollConsole = new JScrollPane(editorPane);
scrollConsole.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.add(editorPane);
this.add(scrollConsole);
Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify
See Why is it frowned upon to use a null layout in SWING? for more details...
You have two basic mistakes...
You've decided to use a null layout, but neglected to set the size of the JScrollPane
You set the JTextPane as the view for the JScrollPane but then add it to the container, along with the JScrollPane. A component can only belong to a single container, by adding it a second time, you've removed it from the JScrollPane
See How to Use Scroll Panes for more details
I am using Left-To-Right (LTR) as default Component Orientation for laying out the swing components in JPanels. Now I need to mirrorize all the components in the panel and display them from Right-To-Left(RTL). This will be used for Arabic Locale.
For example the order of components: "label-textbox-button" must turn the new order: "button-textbox-label" without changing their vertical positions.
setComponentOrientation and applyComponentOrientation methods are not working because the layout managers I used, does not support these methods. These methods are supported by FlowLayout, GridLayout etc. I'm using absolute coordinates like this :
JPanel panel = new JPanel();
panel.setBounds(0, 50, 800, 100);
JButton button1 = new JButton("Cancel");
button1.setBounds(100, 60, 100, 120);
panel.add(button1);
when mirrorize, bounds of button will be : (600, 60, 100, 120);
Here is a clear sample about what I want but that is JavaFX version of it. I also do not want to recreate components screens. I just want to repaint and redisplay them according to Locale. So looking for any Java API or open library, if there are any. Thanks for your suggestions and help about where to find it if possible.
I have got a window that looks like window1 and I would like it to look like window2:
This is my code:
String q = "Have you used GUI before?";
JLabel textLabel2 = new JLabel(
"<html><div style=\"text-align: center;\">" + q + "</html>", SwingConstants.CENTER);
add(textLabel2, BorderLayout.NORTH);
JPanel radioPanel = new JPanel();
add(radioPanel, BorderLayout.CENTER);
JPanel btnPanel = new JPanel();
add(btnPanel, BorderLayout.SOUTH);
For the radio-buttons, I tried to use GridLayout, but it broke the position of "Yes" and "No". And for the "back" and "next" buttons, horizontal alignment did not work (btnPanel.setAlignmentX(RIGHT_ALIGNMENT);), apparently. Any solutions will be highly appreciated, I'm stuck with this bit way too long. Thanks
--EDIT--
That is working perfectly fine:
btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.LINE_AXIS));
btnPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
btnPanel.add(Box.createHorizontalGlue());
so the buttons problem is solved.
However, still can't get the radio-buttons fixed.
--EDIT 2--
Fixed the background for the radio-buttons using setOpaque(false);
What do you mean by it "broke" the position of "yes" and "no" as a GridLayout should work just fine. I'd give it 1 column and 2 (or 0 for variable number of) rows via new GridLayout(0, 1). Be sure that its opaque property is set as false by doing radioPanel.setOpaque(false);. This way it will show the background color of the container that it sits in. You may need to make the JRadioButtons non-opaque as well, I'm not sure.
Your btnPanel could use a BoxLayout and use Box.createGlue() to push the buttons over to the right side.
Most importantly -- if you haven't yet done so, please read the tutorials on use of the Swing layout managers which you can find here.
A couple of things you can do about this. You need to change your LayoutManager. This is not a great task for BorderLayout. You could do nested BoxLayouts. A vertical box that has the vertical fixed height strut, label, vertical fixed height strut, yes radio, vertical fixed strut, no radio, Vertical glue, and the final button panel. Then use your edit in the button panel to horizontally align them. That's one option, but the nesting of the panels is annoying.
Another option go get TableLayout and learn how to use it. TableLayout is one of the best LayoutManagers. It's easy to use, solidly tested, and it makes Swing fun again. You'll never use GridBagLayout ever ever ever again.
http://java.sun.com/products/jfc/tsc/articles/tablelayout/
The final option is use the new GroupLayout. I'm not terribly familiar with it, but it looks pretty easy. And, it doesn't take as much code or nesting unnecessary panels like Box does.