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).
Related
How would one go about to do that?
If I have
JLabel label1 = new JLabel ("Hello1");
JLabel label2 = new JLabel ("Hello2");
label2.setPreferredSize(new Dimension(100,20));
label1.setPreferredSize(new Dimension(100,20));
how do I cast a border around both labels?
Not a border for each, but one border spanning both labels.
Obviously I could draw it by hand, but that is unpractical.
I currently use flowlayout and my frame is unresizable.
Wrap your labels inside a JPanel with an appropriate layout manager and set the border f the panel.
The following code will result with the labels being horizontally next to each other with the given border around both.
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel.add(label1);
panel.add(label2);
panel.setBorder(...);
I am trying to make a panel to have 4 items on top. these are a JLabel, JTextField, JLabel and JTextField.
In the center I need a JTextArea and to the left of it a JList that is scrollable.
On the bottom I need 3 buttons.
What would be the best layout manager for this and how should I go about it?
Would having just 3 columns be a good idea?
Heres what I have so far:
JPanel panel = new JPanel();
JTextField IDLabel = new JLabel("ID: ");
IDLabel.setBounds(10, 10, 80, 25);
panel.add(IDLabel);
JTextArea IDText = new JTextField(5);
IDText.setBounds(100, 10, 160, 25);
panel.add(IDText);
JLabel TitleLabel = new JLabel("Title: ");
TitleLabel.setBounds(10, 10, 80, 25);
panel.add(TitleLabel);
JTextField TitleText = new JTextField(10);
TitleText.setBounds(100, 10, 160, 25);
panel.add(TitleText);
JList list = new JList(new String[]{"test1", "test22"});
list.setFixedCellWidth(150);
list.setFixedCellHeight(50);
list.setFont(new Font("Serif",Font.BOLD,16));
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
panel.add(list);
JTextArea BodyArea = new JTextArea();
BodyArea.setSize(200, 200);
BodyArea.setText("Test area");
panel.add(BodyArea);
You will mostly likely need to use a combination of layouts (AKA compound layouts), for example
North Panel
Create a JPanel and assign it a FlowLayout or GridBagLayout or GridLayout depending on what you want to achieve.
Add the JLabel, JTextField, JLabel, JTextField to it.
Center Panel
Create a JPanel with BorderLayout. Add the JTextArea to the CENTER position and the JList to the WEST position
South Panel
Craete a JPanel with a FlowLayout or GridBagLayout or GridLayout depending on what you want to achieve.
Add the buttons to it.
Putting it together
Create a JPanel with a BorderLayout, add the "north" panel to the NORTH position, the "center" panel to the CENTER position and the "south" panel to the SOUTH position
You could use a single container and a GridBagLayout, but that's a lot of work.
Take a look at Laying Out Components Within a Container for more details
Maybe start with a BorderLayout for the main layout. Then you can add components to the PAGE_START (NORTH), LINE_START (WEST) and CENTER and PAGE_END (SOUTH). Check out the section from the Swing tutorial on Using Layout Manager, for more information and examples.
Of course you would also use panels when you want to display multiple components in a single area. So your buttons would first be added to panels.
Also, follow standard naming conventions. Variable names should NOT start with an upper case character( ie. BodyArea, TitleText). You got variable like (panel, list) correct so be consistent.
I have a BorderLayout and I wish to include a BoxLayout in it such that I have three vertical buttons it .
The design is like this:
I want one horizontal borderlayout,below which I have two borderlayouts. In the left borderlayout I want boxlayout with three vertical buttons.
Here is the code I have tried:
JLabel label2 = new JLabel("LOGO");
pane.add(label2, BorderLayout.PAGE_START);
button = new JButton("Button 2 (CENTER)");
button.setPreferredSize(new Dimension(200, 100));
pane.add(button, BorderLayout.CENTER);
button = new JButton("Button 3 (LINE_START)");
pane.add(button, BorderLayout.LINE_START);
button = new JButton("Long-Named Button 4 (PAGE_END)");
pane.add(button, BorderLayout.PAGE_END);
How should I go about such that I get the design mentioned. Right now I am not getting it and I do not know how to add boxlayout inside borderlayout.
You need to create a new JPanel with desired layout (BoxLayout) and add those two buttons to it and then add this newly created panel to your existing pane.
JPanel pane1 = new JPanel(<Pass BoxLayout here>);
pane1.add(<add button 1 here>);
pane1.add(<add button 2 here>);
pane.add(pane1, <Position>);
Hope this helps.
I have a JPanel with some JLabel added with the add() method of JPanel. I want to align the JLabel to the right like the image below but I don't know how to do that. Any Idea? Thanks!
This can be done in two ways.
JLabel Horizontal Alignment
You can use the JLabel constructor:
JLabel(String text, int horizontalAlignment)
To align to the right:
JLabel label = new JLabel("Telephone", SwingConstants.RIGHT);
JLabel also has setHorizontalAlignment:
label.setHorizontalAlignment(SwingConstants.RIGHT);
This assumes the component takes up the whole width in the container.
Using Layout
A different approach is to use the layout to actually align the component to the right, whilst ensuring they do not take the whole width. Here is an example with BoxLayout:
Box box = Box.createVerticalBox();
JLabel label1 = new JLabel("test1, the beginning");
label1.setAlignmentX(Component.RIGHT_ALIGNMENT);
box.add(label1);
JLabel label2 = new JLabel("test2, some more");
label2.setAlignmentX(Component.RIGHT_ALIGNMENT);
box.add(label2);
JLabel label3 = new JLabel("test3");
label3.setAlignmentX(Component.RIGHT_ALIGNMENT);
box.add(label3);
add(box);
JLabel label = new JLabel("fax", SwingConstants.RIGHT);
To me,
it seems as if your actual intention is to put different words on different lines.
But let me answer your first question:
JLabel lab=new JLabel("text");
lab.setHorizontalAlignment(SwingConstants.LEFT);
And if you have an image:
JLabel lab=new Jlabel("text");
lab.setIcon(new ImageIcon("path//img.png"));
lab.setHorizontalTextPosition(SwingConstants.LEFT);
But, I believe you want to make the label such that there are only 2 words on 1 line.
In that case try this:
String urText="<html>You can<br>use basic HTML<br>in Swing<br> components,"
+"Hope<br> I helped!";
JLabel lac=new JLabel(urText);
lac.setAlignmentX(Component.RIGHT_ALIGNMENT);
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());