Adding a label for a JTextField - java

I have a JTextField and I want to add a label next to it, so that it looks like this...
+---------------+
TEAM 1: |Text field here|
+---------------+
This is the code where I am constructing the JTextFields...
jb = new JButton(">> FIGHT <<");
jt0 = new JTextField("", 25);
jt1 = new JTextField("", 25);
jt2 = new JTextField("<< BATTLE VICTOR >>", 35);
Could someone please tell me how to add the label.

Just put a JLabel next to your JTextField.

In addition to layout selection, don't forget that "you can improve your program's accessibility by using the setLabelFor() method," as discussed in How to Use Labels.

If you want to add any text to your interface, you should use JLabel. By placing a JLabel next to a JTextField, you can achieve the look you are asking for.
A JLabel is simply constructed like this...
JLabel myLabel = new JLabel("This is my message");
The way you add the JLabel to your interface depends on your JPanel/JFrame layout, but I tend to prefer BorderLayout, which you would use like this...
JLabel myLabel = new JLabel("Team 1");
JTextField myTextField = new JTextField("Team Awesome!");
JPanel panel = new JPanel(new BorderLayout());
panel.add(myLabel,BorderLayout.WEST);
panel.add(myTextField,BorderLayout.CENTER);
This will put a JLabel to the left (WEST) of the JTextField.
Refer to the following documentation for more information:
JLabel: http://docs.oracle.com/javase/6/docs/api/javax/swing/JLabel.html
BorderLayout: http://docs.oracle.com/javase/6/docs/api/java/awt/BorderLayout.html

You want to use a combination of a layout manager and JLabels
JLabel label = new JLabel("User name:");
JTextField field = new JTextField(12);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
add(label, gbc);
gbc.grid++;
add(field, gbc);
Check out How to Use Labels for more examples

Related

After stretch, the last panel is not stretched, but second last did in panel

Following Java's official tutorial:(https://docs.oracle.com/javase/tutorial/uiswing/components/table.html)
However, there is no source code for this, picture:
So I did it myself. My stupid idea is that there are 4 areas in here. So BorderLayout does not work well for me. Because I already tried BorderLayout.PAGE_END. It's not working. So I make one panel consolidate all 3 panels except the first scroll pane. It's inefficient, but works.
But now the issue is when I drag(stretch) the frame, the last text-area not stretched, but the second last selection option is stretched.
How to make the last text area stretch when I resize the frame?
Below is my code:
JRadioButton mulintselRadioButton = new JRadioButton("Multiple Interval Selection");
JRadioButton singleselRadioButton = new JRadioButton("Single Selection");
JRadioButton singleIIntSelRadioButton = new JRadioButton("Single Interval Selection");
JCheckBox rowSelection = new JCheckBox("Row Selection");
JCheckBox columnSelection = new JCheckBox("Column Selection");
JCheckBox cellSelection = new JCheckBox("cell Selection");
ButtonGroup G = new ButtonGroup();
ButtonGroup GButton = new ButtonGroup();
GButton.add(rowSelection);
GButton.add(columnSelection);
GButton.add(cellSelection);
G.add(mulintselRadioButton);
G.add(singleIIntSelRadioButton);
G.add(singleselRadioButton);
JTable table = new JTable(data, columnNames);
TableColumn column = null;
table.setPreferredScrollableViewportSize(new Dimension(400,70));
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
JPanel selectionMode = new JPanel(new GridLayout(4,1));
JPanel selectionOption = new JPanel(new GridLayout(4,1));
JPanel textAreaPanel = new JPanel(new GridLayout(1,1));
JPanel consolidatedPanel = new JPanel(new BorderLayout());
JLabel selcttionModeTitle = new JLabel("Selection Mode");
JLabel selcttionOptionTitle = new JLabel("Selection Options");
JTextArea textArea = new JTextArea("thsisaskjhkjk shial");
textAreaPanel.add(textArea);
selectionMode.add(selcttionModeTitle);
selectionMode.add(singleIIntSelRadioButton);
selectionMode.add(singleselRadioButton);
selectionMode.add(mulintselRadioButton);
selectionOption.add(selcttionOptionTitle);
selectionOption.add(rowSelection);
selectionOption.add(columnSelection);
selectionOption.add(cellSelection);
textArea.setText("hsknd hkcjshksdjl sldh RadioButton mulintselRadioButton = new JRadioButton(\"Multiple Interval Selection\");\n" +
" JRadioButton singleselRadioButton = new JRadioButton(\"Single Selection\");\n" +
" JRadioButton singleIIntSelRadioButton = new JRadioButton(\"Single Interval Selection\");\n" +
" JCheckBox rowSelection = new JCheckBox ");
add(scrollPane,BorderLayout.NORTH);
consolidatedPanel.add(selectionMode,BorderLayout.NORTH);
consolidatedPanel.add(selectionOption,BorderLayout.CENTER);
consolidatedPanel.add(textAreaPanel,BorderLayout.SOUTH);
add(consolidatedPanel);
Following the Java's official tutorial (the link of which you have posted in your question), one can read that it says that you can consult the example index... If you follow this link you can find a table which gives you the link to the file you are searching for. Specifically the code you are looking for is in the following link:
https://docs.oracle.com/javase/tutorial/uiswing/examples/components/TableSelectionDemoProject/src/components/TableSelectionDemo.java
As for your question, you can use a BoxLayout to achieve what you are asking, exactly as the TableSelectionDemo does. The result can strech the table and the text area, but not the middle section.

