I have created a program in which I display result-filtering options on the side of my Swing window, however by default they appear horizontally next to one another, which wastes the space I have allotted in my BorderLayout's WEST side.
Is there anything I can pass in my constructor or add-statement that will stack these up?
Here's my relevant code in a class that extends JFrame:
box1 = new JCheckBox("Points");box2 = new JCheckBox("Bleh");
pane = new JPanel();
pane.add(box1);pane.add(box2);
add(pane, BorderLayout.WEST);
So this is quite simple, but when displayed they show side-by-side, and that's what I am looking to change.
Any help will be greatly appreciated. If I missed a good source of research info, please pass it along.
Use GridLayout with n rows and one column for pane, where n is the number of checkboxes
Related
Im new in Java Swing, and want to make my layout, but can't do this
Look Now :
Look I want :
Code Now :
JPanel MainPanel = new JPanel(new GridBagLayout());
JLabel MoneyLabel = new JLabel(MoneyIcon);
MoneyLabel.setHorizontalTextPosition(JLabel.CENTER);
MoneyLabel.setVerticalTextPosition(JLabel.BOTTOM);
MoneyLabel.setText("Money:" + CarMain.Money);
JLabel MoneyClicksLabel = new JLabel();
MoneyClicksLabel.setHorizontalTextPosition(JLabel.CENTER);
MoneyClicksLabel.setVerticalTextPosition(JLabel.BOTTOM);
MoneyClicksLabel.setText("Money Clicks: " + CarMain.MoneyClicks);
JLabel BoxesLabel = new JLabel(BoxLv9_10Icon);
BoxesLabel.setHorizontalTextPosition(JLabel.CENTER);
BoxesLabel.setVerticalTextPosition(JLabel.BOTTOM);
BoxesLabel.setText("Boxes: " + CarMain.Boxes);
JLabel BoxesClicksLabel = new JLabel();
BoxesClicksLabel.setHorizontalTextPosition(JLabel.CENTER);
BoxesClicksLabel.setVerticalTextPosition(JLabel.BOTTOM);
BoxesClicksLabel.setText("Boxes Clicks: " + CarMain.BoxesClicks);
MainPanel.add(MoneyLabel);
MainPanel.add(MoneyClicksLabel);
MainPanel.add(jbtnMoney);
MainPanel.add(BoxesLabel);
MainPanel.add(BoxesClicksLabel);
MainPanel.add(jbtnBoxes);
This is simple example of, what i want, becouse i'm building ingame shop, with 13 labels like these, in each tabbedpane window. How can i make it look, like in second picture, what I want?
Im new in Java Swing, and want to make my layout, but can't do this
Probably no single layout can suit everyone's needs. But combining several layouts can usually handle most scenarios.
From the image you showed in the question. There is no need to write your own layout. You can always use sub panels to hold your components and set a specific layout for each sub panel to handle what you need for those individual areas.
The reason for the alignment in your first attached image is because:
JPanel uses FlowLayout as its default layout. Hence all the components added will appear in a linear fashion and tries to fill up the row as much as possible the panel's width can hold. Once exceeded the panel's width, the components will be pushed to the next row.
If you want to achieve the alignment in the second attached image:
You may create a main panel to contain several sub-panels (see image below).
The red box is your main panel and you may continue to use the default FlowLayout.
Then add your components into sub-panels (orange boxes) before adding it to the main. You may then use BoxLayout, FlowLayout or even GridBagLayout for the sub panels (orange boxes).
Artis Uljanovs, at night after work i will give a look at this to help you.
I recommend you already to read the following: https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
You need some foundations on Java Layouts.
Take a look at this image:
As you can see, I have a JSeparator between my "Auto Refreshing" JCheckBox and my "Show Column" menu, and my "Show Column" menu is wanting to be as far right as possible. Why is it not aligning itself to the left, like everything else before the JSeparator? And I can't seem to make it do so, here is my current code:
JCheckBox pulling = new JCheckBox("Auto Refreshing");
...
menuBar.add(pulling);
menuBar.add(new javax.swing.JSeparator(javax.swing.SwingConstants.VERTICAL));
JMenu showMenu = new JMenu("Show Column");
showMenu.setAlignmentX(Component.LEFT_ALIGNMENT);
menuBar.add(showMenu);
This tutorial might be helpful. A quote:
By default, most components have center X and Y alignment. However, buttons, combo boxes, labels, and menu items have a different default X alignment value: LEFT_ALIGNMENT.
So you can see that placement logic differs, in other words, don't count on it. However, I do not know why your manual alignment to left did not work. Most likely the problem is the size of your last menu. What you can do, is use glue as filler since JMenuBar has a BoxLayout.
menuBar.add(showMenu);
menuBar.add(Box.createHorizontalGlue());
This invisible space will be added to the end of your menu and it will push components before it the left.
The issue was the size of the JSeparator, it wanted to take up as much horizontal space as possible. So, my solution was to restrict it's size so that it could only be one pixel wide max:
JSeparator menuSep = new JSeparator(javax.swing.SwingConstants.VERTICAL);
menuSep.setMaximumSize(new java.awt.Dimension(1, 1000));
menuBar.add(menuSep);
Hi guys/girls i am trying to make a box which will take a user input of a number. Now i have made a text field but how do i change the size of the text feild.
This is what i got so far
public class example extends JFrame {
private JTextField example1;
example() {
box1 = new JTextField(10);
add(example1);
Now its just iam trying 2 get like a square size text feild which will allow the user to write a number in there. It will be a one diget number. Any help would be great. Thanks
You state:
iam trying to make a caluclater. So i am using the box to hold a number.
Most calculators that I know of don't have a small field for displaying the current number but rather they have a text component that covers the entire width of the top of their GUI, and the key to achieving this with Java Swing is in using the right layout managers and the right placement of your components. For instance, this GUI design could be solved by using a container that uses BorderLayout, and placing your display JTextField (not JTextArea) in the BorderLayout.PAGE_START position and the buttons in another JPanel held in the main container's BorderLayout.CENTER position.
For example, please look at the code in my answer here. It creates a GUI that looks like so: . This shows two calculators that only differ based on the size of fonts chosen.
Key code for this is:
textField.setFont(textField.getFont().deriveFont(BTN_FONT_SIZE));
mainPanel.setLayout(new BorderLayout(gap, gap));
mainPanel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
mainPanel.add(textField, BorderLayout.PAGE_START);
mainPanel.add(buttonPanel, BorderLayout.CENTER);
JTextField uses the number of characters(# of columns) rather than a pixel size. If you're looking for a text box which you can specify the size of, use JTextArea.
I'm trying to create a JDialog like the Symbol dialog in Microsoft Word that you get by choosing Symbol... from the Insert menu. Basically, it's an n x m (n and m are not known until runtime) grid of small buttons. I've got a first version of this working nicely using a GridLayout. The problem is that when you resize the dialog (and there is a requirement that you should be able to resize it), the size of the buttons changes. I need the size of the buttons to remain constant.
But I want the dimensions of the grid containing the buttons to change. For example, if the dialog gets wider, but stays the same height, the number of rows should lessen, while the number of columns increases.
I've thought of a couple of ways to fix this:
When the dialog is resized, create a new GridLayout and repopulate it with the buttons. I'm going to try this and see how it looks, but it seems like a clumsy way of doing it.
Use some other type of layout such as a FlowLayout. I took a stab at this, but it put all n x m buttons in one row. I do not want to use horizontal scroll-bars and the buttons ran off the right edge. Anyway, it's supposed to be a 2-dimensional grid of buttons.
What is the best way to solve this layout problem?
Create a buttons panel with GridLayout and set a fixed size (could be calculated at runtime of course) to it. The buttons panel should be contained in a panel of BoxLayout.
Check out the BoxLayout Tutorial
Very Very basic example:
public static void main(String[] args) throws Exception
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel buttonPanel = new JPanel();
JPanel containerPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(2,2));
buttonPanel.add(new JButton("1"));
buttonPanel.add(new JButton("2"));
buttonPanel.add(new JButton("3"));
buttonPanel.add(new JButton("4"));
buttonPanel.setPreferredSize(new Dimension(300, 400));
containerPanel.add(buttonPanel);
frame.getContentPane().add(containerPanel);
frame.pack();
frame.setVisible(true);
}
if the dialog gets wider, but stays the same height, the number of rows should lessen, while the number of columns increases.
Wrap Layout might be what you are looking for.
I had a similar issue with a single column of buttons, and found that MiGLayout (third-party, available here) was simple and effective for this. It helped both with making a grid and with setting button sizes, although it took me a day or two to get used to its syntax.
But the key is really setting button sizes; GridLayout certainly seems like the way to go for a layout that is, well, a grid. I haven't tested, but I suspect that the built-in setXSize() methods would work just as well. The GridBagLayout tutorial has examples of some things you can do with sizing/positioning.
FlowLayout would be the way to go but you might have some configuration problems. What layout manager does the parent component use?
I need to make a button that will display different things depending on the state if the app. So for example if nothing has been opened its title will be "Lesson Plans" and if project B is open it will be "project B Lesson Plan", I am using the java.awt.Button class for this. My question, is there a way to determine how big the button should be to fit the given text. For example can I somehow get the width of each character in the given font used by the button then multiply this by the characters in the title I want to use? How would I get this character width or is there some better way? Thanks!
Measuring widths of individual characters in particular fonts (not all characters are the same width) gets complicated, especially if you want to take into account the spaces between characters, and add some margin either side so they don't butt up against the edges of the button. (N.B. Others have provided a few ways of doing this more simply than I realized was possible!)
Much better/more usual solution is to let Java do the work for you. If you're using using a java.awt.Button (or javax.swing.JButton), then it will usually automatically size to the text on it. If your particular button is behaving strangely, then you probably have a layout problem. Check the section of the Java tutorial on Layout Managers for more information.
As an example (in Swing, rather than AWT - sorry!), these two buttons end up being the right size for the text on them:
import javax.swing.*;
public class ButtonTest {
public static void main(String[] args) {
JButton button1 = new JButton("Some label");
JButton button2 = new JButton("Some much longer label");
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.add(button1);
panel.add(button2);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
If you really do want the size, rather than just letting Java do its best, then I would recommend calling button1.getPreferredSize() and button2.getPreferredSize() after the call to frame.pack().
Just a quick glance at the API Docs shows:
public Rectangle2D getStringBounds(String str, FontRenderContext frc)
in java.awt.Font
Returns the logical bounds of the specified String in the specified FontRenderContext. The
logical bounds contains the origin, ascent, advance, and height, which includes the leading.
So that should help you.
On the awt Button you can call getFontMetrics and then ask for the getStringBounds. This you can then set the preferred size of the button based on this. It won't take into account the extra spacing you'll need for the borders though (which varies depending on platform).
I'd try setting each to the strings in turn, getting the preferred sizes and then just set the preferred size for the largest size.
Have you tried using either getMinimumSize() or getPreferredSize() on the button? I believe one of them will tell you the minimum size the button needs to be to display its label.