I am trying to make a 2x2 grid layout that has a JLabel on the top left, and three buttons on the other three spaces. When I do this, I get the unexpected result of one big button (filling up the entire JDialog) that says "Do you want to push me". I don't know why this result shows up, please help, Thanks!
public void sinceyoupressedthecoolbutton() {
JDialog replacementwindow = new JDialog(); //Like a window
JButton best = new JButton("best");
JButton first = new JButton("FIRST");
JButton second = new JButton("Second");
replacementwindow.setLayout(new GridLayout(2,3,0,0)); //Row, column, distance horizontally, distance vertical
JPanel panel = new JPanel();
replacementwindow.add(panel); //adding the JPanel itself
replacementwindow.add(first);
replacementwindow.add(second);
replacementwindow.add(best);
replacementwindow.setSize(500, 500);
replacementwindow.setTitle("NEW WINDOW!");
replacementwindow.setVisible(true);
}
It's because you set the layout of your JButton, and not of your JDialog
Change
label.setLayout(new GridLayout(2,2,0,0));
to
YES.setLayout(new GridLayout(2,2,0,0));
Also, your variable called label is a JButton, you probably want to change that.
Don't add components to a button. You add components to a panel.
So the basic code should be:
JDialog dialog = new JDialog(...);
JPanel panel = new JPanel( new GridLayout(...) );
panel.add(label);
panel.add(button1);
...
dialog.add(panel);
Also, variable names should NOT start with an upper case character! "Yes" does not follow Java standards. The other variables do. Be consistent!
Related
I have a very big problem. I need to do vertical menu in the center of window. What could be easier? What I did:
I create JFrame and set BorderLayout to it:
JFrame jfr = new JFrame("Frame");
Then I create 4 buttons:
JButton b1 = new JButton("b1");
JButton b2 = new JButton("b2");
JButton b3 = new JButton("b3");
JButton b4 = new JButton("b4");
I created panel and add all buttons to panel:
JPanel jpan = new JPanel();
jpan.setLayout(new BoxLayout(jpan, BoxLayout.Y_AXIS));
jpan.add(b1);
jpan.add(b2);
jpan.add(b3);
jpan.add(b4);
Aligned all buttons
b1.setAlignmentX(JComponent.CENTER_ALIGNMENT);
b2.setAlignmentX(JComponent.CENTER_ALIGNMENT);
b3.setAlignmentX(JComponent.CENTER_ALIGNMENT);
b4.setAlignmentX(JComponent.CENTER_ALIGNMENT);
And add panel to JFrame
jfr.add(jpan, BorderLayout.CENTER);
Please help me to understand this layouts!
Only say like this: "You should use this, when you use this layout"
And now main question: How can I change size of buttons?
There are a number of easy ways to change the size of buttons:
Give the buttons an icon of a different size.
Make the font of the buttons a different size.
Set more / less space between the button contents (the icon and text) and the border of the button.
Give the buttons more / less text.
Given the last is quite arbitrary, here is a demonstration of the first 3 techniques:
I'm trying to fix the height of the "amountField" text field, but I can't.
I would like the height of amountField to have the same height as the JComboBox that it's above, so it looks better.
Right now, the JTextField looks very tall compared with the rest of design.
I've tried everything that I've read in this forum, but nothing seems to work.
I don't know if it's relevant, but this whole JPanel (WithdrawalScreen) is inside another JPanel with BorderLayout. This panel is the center part of it
Thanks
PictureHere
public class WithdrawalScreen extends JPanel {
Public JPanel init() {
this.setLayout(new GridLayout(0,1));
account = new JLabel("account");
accountSelect = new JComboBox(labels);
amount = new JLabel("amount");
amountField = new JTextField("");
submit = new JButton("SUBMIT");
this.add(account);
this.add(accountSelect);
this.add(amount);
this.add(amountField);
this.add(submit);
return this;
}
}
Try creating the Grid Layout with 5 rows and 1 column. I think the height is messed up because you are not setting the constructor arguments properly.
new GridLayout(5,1);
Grid layout will stretch the component and give the same size to all of its components. In order to keep the "default" size of each component, you can use BoxLayout with BoxLayout.Y_AXIS parameter in its constructor. Another way would be to use a dummy-nested JPanel with another layout. Let's say FlowLayout.
JTextField textField = new JTextField(10);
JPanel nestedPanel = new JPanel(new FlowLayout());
nestedPanel.add(textField);
gridLayoutPanel.add(nestedPanel);
JTextField will not be stretched. nestedPanel will be. Do some experiments yourself and you will find the way that fits your needs.
A link that will help you: A visoual guide to Layout Managers.
So i lost some hours already with this and i can't seem to find a solution.
Basically i have a Jframe and inside, i have a Scrollpane and a panel
I have 1 Jlabel, 1 JTextField and 1 JButton inside that panel in a single line.
The JButton can add a new JLabel, a new JTextField and a new JButton, but i can't get them to be positioned in the next line.
I have been messing around with the layouts, but none of them fits my needs, and unfortunaly i never understand or learned how the GUI of java Works.
How's the best way to just keep adding those componentes (Jlabel, Jtextfields and Jbuttons) on a next line for every click i made?
This is my code:
private void BtnaddvariableActionPerformed(java.awt.event.ActionEvent evt) {
JLabel Lblvariablextra = new JLabel("Testing");
PanelVariable.add(Lblvariablextra);
ScrollPaneVariable.setViewportView(PanelVariable);
}
The code only contains an exemple of the label tough.
Create a main panel that is added to the scroll pane when the GUI is created:
Box main = Box.createVerticalBox();
scrollPane.setViewportView( main );
Then in the ActionListener you create a child panel contain the 3 components every time the button is pressed:
JPanel child = new JPanel();
child.add( new JLabel("I'm a label") );
child.add( new JTextField(10) );
child.add( new JButton("Click Me") );
main.add(child);
Read the section from the Swing tutorial on Layout Manager to understand how layout management works.
This question already has an answer here:
How can I use BoxLayout to do this?
(1 answer)
Closed 9 years ago.
I have a JPanel, and I want to add JRadioButtons to it, this is the code I tried :
private void populateQuestionnaire(Question question){
buttonGroup = new ButtonGroup();
for(Choix c : question.getListChoix()) {
radioButton = new JRadioButton(c.getChoixLibelle());
buttonGroup.add(radioButton);
jPanel1.add(radioButton);
}
jPanel1.revalidate();
jPanel1.repaint();
}
And I have the layout of JPanel is FlowLayout.
This is how the JRadioButtons displayed :
I want JRadioButtons to be added one below the other and to be centered in the JPanel.
Instead of using a FlowLayout, which lays out items left to right and wraps appropriately, you can use a BoxLayout, which allows you to specify laying out items either horizontally or vertically.
You can set the LayoutManager for your JPanel at construction:
JPanel jpanel1 = new JPanel(new BoxLayout(parentComponent, BoxLayout.Y_AXIS));
BoxLayout is great for stacking elements on top of each other. Consider this code:
public class MyFrame extends JFrame {
public MyFrame() {
ButtonGroup bg = new ButtonGroup();
JRadioButton b1 = new JRadioButton("My Button 1");
JRadioButton b2 = new JRadioButton("My Button 2");
bg.add(b1);
bg.add(b2);
BoxLayout bl = new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS);
this.setLayout(bl);
b1.setAlignmentX(CENTER_ALIGNMENT);
b2.setAlignmentX(CENTER_ALIGNMENT);
this.add(b1);
this.add(b2);
}
}
Which makes, when instantiated and shown, the following window:
Now let's look at how this code works:
Consider this code:
ButtonGroup bg = new ButtonGroup();
JRadioButton b1 = new JRadioButton("My Button 1");
JRadioButton b2 = new JRadioButton("My Button 2");
bg.add(b1);
bg.add(b2);
This code does the same thing you were doing before, only a little simpler for example's sake. It creates a button group and two JRadioButtons, then adds the buttons to the button group. Now here is when it gets interesting.
Next, consider this code:
BoxLayout bl = new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS);
this.setLayout(bl);
The first line creates a new BoxLayout with the following parameters:
1 The container which it is laying out. (It needs this because it can't be shared.)
2 The axis which it should be laying out components. (You want Y-AXIS for your case.)
The second line set's the JFrame's contentPane's layout to the BoxLayout you just created.
Finally, consider this code:
b1.setAlignmentX(CENTER_ALIGNMENT);
b2.setAlignmentX(CENTER_ALIGNMENT);
this.add(b1);
this.add(b2);
This sets the alignment of the two radio buttons so that their centers will be aligned to each other and to the center of the frame. Then it adds them to the frame's content pane.
Hope this helped!
Note: The reason I used this.getContentPane() while constructing the BoxLayout instead of just using this is because when working with JFrames commands like add() and setLayout() get redirected to the frame's content pane. So if we were to use this in the constructor, when we called this.setLayout(bl) we really would be calling this.getContentPane().setLayout(bl). But, we just told the BoxLayout it'll be laying out the frame, not it's content pane, so you'll get an exception saying that the BoxLayout can't be shared. To correct the error, we just need to realise that we are actually working with the content pane through the frame's methods, and update the BoxLayout's constructor accordingly to let it know what it really is laying out.
So I've been learning Java for the very first time and it's time for me to attempt my first project. And I'm stuck at the "first hurdle" haha.
The issue I have is the fact that I don't actually know how to space J Items apart.
I have a 250,350 window for a Log In form with a JLabel, a JTextField for username and JLabel JPassword for Password with a JButton at the bottom.
What I want to do now is style it so that the spacing between the top and the bottom of the form makes it so that the form is centered as well as adding a line's height space between the JLabel and the JTextField. (Basically a \n type deal but that isn't working.)
Hopefully this makes sense, if not, I apologise and I'll try to rephrase/add code!
public Game() {
this.setSize(250,350);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Sticket Cricket - Login");
JPanel loginMenuPanel = new JPanel();
loginButton = new JButton("Login");
usernameField = new JTextField();
usernameField.setColumns(10);
passwordField = new JPasswordField();
passwordField.setColumns(10);
passwordField.requestFocus();
usernameLabel = new JLabel("Username: ");
passwordLabel = new JLabel("Password: ");
this.add(loginMenuPanel);
loginMenuPanel.add(usernameLabel);
loginMenuPanel.add(usernameField);
loginMenuPanel.add(passwordLabel);
loginMenuPanel.add(passwordField);
loginMenuPanel.add(loginButton);
this.setVisible(true);
}
Short Answer:
Create a JPanel, set the layoutmanger of the panel (some examples, GridLayout, BorderLayout, Check out the tutorial here where more of these are explained)
Then add your components to this panel accordingly
For the layout you are looking for it would possibly be easier to use an IDE to create this, I find Net Beans to be the easiest for doing this.
My recommendation would be for you to create a JPanel with a grid layout of 2 columns and 2 rows, to this add you JLabels and Text fields for the logon name and password.
Then create another JPanel possibly BorderLayout or Flow Layout and add the above panel to this then add this parent panel to the frame.