Java BoxLayout use full width

I am creating a messaging program that has multiple chats. On the side of the chat window there is a JPanel containing a List Array of all the buttons to switch chats.I am having trouble getting all the buttons in the side panel to be the same width, no matter what they contain. But whatever I try doesn't seem to work and I am looking for some help. Please can you explain what the code does and how it can be used so I can learn it for next time. Sorry if the comments aren't the best it's a work in progress as I wait till code works before adding detailed comments otherwise I am constantly changing them. :(
This is what it looks like without any chats:
This is with multiple chats and you can see the width variation
Here is the code that is run when a new user is added:
public void newUser() {
JPanel dialogue = new JPanel();
dialogue.setLayout(new BoxLayout(dialogue, BoxLayout.Y_AXIS));
//Creating/adding dialogue components
JLabel Enter_ip = new JLabel("Enter the ip address");
JTextField Get_ip = new JTextField("");
dialogue.add(Enter_ip);
dialogue.add(Get_ip);
dialogue.add(Box.createHorizontalStrut(15));
JLabel Enter_name = new JLabel("Enter the user's name");
JTextField Get_name = new JTextField("");
dialogue.add(Enter_name);
dialogue.add(Get_name);
//Creating the dialogue box
JOptionPane.showConfirmDialog(null, dialogue, "New User", JOptionPane.OK_CANCEL_OPTION);
//Getting data from dialogue box
String ip = Get_ip.getText();
String name = Get_name.getText();
//Try connecting to other user here
//Adding user message data
int size = Users_Messages_Data.size();
Users_Messages_Data.add(new ArrayList());//New user
Users_Messages_Data.get(size).add(new ArrayList());//Messages
Users_Messages_Data.get(size).add(new ArrayList());//Details
Users_Messages_Data.get(size).get(1).add(name);
Users_Messages_Data.get(size).get(1).add(ip);
Users_Messages_Data.get(size).get(1).add("port number");
//adds new UserButton
int temp = users.size();
users.add(new JButton(Users_Messages_Data.get(size).get(1).get(0)));
users.get(temp).addActionListener(this);
users.get(temp).setSize(new Dimension(500, 500));
SelectUser.add(users.get(temp), gbc);
Messaging.revalidate();
pack();
}
and here is the initialisation method:
public void MessagingGUI() {
//Creates JFrame and pane
Messaging = new JFrame();
Container pane = getContentPane();
JLabel info = new JLabel("29/07/2016 15:36");
//Creates user chats panel
SelectUser = new JPanel(new GridBagLayout());
gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = GridBagConstraints.REMAINDER;
SelectUser.setLayout(new BoxLayout(SelectUser, BoxLayout.Y_AXIS));
SelectUser.setSize(new Dimension(500, 500));
//Adds different chats
users = new ArrayList<JButton>();
int x;
for (x = 0; x < Users_Messages_Data.size(); x++) {
users.add(new JButton(Users_Messages_Data.get(x).get(1).get(0)));
users.get(x).addActionListener(this);
SelectUser.add(users.get(x), gbc);
}
JButton newUser = new JButton("+");
newUser.addActionListener(this);
SelectUser.add(newUser);
JScrollPane UserScroll = new JScrollPane(SelectUser,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//Creates messages feed
JPanel Messages = new JPanel();
Messages.setLayout(new BoxLayout(Messages, BoxLayout.Y_AXIS));
AllMessages = new JTextArea(10, 30);
AllMessages.setBackground(Color.WHITE);
AllMessages.setEditable(false);
AllMessages.setBorder(BorderFactory.createLineBorder(Color.BLUE, 1));
JScrollPane MessageScroll = new JScrollPane(AllMessages,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//Creates user text entry box
UserText = new JTextArea(5, 30);
//UserText.setLineWrap(true);
//UserText.setWrapStyleWord(true);
UserText.setBorder(BorderFactory.createLineBorder(Color.CYAN, 1));
UserText.setText("Enter Message. Press enter to send");
UserText.setFocusable(true);
UserText.addKeyListener(this);
UserText.setPreferredSize(new Dimension(5, 20));
//Adds all components to pane
Messages.add(info);
Messages.add(MessageScroll);
Messages.add(UserText);
pane.add(UserScroll, BorderLayout.WEST);
pane.add(Messages, BorderLayout.CENTER);
//JFrame setup
Messaging.setTitle("Messaging");
Messaging.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Messaging.setContentPane(pane);
Messaging.setVisible(true);
Messaging.setSize(400, 350);
Load_User(current_user);
}
First of all, variable names should NOT start with an upper case character. Some of your variable names are correct, other are not. Be consistent and follow Java conventions!!!
.I am having trouble getting all the buttons in the side panel to be the same width,
Don't use a BoxLayout. It does not automatically resize on the opposite axis of the layout.
Instead you can use:
a GridLayout to make all the buttons the same size. The GridLayout will also fill the area vertically which is not what you want so you will need to nest panels. So create a parent panel using a BorderLayout. Add your panel using the GridLayout with the buttons to the BorderLayout.PAGE_START of this parent panel. Then add the BorderLayout panel to the scroll pane.
a GridBagLayout. You will need to use the "fill" contstraint to have the component fill the width of the cell.
Read the Swing tutorial on Layout Managers for more information and examples.
Or maybe a different approach is to use a JList to display the users.

How to set left alignement JLabel in JPanel

I have a JPanel that I insert into my JFrame.
Now, I want to display a JLabel on the left at my JPanel. So I have this code:
JPanel panelHeader = new JPanel();
panelHeader.setBackground(new Color(52,151,236));
panelHeader.setPreferredSize(new Dimension(300,25));
GridBagConstraints GBC = new GridBagConstraints();
Container CR = new Container();
GridBagLayout GBL = new GridBagLayout();
CR.setLayout(GBL);
panelHeader.add(CR);
LabelFormat label = new LabelFormat("EasyManagement",new Font("Arial Narrow", Font.PLAIN, 16),Color.white);
GBC = new GridBagConstraints();
CR.add(label);
GBC.gridx=0;
GBC.gridy=0;
GBC.insets.left = 1;
GBC.insets.top=0;
GBC.fill = GridBagConstraints.BOTH;
GBC.anchor= GridBagConstraints.EAST;
GBL.setConstraints(label,GBC);
With this code, I'm not able to display the JLabel at the left on JPanel but I see it on the center.
How can I fixed it?
-You might wish to set the JLabel's horizontalAlignment property. One way is via its constructor. Try:
JLabel label = new JLabel(lText, SwingConstants.LEFT);
This can also be done via the expected setter method:
label.setHorizontalAlignment(SwingConstants.LEFT);
-You can also try using LEFT alignment for your JLabel JPanel container
myJPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
-Final way :
You could wrap the label in JPanel with FlowLayout.LEADING
JPanel wrapper = new JPanel(new FlowLayout(FlowLayout.LEADING,0, 0));
wrapper.add(Jlabel);
panel.add(wrapper);
Also remember to follow Java naming convention. variables begin with lower case letters using camel casing: Jlabel → jLabel

Make TextField normal size using LayoutManagers

In the image above, this is what my GUI looks like, i have a problem fixing up the textfield as it takes up the most percentage of space in my frame, and i want to correct the error I have.
I know layout managers is a pain in the ass, but does anyone know how i can make the text field smaller so that my frame looks nicer.
I am using jpanels to hold most of my components
Here is the necessary part of my code
panel1 = new JPanel();
panel1.setLayout(new GridLayout());
frame.getContentPane().add(panel1, BorderLayout.NORTH);
label2 = new JLabel("Score: " + score);
label2.setFont(new Font("SimSun", Font.BOLD, 22));
label2.setLocation(0,0);
label2.setSize(117,37);
panel1.add(label2);
label3 = new JLabel("Lives: " + lives);
label3.setFont(new Font("SimSun", Font.BOLD, 22));
label3.setLocation(0,70);
label3.setSize(105,49);
panel1.add(label3);
panel2 = new JPanel();
panel2.setLayout(new BorderLayout());
frame.getContentPane().add(panel2, BorderLayout.CENTER);
label1 = new JLabel(a +" + " +b);
label1.setFont(new Font("SimSun", Font.BOLD, 22));
label1.setHorizontalAlignment(SwingConstants.CENTER);
Border paddingBorder = BorderFactory.createEmptyBorder(10,10,10,10);
label1.setBorder(BorderFactory.createCompoundBorder(null,paddingBorder));
label1.setLocation(249,105);
label1.setSize(183,60);
panel2.add(label1, BorderLayout.NORTH);
box1 = new JTextField();
box1.setPreferredSize(new Dimension(50, 50));
box1.setLocation(249,176);
box1.setSize(96,25);
panel2.add(box1, BorderLayout.CENTER);
panel3 = new JPanel();
panel3.setLayout(new GridLayout());
frame.getContentPane().add(panel3, BorderLayout.SOUTH);
button1 = new JButton("Answer");
panel3.add(button1);
button1.addActionListener(this);
button1.setLocation(187,236);
button1.setSize(105,50);
button2 = new JButton("Reset");
panel3.add(button2);
button2.addActionListener(this);
button2.setVisible(false);
button2.setLocation(302,236);
button2.setSize(105,50);
The reason for this is you are adding your JTextField to BorderLayout.CENTER, which will stretch it to fill all the space available to this particular layout manager. Although I am not exactly sure how you want your GUI to look, perhaps a simple FlowLayout (the default layout) or a BoxLayout coupled with setAlignmentX(Component.CENTER_ALIGNMENT); on the JTextField will solve your problem.
You can find more information on layouts here: https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
Additionally, note that you shouldn't be using things like setLocation() as the point of the layout manager is to allow it to choose how components should be laid out on the screen, and as such the layout manager may ignore this as well as methods like setSize.

how do you increase panel width?

With this code I will have the following window. I created 2 panels and added the mainp one to the frame and the panel to the mainp I did this in order to make window resizing dynamic (so the panel wont resize to the frame) I tried making my default panel size wider so that the text fields and label become wider but panel.setsize doesn't seem to do anything.
// creates the labels
studId = new JLabel("Student ID");
studAvg = new JLabel("Student Average");
studName = new JLabel("Student Name");
// creates the text fields
JTextField studIdText = new JTextField();
JTextField studAvgText = new JTextField();
JTextField studNameText = new JTextField();
JPanel mainp = new JPanel();
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2, 2, 2));
panel.setSize(300, 100);
// adds to the GridLayout
panel.add(studId);
panel.add(studIdText);
panel.add(studName);
panel.add(studNameText);
panel.add(studAvg);
panel.add(studAvgText);
mainp.add(panel);
add(BorderLayout.CENTER,mainp);
// verifies the textfields
studIdText.setInputVerifier(new IntVerifier());
studAvgText.setInputVerifier(new DoubleVerifier());
setTitle("Student Form");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
The method you are looking for is setPreferredSize. Use it instead of panel.setSize(300, 100).
panel.setPreferredSize(new Dimension(300, 100));
I would also recommend to not setting the size of your JFrame to the fixed value (300,200) but do pack() instead. This will set the size of your JFrame to fit the panels inside.
using Advise from #Dan and #MADprogrammer and #trashgod i came up with the following
JTextField studIdText = new JTextField(20);
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints r1 = new GridBagConstraints();
r1.fill = GridBagConstraints.HORIZONTAL;
r1.weightx = 0.0;
r1.gridx = 0;
r1.gridy = 0;
panel.add(studId,r1);
r1.weightx = 0.5;
r1.gridx = 1;
r1.gridwidth = GridBagConstraints.REMAINDER;
panel.add(studIdText,r1);
of course you can make GridBagConstraints for every row and just change the gridy
Set the layout for mainp as BorderLayout.
mainp.setLayout(new BorderLayout());
Then, in order to avoid having the textfields resize vertically and look strange, you can add panel to BorderLayout.NORTH, for instance.
mainp.add(panel, BorderLayout.NORTH);

Categories