Setting the preferred size of a JTextFieldis not working - java

I'm trying to set the size of JTextField, but for some reason it stays the same size and fills up the whole JPanel, I am using setPreferredSize, but this makes no difference:
JPanel loginJPanel = new JPanel(new BorderLayout());
JTextField usernameJTextField = new JTextField();
usernameJTextField.setPreferredSize(new Dimension(50, 100));
loginJPanel.add(usernameJTextField);

It does make a difference, but the layout may choose to ignore preferred size settings. The center area of BorderLayout gets as much of the available space as possible. See How to Use BorderLayout for more details.
Consider this example that packs the frame, as a result the preferred size of the text field is respected. But once the frame is resized, the text field is resized as well.
import javax.swing.*;
import java.awt.*;
class Demo {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JPanel loginJPanel = new JPanel(new BorderLayout());
JTextField usernameJTextField = new JTextField();
usernameJTextField.setPreferredSize(new Dimension(50, 100));
loginJPanel.add(usernameJTextField);
JFrame frame = new JFrame();
frame.add(loginJPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
});
}
}
Take a look at Visual Guide to Layout Managers and perhaps you would find a more suitable layout for your needs.
Also, see Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?.
EDIT:
Note that you're usually encouraged to specify the number of columns when initializing text fields. This number is used to calculate preferred width. For example textField = new JTextField(20); See How to Use Text Fields for more details:
If you do not specify the number of columns or a preferred size, then
the field's preferred size changes whenever the text changes, which
can result in unwanted layout updates.

Since you set layout manager of your jpanel to BorderLayout, it adds jtextfield to center by default. Use a null layout instead.
JPanel loginJPanel = new JPanel(null);

Related

How to automatically resize the remaining elements after adding a BorderLayout?

