'The JTextfield doesn't show like I want
I'm new to Java and I can't find a simple solution. In the image below, you can see what my problem is. I want to write a program, where I can create playlists and safe them, but I am already stuck at the beginning because the JTextfield is too small.
import javax.swing.JFrame;
public class Main {
public static void main(String args[]) {
JFrame w = new Layout();
w.setVisible(true);
w.setSize(600, 600);
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
w.setLocationRelativeTo(null);
w.setResizable(true);
}
}
import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class Layout extends JFrame{
JTextField input;
Layout(){
//title and layout type
super("ThePlay");
setLayout(new FlowLayout());
JButton s; //search button
JButton h; //home button
JButton mp; //my playlist button
JButton cp; //create playlist button
JButton fs; //favorite songs button
//frame constructor
Container mainContainer = this.getContentPane();
mainContainer.setLayout(new BorderLayout(8,6));
mainContainer.setBackground(Color.GRAY);
this.getRootPane().setBorder(BorderFactory.createMatteBorder(4, 4, 4, 4, Color.GRAY));
//input constructor
input = new JTextField();
input.setBounds(300, 50, 150, 25);
//up frame
JPanel topPanel = new JPanel();
topPanel.setBorder(new LineBorder(Color.BLACK, 3));
topPanel.setBackground(Color.GRAY);
topPanel.setLayout(new FlowLayout(6));
//bot frame
JPanel downPanel = new JPanel();
downPanel.setBorder(new LineBorder(Color.BLACK, 3));
downPanel.setBackground(Color.GRAY);
downPanel.setLayout(new FlowLayout(5));
//create buttons
s = new JButton("Search");
h = new JButton("Home");
mp = new JButton("My Playlists");
cp = new JButton("Create Playlist");
fs = new JButton("Favorite Songs");
//add buttons to the frame
mainContainer.add(topPanel,BorderLayout.NORTH); //top frame location
mainContainer.add(downPanel,BorderLayout.CENTER); //bottom frame location
topPanel.add(input);
topPanel.add(s);
topPanel.add(h);
topPanel.add(mp);
topPanel.add(cp);
topPanel.add(fs);
}
}

Solution 1:[1]
You can give to the JTextField a size in this way:
input = new JTextField(20);
From the documentation:
The integer argument passed to the JTextField constructor, 20 in the example, indicates the number of columns in the field. This number is used along with metrics provided by the field's current font to calculate the field's preferred width. It does not limit the number of characters the user can enter.
Solution 2:[2]
When the int in the constructor is to big the JTextField will bug
I think it's depend of the size of the container.
here if i write 10 instead of 5 it's doesn't work:
Dimension dim = getPreferredSize();
dim.width = 150;
setPreferredSize(dim);
nameLabel = new JLabel("Name: ");
nameField = new JTextField(5);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | user18121801 |
