I'm currently working on a small project that requires "null" layout of my JPanel.
When adding or editing a JTextField, is there any other way for me to have it visible without having to setBounds()?
What I want is for the height and width to be determined by the text size, and for me to set only the location of the text in my JPanel.
If you want to set it to the default layout, do setLayout(new BorderLayout());
Related
I hava a JLabel (lets call it main) and during Runtime I am adding multiple JLabels to it. Sometimes these JLabels contain an Image, sometimes a JTextArea. The Problem is, because of the different Content they have different sizes. As long as I only add Text the layout is like I want it, just the size of the text. But if I add the first JLabel with an Image as content every JLabel I already added increases its height to the one from the JLabel containing the image.
My question is, which Layout should I use for the main JLabel and how should I add the other JLabeles which I add during runtime?
I first tried the GridLayout and now have the BorderLayout but not like I want it to.
So I have got round to creating a panel that has two labels and a button inside and these are alligned on the Y_axis via a box layout.
I am now trying to get it so that the text is alligned to the centre of the panel as well as on the Y axis for neatness.
Here is the code I have right now:
JPanel statPanel = new JPanel();
statPanel.setBorder(BorderFactory.createTitledBorder("Text Statistics"));
statPanel.add((averageLength = new JLabel("The average length of the words: ")));
statPanel.add((totalWords = new JLabel("The total number of words: ")));
//Create button inside statPanel
statPanel.add((stats = new JButton("Update Text Statistics")));
stats.addActionListener(new ButtonListener());
statPanel.setOpaque(false);
statPanel.setLayout(new BoxLayout(statPanel, BoxLayout.Y_AXIS));
As you can see I have already used BoxLayout in order to get the vertical alignment and I have tried the following code which didnt seem to affect my situation at all (and did seem very long winded):
averageLength.setHorizontalAlignment(SwingConstants.CENTER);
averageLength.setVerticalAlignment(SwingConstants.CENTER);
totalWords.setHorizontalAlignment(SwingConstants.CENTER);
totalWords.setVerticalAlignment(SwingConstants.CENTER);
stats.setHorizontalAlignment(SwingConstants.CENTER);
stats.setVerticalAlignment(SwingConstants.CENTER);
If you could advise me that would be much appreciated!
Thanks
You don't need to set the horizontal or vertical alignment. Those properties are used with a layout manager (ie BorderLayout) that changes the size of the component to be something greater than the preferred size of the component. Then the component aligns the text based on its painting rules.
Instead, you need to set the x alignment. In this case the component size is still the preferred size. However, the layout manager aligns the component to the space available in the container. So to center the component in the width of the container you would use:
averageLength.setAlignmentX(0.5f);
totalWords.setAlignmentX(0.5f);
stats.setAlignmentX(0.5f);
The BoxLayout doesn't change the size of the component so it respects this property.
I'm creating an interface composed out of a box layout which contains panels inside every space.
In this specific case I've got a "cascade" of panels, the first is supposed to be a panel with a FlowLayout as layout manager, underneath it there's a GridLayout and under it there's supposed to be another label.
The thing is that I'd need the first panel to dynamically resize as the window get resized itself.
Here's the problem: I need the first panel to have a specific size in relation to the absolute size.. the thing is that I can't set my preferred size.. in the class I do the following but the panel stays the exact same size as before..
this.setPreferredSize(new Dimension(windowWidth, 50))
I'll send the code as soon as I get home, for now the situation is the one written above.
I need the first panel to have a specific size in relation to the absolute size
Your main panel should be a BorderLayout (which is the default layout of a JFrame), not a BoxLayout.
Then you add the panel that needs to be a specific size to the BorderLayout.NORTH of the frame.
Then your second panel can use a BoxLayout and add this panel to BorderLayout.CENTER of the frame. Now this panel will get all the extra space as the frame is resize.
I have added a JTextField to a JPanel that is using BoxLayout.PAGE_AXIS.
I am then adding this panel to a content pane at BorderLayout.CENTER.
Right now my text field is stretching the entire width and height of BorderLayout.CENTER.
Is there a way to set a width and height of this text field without using a null layout? Or just somehow make it not stretch the entire width and height of BorderLayout.CENTER?
I see there is a JTextField.setMaximumSize(Dimension arg0); but I'm not sure what a Dimension is, or how to utilize it in this context.
Add a panel to the CENTER. Give the panel a layout that does not stretch component sizes (e.g FlowLayout). Add the text field to the flow layout.
My current problem is that I have a JFrame with a 2x2 GridLayout. And inside one of the squares, I have a JPanel that is to display a grid. I am having a field day with the java swing library... take a look
Image
Java is automatically expanding each JLabel to fit the screen. I want it to just be those blue squares (water) and the black border and not that gray space. Is there a way I can just set the size of that JPanel permanently so that I don't have to go through changing the size of the JFrame a million times before I get the exact dimension so that the gray space disappears?
I also would like to set the size of those buttons so they are not so huge (BorderLayout is being used for the buttons and TextField)
GridBagLayout is what you really want to use. The GridLayout will force the same size for each component in the layout no matter what size constraints you put on them. GridBagLayout is a lot more powerful and a lot more complicated. Study up on the API page for it. Using GridBagLayout, the components won't fill the whole grid space if you don't want them to and can even stay the size that you ask it to be. To keep a component's size from changing, I would set all three available size constraints:
water.setPreferredSize(new Dimension(20, 20));
water.setMinimumSize(new Dimension(20, 20));
water.setMaximumSize(new Dimension(20, 20));
For your buttons, I would definitely use an inner panel as Bryan mentions. You could use either a GridLayout like he suggests or a FlowLayout if you don't want all the buttons to be the same size. Add all your buttons to that inner panel instead of the main one.
If you want the two checkerboards to stay the same size, then you'll need to have them each contained in their own JPanel. Set each of those parent JPanel's to have a layout type of GridBagLayout. Set the preferedSize for each checkerboard component and then add them to their respective containers. GridBagLayout should by default lay each board out in the center of the parent JPanel. So as the window is resized, the JPanel parent area will get larger or smaller, but the checkerboard components inside will remain the same size.
Alternatively, you could have your blue squares scale to the right size as the window is resized by having each checkboard square be a JPanel with a BorderLayout layout manager and adding the JLabel (with a blue background color) to its BorderLayout.CENTER location.
As for your buttons, try something like this:
JPanel theButtonPanel = new JPanel(new BorderLayout());
JButton button1 = new JButton("Fire");
JButton button2 = new JButton("Pass");
JButton button3 = new JButton("Forfiet");
JPanel innerButtonContainer = new JPanel(new Grid(1, 3, 8, 8));
innerButtonContainer.add(button1);
innerButtonContainer.add(button2);
innerButtonContainer.add(button3);
theButtonPanel.add(innterButtonContainer);
Lastly, consider using a design tool for your Swing user interface. Netbeans has an excellent UI designer built into it. Download Netbeans here.
If you can setResizeable( false ) on the top level frame you can then set your layout manager to null and hard code each location and size via setBounds. This is how I would do it (contingent on resizing of course).
I have had success solving problems like these using TableLayout which is a third party layout manager. You will need to download it and read the tutorial but the key would be to set the justification to CENTER when adding the JButtons to their positions in the layout.