I added a simple statusBar to the application. It works normally, adjusts its size depending on the resolution.
Only that by adding this status bar, suddenly other elements in the application, such as text fields or table, lost this ability to "adjust elements".
If we remove these three lines in the code, I will lose the status bar, but the other elements will adapt as I want.
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(statusBar, BorderLayout.SOUTH);
How can I make both statusBar and the other elements adjust to size?
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.sql.SQLException;
public class Menu {
static void MenuBar() throws SQLException, IOException {
JFrame frame = new JFrame("frame");
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
frame.setSize(1600, 1000);
frame.setVisible(true); //If I delete any more, all the content will disappear
frame.setContentPane(new Recipe().Main);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
JStatusBar statusBar = new JStatusBar();
JLabel leftLabel = new JLabel("App");
statusBar.setLeftComponent(leftLabel);
final JLabel timeLabel = new JLabel();
timeLabel.setHorizontalAlignment(JLabel.CENTER);
statusBar.addRightComponent(timeLabel);
contentPane.add(statusBar, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
As a proof of concept, I modified your code slightly to show you what I meant:
public class Menu {
void MenuBar() throws SQLException, IOException {
JFrame frame = new JFrame("frame");
JMenuBar menuBar = new JMenuBar();
menuBar.add(new JMenu("Main Menu"));
frame.setJMenuBar(menuBar);
frame.setSize(600, 400);
JPanel center = new JPanel();
center.add(new JLabel("CENTER"));
Container contentPane = frame.getContentPane();
contentPane.add(center, BorderLayout.CENTER);
JPanel south = new JPanel();
south.add(new JLabel("SOUTH"));
south.setBackground(Color.green);
JLabel leftLabel = new JLabel("App");
// statusBar.setLeftComponent(leftLabel);
final JLabel timeLabel = new JLabel();
timeLabel.setHorizontalAlignment(JLabel.CENTER);
// statusBar.addRightComponent(timeLabel);
contentPane.add(south, BorderLayout.SOUTH);
JPanel east = new JPanel();
east.add(new JLabel("EAST"));
east.setBackground(Color.cyan);
contentPane.add(east, BorderLayout.EAST);
JPanel west = new JPanel();
west.add(new JLabel("WEST"));
west.setBackground(Color.yellow);
contentPane.add(west, BorderLayout.WEST);
JPanel north = new JPanel();
north.add(new JLabel("NORTH"));
north.setBackground(Color.orange);
contentPane.add(north, BorderLayout.NORTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
Menu menu = new Menu();
try {
menu.MenuBar();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
The code above produces the following result (not calling pack() to respect the set size of the frame)
As you can see from the code above, I am not adding a new BorderLayout to the frame's content pane because it already contains one by default when you instantiate the content pane via the JFrame. I am simply using it and setting the components in the different regions designated by this layout manager.
Resizing the frame
Different layout managers enforce different behaviors during a resize event. It all depends on the layout manager. For instance, JPanel uses FlowLayout by default. Depending on the size when the panel renders, you will see components inside a JPanel arranged from left to right. When there is no more space (defined by the bounds of the panel), you will see components arranged on a "row" below the first components. This means that, if you reduce the panel's width during a resize, you should see components wrapped and stacked one on top of others inside this panel. In contrast, if you use a GridLayout and resize the panel, the behavior is totally different. You can even have restrictions to set a minimum and/or maximum sizes to constraint much much or how little a component should resize and a preferred size attribute which is different to the "size" attribute of a container when "no layout manager" is preferred. This is called "Absolute Positioning" and it is done when you want full control of the actual (x, y) coordinates where the components should be placed and the layout manager is set to null. Of course, this is not preferable. It is always better (in my opinion) to delegate this responsibility to the various layout managers that comes with Java Swing, or write one of your own (writing one might be easier said than done). I said all that to say the same thing I posted on the comments section:
Spend some time reading the Oracle Tutorials on Layout Managers to get a glimpse of what Layout Managers come by default with different Swing components.
This will also help you understand what behavior to expect when (for instance) a resize event occurs and the things you need to do in the code to adjust this behavior to suit your needs. For example, if you need to create a checkers or chess application, it is obvious you will need a container with a GridLayout manager. But it is up to you to tweak it to define min and max size, preferred size, etc.
Also, something very important mentioned by Andrew Thompson... you need to know WHEN to make calls to certain methods. He specifically mentioned when to call pack() and setVisible(). Little things like that (are not really little) can make a difference.

Second JPanel and JTexField look too cramped

I have an application where I use two JPanels. One of them is a PaintPanel. The second panel,the jtextfield and the jtextarea work fine but they look too cramped against the right side. I tried changing the sizes with setSize() but it didn't work.
The code for the paintpanel
public void center() {
jpCenter = new PaintPanel();
jpCenter.addMouseListener(this);
jpCenter.setSize(100, 100);
jpCenter.setBackground(Color.white);
add(jpCenter, BorderLayout.CENTER);
}
The code for the panel of the chatbox
public void east() {
// CREATE EAST Panel
gl = new GridLayout(4, 1);
jpEast = new JPanel();
jpEast.setSize(200, 200);
jpEast.setLayout(gl);
jpEast.setBackground(Color.white);
label = new JLabel("Number of shapes: ");
jpEast.add(label);
// ADD TEXT FIELD
jtf = new JTextField();
jtf.setText("");
jtf.setSize(200, 200);
jpEast.add(jtf);
// ADD BUTTON
jbSend = new JButton("Send");
jbSend.setEnabled(false);
jbSend.setSize(20, 60);
jpEast.add(jbSend);
jbSend.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
send(jtf.getText());
jtf.setText("");
}
});
// ADD TEXT AREA
jta = new JTextArea("");
jta.setSize(100, 100);
jpEast.add(jta);
// ADD EAST panel
add(jpEast, BorderLayout.EAST);
}
Avoid setting the size of components since it can make them not work well on all platforms, and with JTextArea in particular, it will not allow it to expand correctly if held within a JScrollPane (which is where a JTextArea belongs). Note also that most layout managers don't even respect a component's size but rather its preferred size.
Instead, set the row and column properties of your JTextAreas (done most easily via the JTextArea(int row, int column) constructor), the column property of your JTextField, the font sizes of other components (if need be). Then allow your container (JPanel) layout managers and component's own preferred sizes size all appropriately when you call pack() on your top-level window (often a JFrame), after adding all components but before setting it visible.
For more specific help, consider posting an image of the GUI you're getting vs. the one you're trying to achieve.

textfield not shown with proper size

I am new to Java and am trying to run this code. What could be error in the following code—my JTextField, txtfld, is shown just as a line instead of as a full text box?
public class calculator
{
public static void main(String s[])
{
JFrame j=new JFrame();
j.setSize(400,600);
JPanel p1=new JPanel();
JPanel p2=new JPanel();
p1.setSize(400, 100);
p2.setSize(400, 500);
p1.setLocation(0, 0);
p2.setLocation(0, 100);
p2.setLayout(new GridLayout(4,4));
j.add(p1);
j.add(p2);
JTextField txtfld=new JTextField();
txtfld.setSize(390, 92);
txtfld.setLocation(5, 2);
//txtfld.setVisible(true);
p1.add(txtfld);
j.setVisible(true);
}
}
JTextField txtfld=new JTextField();
You need to give a hint to the layout manager what the size should be.
So you should use something like:
JTextField txtfld=new JTextField(10);
Now the preferred size will be such that 10 "W" characters can be displayed in the text field before scrolling is required.
You should also pack() the frame before making it visible:
j.pack();
j.setVisible();
This will allow the frame to display all the components at their preferred sizes.
Also, get rid of all the setSize() and setLocation() statements. It is the job of the layout manager to set the size and location. Those values will be recalculated by the layout manager.
j.add(p1);
j.add(p2);
The default layout manager for a frame is a BorderLayout. So the above code will cause p2 to replace p1 on the frame.
Basically your entire code is wrong.
Start by reading the section from the Swing tutorial on How to Use Layout Managers for more information and working examples to get you started. Maybe start with the BorderLayout example, since this is the default layout of the frame you need to understand how it works first.

Java Swing JTextArea not displaying correctly

I am trying to use Java Swing to create a simple GUI in which I have a drawing pad and some buttons it all works fine until I add this code for the JTextField:
String text = "hello";
JTextArea textArea = new JTextArea(text);
textArea.setPreferredSize(new Dimension(500, 50));
textArea.setEditable(false);
Before adding this code the drawpad displays on the left of the screen followed by the buttons, when I add this only the drawpad is displayed unless I resize the frame in which case the buttons and text field reappear although the text field is hidden behind the drawpad slightly. Here is the full code:
public class testGUI extends Frame{
public static void main(String[] args) {
JFrame frame = new JFrame("Neural Networks");
frame.setSize(700, 300); //set the size of the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true); //make it visible
Container content = frame.getContentPane();
content.setLayout(new BorderLayout());
JPanel mainPanel = new JPanel();
final PadDraw drawPad = new PadDraw();
drawPad.setSize(100, 100);
content.add(drawPad);
JButton clearButton = new JButton("Clear");
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.clear();
}
});
JButton loadButton = new JButton("Load");
loadButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//Load something here
}
});
JButton testButton = new JButton("Test Draw Pad Image");
testButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//
}
});
JButton loadImage = new JButton("Test image from file");
loadImage.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//String filename = textField.getText();
}
});
String text = "hello";
JTextArea textArea = new JTextArea(text);
textArea.setPreferredSize(new Dimension(500, 50));
textArea.setEditable(false);
mainPanel.add(clearButton);
mainPanel.add(loadButton);
mainPanel.add(testButton);
mainPanel.add(loadImage);
mainPanel.add(textArea);
content.add(mainPanel);
}
}
You're adding the drawPad and the mainPanel to the content panel, which uses BorderLayout, without specifying any location. They thus end up both in the center position of the border layout, which is supposed to contain only one component.
See How to use BorderLayout in the Swing tutorial.
Also note that setting the preferred size is not something you should do. Instead, the preferred size is supposed to be automatically computed based on other sttings (the contained components, the number of rows and columns of a text area, etc.)
And a JTextArea should be enclosed into a JScrollPane to be good-looking and allow you to scroll.
JPanel mainPanel = new JPanel();
The default layout for a JPanel is a FlowLayout, so all the components flow on a single row. If there is not enough room on the row then the components wrap to the next row.
So when you add the JTextArea the flow is disturbed. The solution is to use a combination of layout managers to get your desired layout effect. Read the section from the Swing tutorial on Using Layout Managers for more information and examples.
JTextArea textArea = new JTextArea(text);
textArea.setPreferredSize(new Dimension(500, 50));
Also, you should NOT set the preferred size of the text area (or any Swing component for that matter). Instead you should do something like:
JTextArea textArea = new JTextArea(rows, columns);
and let the component determine its own preferred size. Also a text area is typically used with a JScrollPane and then you add the scroll pane to your panel:
JScrollPane scrollPane = new JScrollPane( textArea );
Edit:
Taking a second look at your code you have many more problems.
The point of using a layout manager is to have the layout manager set the size and location of the components. So your code should not have any logic related to the size/location of a component.
When you use the add(...) statement on a BorderLayout without a constraint, the component gets added to the CENTER. However only the last component added is managed by the BorderLayout. So only the "mainPanel" is given a size/location by the layout manager. That is why you need the setSize(...) statement on the drawPad to make the component visible. Although you now have the problem that two components are painted in the same space.
So to see the drawPad on the left you might want to use:
content.add(drawPad.BorderLayout.LINE_START);
However this still probably won't work because I'm guessing you are doing custom painting on the draw pad which means you will also need to override the getPreferredSize() method of the class so the layout manager can use the information to determine the size of the component. Read the section from the Swing tutorial on Custom Painting for more information and working examples.
Finally some other issues:.
The setVisible(...) statement should be invoked AFTER all the components have been added to the frame.
To follow Java standards, class names should start with an upper case character.
You should NOT be extending "Frame". There is no need to extend any class in your example.
Read the tutorial and download the demos for examples of better structured code.

