I wrote this code for realize a box into I put four JRadioButtons
JRadioButton beginner = new JRadioButton("Beginner"); beginner.setSelected(true);
JRadioButton intermedie = new JRadioButton("Intermedie");
JRadioButton expert = new JRadioButton("Expert");
JRadioButton custom = new JRadioButton("Custom");
Box boxDifficulty = Box.createVerticalBox();
boxDifficulty.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 50));
boxDifficulty.add(beginner);
boxDifficulty.add(intermedie);
boxDifficulty.add(expert);
boxDifficulty.add(custom);
Furthermore I set the border of the box using the setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 50)) method.
Also, I created a JLabel for indicate the title of the box:
JLabel difficulty = new JLabel("Choose the difficulty:");
This is the portion complete of the code:
Box boxDifficulty = Box.createVerticalBox();
boxDifficulty.add(difficulty);
boxDifficulty.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 50));
boxDifficulty.add(beginner);
boxDifficulty.add(intermedie);
boxDifficulty.add(expert);
boxDifficulty.add(custom);
How can I increase the space between JLabel and JRadioButton?
How can I increase the space between JLabel and JRadioButton?
You can add a spacing component to the panel after you add the label:
boxDifficulty.add(difficulty);
boxDifficulty.add( Box.createVerticalStrut(...) );
A strut is a fixed height(width) invisible component.
Related
So I have a problem that I have to make a questionnaire about something and I have to use multiple Layouts.
My problem is that when I add 2 JPanels to a Grid layout (only to 1 side) my first Panel takes up most of the space.
Code:
public class MainFrame extends JFrame implements ItemListener{
JPanel mainPanel,rightSideAge,rightSideGender,leftSide,rightSideBox,leftSideBox;
JTextArea nameArea;
JSpinner ageSpinner;
JRadioButton genMale,genFema;
ButtonGroup genderGroup;
MainFrame(){
this.setSize(1000, 800);
this.setLocationRelativeTo(null);
this.setTitle("Közvélemény kutatás a zenei ízlésekről");
mainPanel = new JPanel(new GridLayout(0, 2));
this.setContentPane(mainPanel);
/* --- RIGHT PANEL --- */
rightSideBox = new JPanel();
rightSideBox.setLayout(new BoxLayout(rightSideBox, BoxLayout.Y_AXIS));
rightSideAge = new JPanel(new FlowLayout(FlowLayout.LEFT));
rightSideAge.setBorder(BorderFactory.createLineBorder(Color.BLUE));
//rightSide.setLayout(new BoxLayout(rightSide, BoxLayout.Y_AXIS));
mainPanel.add(rightSideBox);
//Age label
//JLabel labelAge = new JLabel("Kor: ");
//labelAge.setSize(100, 30);
//Age Spinner
ageSpinner = new JSpinner(new SpinnerNumberModel(1, 1, 120, 1));
ageSpinner.setPreferredSize(new Dimension(40, 20));
Component mySpinnerEditor = ageSpinner.getEditor();
JFormattedTextField jftf = ((JSpinner.DefaultEditor) mySpinnerEditor).getTextField();
jftf.setColumns(5);
//New box for zenei ízlés
rightSideGender = new JPanel();
rightSideGender.setBorder(BorderFactory.createLineBorder(Color.GREEN));
rightSideGender.setLayout(new BoxLayout(rightSideGender,BoxLayout.Y_AXIS));
//Gender ComboBox
genderGroup = new ButtonGroup();
genMale = new JRadioButton("Férfi");
genderGroup.add(genMale);
genFema = new JRadioButton("Nő");
genderGroup.add(genFema);
/* --- LEFT SIDE --- */
rightSideBox.setBorder(BorderFactory.createLineBorder(Color.RED));
/* ADD STUFF TO PANELS */
/* RightSideBox */
rightSideBox.add(rightSideAge);
rightSideBox.add(rightSideGender);
/*RIGHT SIDE PANELS*/
rightSideGender.add(new JLabel("Nem:"));
rightSideGender.add(genMale);
rightSideGender.add(genFema);
rightSideAge.add(new JLabel("Kor"));
//rightSide.add(labelAge);
rightSideAge.add(ageSpinner);
/*LEFT SIDE PANEL*/
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
The Blue lineout sould be only under the JSpinner:
ageSpinner.setPreferredSize(new Dimension(40, 20));
First of all, you should not be manually setting the size of a component. Each Swing component is responsible for determining its own size.
The Blue lineout should be only under the JSpinner:
The box layout will resize a component to its maximum size if there is space available. For some reason a JSpinner doesn't appear to have a maximum height so it expands to fill all the available space.
To fix this you can do something like:
//ageSpinner.setPreferredSize(new Dimension(40, 20));
ageSpinner.setMaximumSize( ageSpinner.getPreferredSize() );
mainPanel layout is set to have two columns:
mainPanel = new JPanel(new GridLayout(0, 2));
You only add one panel to mainPanel which uses the GridLayout:
mainPanel.add(rightSideBox);
Note: the first component you add, in this case rightSideBox will occupy the first column, meaning it will be the LEFT one.
To add rightSideGender to mainPanel you need to :
mainPanel.add(rightSideGender);
The second component you add, in this case rightSideGender will occupy the second column, in this case the RIGHT one.
I am making KenKen as my term project using java swing library. For alignment I have used gridbag and gridlayout, But now i want to add one more component of JPanel to the UI. These screenshots will make the problem more clear:
Now I select the grid cell to which i want to add respective candidates of in the left most panel.
It disturbs the adjacent alignments of the grid and panels.
Here are the panels with their respective layouts:
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1, 4, 5, 5));
buttonPanel.setPreferredSize(new Dimension(20,40));
buttonPanel.add(undoButton);
buttonPanel.add(redoButton);
buttonPanel.add(eraseButton);
buttonPanel.add(hintButton);
JPanel cellPanel = new JPanel();
cellPanel.setName("cellPanel");
cellPanel.setLayout(new GridLayout(pSize, pSize, 0, 0));
JPanel numPanel = new JPanel();
numPanel.setName("numPanel");
numPanel.setLayout(new GridLayout(1,1,5,5));
numPanel.setPreferredSize((new Dimension(50,60)));
JPanel candPanel = new JPanel();
candPanel.setName("candidatesPanel");
JLabel candidates = new JLabel("Candidates");
candidates.setFont(new Font("Courier New", Font.ITALIC, 14));
candidates.setForeground(Color.GRAY);
candPanel.setLayout(new GridLayout(0,1));
candPanel.add(candidates);
Then it all goes into the content panel:
content.add(buttonPanel, pos.nextCol().expandW());
content.add(candPanel, pos.nextRow());
content.add(new Gap(GAP) , pos.nextRow()); // Add a gap below
content.add(cellPanel, pos.nextCol());
content.add(numPanel,pos.nextCol().expandW());
The buttons are all generated on runtime, and they are added to the candPanel in an action listener.
You appear to be using a GridBagConstraints subclass of which I am unaware (variable pos), though I can guess its function from context.
Assuming your problem is that you want the candidates panel to be to the left of the cellPanel, and not above it, you need to swap the lines which add the candPanel and the new Gap(GAP) as follows:
content.add(buttonPanel, pos.nextCol().expandW());
content.add(new Gap(GAP), pos.nextRow()); // These two lines
content.add(candPanel, pos.nextRow()); // swapped over
content.add(cellPanel, pos.nextCol());
content.add(numPanel,pos.nextCol().expandW());
I have got a window that should display the following:
JLablel "Have you used GUI before?" on the top, centered
two radioButtons "Yes" and "No" below it, somewhat in the center, a little bit towards the left
a JButton "NEXT" in the bottom-right corner
All three elements should have green font and darkGrey background.
The problem is that the window which is showing up, does not look like I would like it to.
And this is my code:
yesButton = new JRadioButton(yes);
//yesButton.setMnemonic(KeyEvent.VK_B); // doesn't work?
yesButton.setActionCommand(yes);
noButton = new JRadioButton(no);
// noButton.setMnemonic(KeyEvent.VK_C); // doesn't work?
noButton.setActionCommand(no);
ButtonGroup group = new ButtonGroup();
group.add(yesButton);
group.add(noButton);
nextButton = new JButton("NEXT");
nextButton.setActionCommand(next);
yesButton.addActionListener(this);
noButton.addActionListener(this);
nextButton.addActionListener(this);
JPanel radioPanel = new JPanel(new GridLayout(0, 1));
radioPanel.add(yesButton);
radioPanel.add(noButton);
add(radioPanel, BorderLayout.WEST);
// setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
// radioPanel.setBorder(new EmptyBorder(250, 250, 20, 20));
// there is no difference between the above two, right?
String q = "Have you used GUI before?";
JPanel area = new JPanel(new BorderLayout());
area.setBackground(Color.darkGray);
JLabel textLabel2 = new JLabel("<html><div style=\"text-align: center;\">"
+ q + "</html>", SwingConstants.CENTER);
textLabel2.setForeground(Color.green);
Font font2 = new Font("SansSerif", Font.PLAIN, 30);
textLabel2.setFont(font2);
//textLabel2.setBorder(new EmptyBorder(0, 0, 250, 0)); //top, left, bottom, right
area.add(textLabel2, BorderLayout.NORTH);
area.add(nextButton, BorderLayout.EAST);
add(area, BorderLayout.CENTER);
I feel I'm nearly there, thanks for any help!
--EDIT--
A screenshot:
You need to use nested panels.
for the BorderLayout.NORTH you can add the JLabel directly. You will need to set the horizontal text alignment to center.
for the radio buttons you can create a JPanel with a FlowLayout and then add the buttons to the panel and add the panel to the CENTER.
for the button you add the button to a panel using a FlowLayout that is right aligned, then add the panel to the SOUTH.
There are other choices. You could also use a Vertical BoxLayout as the layout of the main panel and then add child panels to it.
You won't be able to get much control with just a BorderLayout. Try something else like MigLayout or one of the other many many layout managers Java has (GridBag, Box, etc).
In MigLayout it would look something like:
area.setLayout(new MigLayout("fill"));
area.add(textLabel2, "wrap");
area.add(radioPanel, "wrap");
area.add(nextButton, "tag right");
I have a code:
Box box_general = Box.createHorizontalBox();
Box box_panel1 = Box.createVerticalBox();
Box box_panel2 = Box.createVerticalBox();
JPanel jpanel_1 = new JPanel();
jpanel_1.setPreferredSize(new Dimension(50, 152));
jpanel_1.setOpaque(true);
jpanel_1.setBorder(BorderFactory.createLineBorder(Color.BLUE));
JPanel jpanel_2 = new JPanel();
jpanel_2.setPreferredSize(new Dimension(340, 152));
jpanel_2.setOpaque(true);
jpanel_2.setBorder(BorderFactory.createLineBorder(Color.RED));
JTextField jtxtf_populationSize = new JTextField();
jtxtf_populationSize.setSize(10, 20);
JTextField jtxtf_processorsAmount = new JTextField();
JButton jbtn_loadProcesses = new JButton("File path");
box_panel1.add(Box.createRigidArea(new Dimension(0,4)));
box_panel1.add(jtxtf_processorsAmount);
box_panel1.add(Box.createRigidArea(new Dimension(0,20)));
box_panel1.add(jbtn_loadProcesses);
jpanel_1.add(box_panel1);
JLabel jlbl_populationSize = new JLabel("Enter the population size");
JLabel jlbl_processorsAmount = new JLabel("Enter the amount of processors");
JLabel jlbl_loadProcesses = new JLabel("Load processes from file");
jlbl_populationSize.setFont(font);
jlbl_processorsAmount.setFont(font);
jlbl_loadProcesses.setFont(font);
box_panel2.add(jlbl_populationSize);
box_panel2.add(Box.createRigidArea(new Dimension(0,4)));
box_panel2.add(jlbl_processorsAmount);
box_panel2.add(Box.createRigidArea(new Dimension(0,15)));
box_panel2.add(jlbl_loadProcesses);
jpanel_2.add(box_panel2);
box_general.add(jpanel_2);
box_general.add(Box.createRigidArea(new Dimension(10,0)));
box_general.add(jpanel_1);
It creates 3 boxes, where the general box contains two other boxes. The problem is: in the box all the compopents are stretched of width. For example, there is a line jtxtf_populationSize.setSize(10, 20); but this text field is stretched in the box_panel1 on width. I tried to use a BoxLayout with it's Alignment_X but this didn't work.
Could you, please, advise me, what to do - how to avoid stretching?
Most of the swing layouts will use the preferred size and the minimum/maximum size over a call to setSize. The key here is to get the right preferred and minimums so that they don't shrink too much, and then insert a strut (Box#createHorizontalStrut) to fill up space where you don't want a component.
With complex layouts like this, consider the SpringLayout, which admittedly has a higher learning curve, but once you get used to it, will allow you to more naturally state the constraints you want.
How can I add two label in the same grid box? e.g. in row 1, col 1 the will be 2 labels?
The code below will add the label in two different grid.
JPanel chckBox = new JPanel(new GridLayout(1,8,3,3));
JLabel label1 = new JLabel("A");
JLabel label2 = new JLabel("B");
...
chckBox.add(label1);
chckBox.add(label2);
...
Put a JPanel into that part of the grid and have the two labels as children of that panel (which can have its own layout manager).