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.
Related
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());
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 have a button inside a JPanel with both, a width and a height of 70 and I can only seem to center it while keeping it's size in a null layout. While I want it centered like that when resizing the button will stay at it's exact cords. Is there a better layout that can easily acheive this or am I able to do it in a null layout?
Use GridBagLayout with anchor=GridBagConstraints.CENTER, fill=NONE, weightX, weightY=0
UPDATE:
See e.g. here
I'm writing a Java Applet and on one of my CardLayout Panels, the GridLayout within the BorderLayout.CENTER is streching the TextFields horizontally and vertically (this is ok because I can set vertical spacing between the parts of the grid). Can I stop the TextFields from being so wide and can I center the text within them?
Consider nesting the JTextField-containing JPanels inside of another FlowLayout using JPanel. This way the FlowLayout using JPanel is the one that gets stretched while the JTextFields are sized based on what you set the int col property to be. Note that this will fail miserably if the nesting FlowLayout using JPanel is smaller than the preferredSize of its child JPanel.
I need to create a panel where I can put some rectangles and it automatically reorder just inserting a scrollbar and growing up vertically. Also this panel can be resizable and again the rectangles must to be reordered to correctly be displayed inside the panel.
If I understand the question you want components to wrap to the next line so that the panel grows vertically while the width remains fixed.
If so then check out the WrapLayout
Note: the FlowLayout already supports the wrapping of components to a new row on the panel. This issue is that the preferred size calculation assumes all components are placed on a single row. The WrapLayout overrides the preferred size calculation to support the wrapping of components on a new row.
Use a JScrollPane. If you never want a horizontal scroll bar you can add the following:
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
(By default the scroll pane will add horizontal and vertical scroll bars when required.)
The scroll pane itself will only be resizeable if you add it to a Container with the appropriate layout manager; e.g.
JFrame frm = new JFrame();
frm.setLayout(new BorderLayout());
JScrollPane sp = new JScrollPane();
frm.add(sp, BorderLayout.CENTER); // Adding a component to the CENTER will cause the component to grow as the frame is resized.