How do I change a JButton height if I'm using the seaglass look and feel?

I'm using the seaglass look and feel in my application. How do I set the height for a JButton?
It seems like there is no way to have a custom height. I took a look at the documentation:
http://seaglass.googlecode.com/svn/doc/client-properties.html
and I tought I had to use JComponent.sizeVariant = scale to solve the problem, but it doesn't work.
How can I solve this?
Thanks trashgod, I tried this:
try {
UIManager.setLookAndFeel("com.seaglasslookandfeel.SeaGlassLookAndFeel");
} catch(Exception e) {
e.printStackTrace();
}
JButton button = new JButton("Test");
button.putClientProperty("JComponent.sizeVariant", "scale");
JPanel panel = new JPanel(new BorderLayout());
panel.add(button);
panel.setPreferredSize(new Dimension(500, 400));
JFrame frame = new JFrame();
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
but doesn't seem to work either. Thanks anyway for your effort.
As shown here, here and here, the JComponent.sizeVariant property value is a String, e.g. "mini", "small", "regular" and "large". For "scale" to work, the enclosing panel's layout must allow the component to resize. In the variation below, GridLayout is used:
f.add(variantPanel("scale"));
…
private static JPanel variantPanel(String size) {
JPanel variantPanel = new JPanel(new GridLayout());
…
return variantPanel;
}
Your frame contains one panel. When you pack the frame the panel will be sized at 500x400, i.e., its preferred size. Your panel uses a BorderLayout. BorderLayout does not use the button preferred size. It will expand the button to fill the entire panel size. Try using a layout for your panel that respects the preferred size of its components, e.g., FlowLayout. If you want to change the button size you can then set the button's preferred size.

Categories