Aligning the JButton and JLabel on JFrame - java

I am trying to create a JFrame and in which I want the button (Select the Device) to be on top and a text message (Active) which is in the form of Label at the bottom. I am unable to do that and they are all coming up in the same line next to each other.
JFrame f= new JFrame("AutoV");
f.setVisible(true);
f.setSize(600,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p=new JPanel();
p.setBackground(Color.gray);
JButton b=new JButton("Select the Device");
JLabel lab=new JLabel("Active");
lab.setVerticalAlignment(SwingConstants.BOTTOM);
//p.add(b);
p.add(lab);
p.setBorder(BorderFactory.createLineBorder(Color.black));
f.add(p);
Dimension dim1 = Toolkit.getDefaultToolkit().getScreenSize();
f.setLocation(dim1.width/2-f.getSize().width/2, dim1.height/2-f.getSize().height/2);

You should look up different layouts. The default layout of many components is FlowLayout, witch just aligns all elements horizontally, and as small as possible. Setting the panels layout to box or grid layout should do the trick.
https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

Related

How do i use border layouts?

I'm currently working on Layouts in Java. I'm trying to combine different layouts. So i have created a login Screen using Spring Layout, GridLayout and Border Layout.
The MainFrame (JFrame) uses GridLayout. The GridLayout consists of 2 Panel (North Panel and Main Panel). The Main Panel consists of the Jlabel, JTextfield and JButton of which I have no problem of. My problem is in the North Panel which uses Border Layout. It contains a JLabel (lblWelcome). I have been trying to bring the label to the center of the panel using Border Layout but it still aligns to the left. This is the below code:
JLabel lblWelcome = new JLabel("Welcome To The Login Screen");
JPanel northPanel = new JPanel(new BorderLayout());
northPanel.setBackground(Color.green);
northPanel.add(lblWelcome, BorderLayout.CENTER);
Login Screen :
Your JLabel is actually correctly centered, but its text isn't.
Simply change its creation to :
JLabel lblWelcome = new JLabel("Welcome To The Login Screen", SwingConstants.CENTER);

Java: How do i set my background as a picture with my buttons showing [duplicate]

This question already has answers here:
How to set an image as a background for Frame in Swing GUI of java?
(8 answers)
Closed 7 years ago.
Alright, I figured out how to add a background picture but how do i make the buttons show. I'm making pong if you're interested.
Here is my Code:
public class Gui extends JFrame{
private JButton JB;
private JButton EB;
public Gui(){
super("Pong");
JPanel outside = new JPanel();
JPanel inside = new JPanel();
setLayout(new BorderLayout());
this.setContentPane(new JLabel(new ImageIcon("S:\\Music\\Pong title pic.jpg")));
outside.setLayout(new BoxLayout(outside, BoxLayout.LINE_AXIS));
inside.setLayout(new BoxLayout(inside, BoxLayout.PAGE_AXIS));
outside.add(Box.createHorizontalStrut(280));
outside.add(inside);
outside.add(Box.createHorizontalStrut(20));
inside.add(Box.createVerticalStrut(20));
JLabel title = new JLabel("Pong");
inside.add(title);
inside.add(Box.createVerticalStrut(20));
JButton btt1 = new JButton("Start");
Dimension d = new Dimension(200,40);
btt1.setSize(d);
btt1.setMinimumSize(d);
btt1.setMaximumSize(d);
btt1.setPreferredSize(d);
JButton btt2 = new JButton("Credits");
btt2.setSize(d);
btt2.setMinimumSize(d);
btt2.setMaximumSize(d);
btt2.setPreferredSize(d);
JButton btt3 = new JButton("Exit");
btt3.setSize(d);
btt3.setMinimumSize(d);
btt3.setMaximumSize(d);
btt3.setPreferredSize(d);
inside.add(btt1);
inside.add(Box.createVerticalStrut(5));
inside.add(btt2);
inside.add(Box.createVerticalStrut(5));
inside.add(btt3);
inside.add(Box.createVerticalStrut(20));
add(outside);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800,600);
this.setVisible(true);
this.setResizable(false);
}
}
Thanks for responding.
//setLayout(new BorderLayout());
this.setContentPane(new JLabel(new ImageIcon("S:\\Music\\Pong title pic.jpg")));
setLayout(new BorderLayout());
You set the layout but then you replace the content pane of the frame so you loose the layout manager.
You need to set the layout after you set the content pane.
Note you can only use the label as the background is the image of the label is larger than the components being added to the label.
Also, don't attempt to manipulate the size of the buttons by using setSize(), setPreferredSize(), setMinimumSize() and setMaximumSize(). Let the button display at its preferred size.
If you want the button to all be the same size, then add the buttons to a panel using a GridLayout first. You can specify the spacing between components when you create the GridLayout. Or you can use a GridBagLayout, which will allow you so specify a constraint that "fills" the width of each cell.

JLabel not Truncating Within JPanel

If I put a JLabel inside of a JPanel with no rigid boundaries, it does not truncate the text when the JPanel is sized to be smaller than the text. Why does this happen? Shouldn't the JPanel realize that there isn't enough room & truncate the text accordingly, regardless of its layout?
As an example, I created a JFrame with a GridLayout with two rows & one column. In that, I placed a JPanel with a FlowLayout on top, & a JPanel with a BoxLayouton the bottom. Each JPanel contains a JLabel.
In short: Why doesn't the JLabel on the top truncate its text?
Images to demonstrate what I mean:
As well, this is the SSCCE to demonstrate the effect:
import javax.swing.*;
import java.awt.*;
public class TruncationTest1
{
public static void main(String[] args)
{
JFrame f = new JFrame("Truncation Test");
JPanel panel = new JPanel(); //Default layout, aka FlowLayout
JLabel label = new JLabel("Try resizing the frame: This will not be truncated for some reason.");
JLabel label2 = new JLabel("However, this JLabel, on the other hand, will become truncated.");
f.setLayout(new GridLayout(2,1));
f.setBackground(Color.BLACK);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(panel);
panel.setBackground(Color.WHITE);
panel.add(label);
f.add(label2);
label2.setHorizontalAlignment(JLabel.CENTER);
label2.setForeground(Color.WHITE);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
FlowLayout does not resize components inside it. It shows the components with preferred size.
You need to use different layout in place of FlowLayout for proper resizing. For instance you could use BorderLayout with the label at LINE_START or WEST.

Java Layout for scrollable panels

I am trying to create a JPanel that is resizable & scrollable and contains x smaller inner panels. Each inner panel can be as wide as it wants/needs. BUT the depth should be a preferred size.
Like:
So far my code is:
public class TestSize {
public static void main(String[] args) {
JFrame F = new JFrame();
F.setVisible(true);
JPanel P = new JPanel();
P.setLayout(new BorderLayout());
JScrollPane scrollPane = new JScrollPane(P);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setPreferredSize(new Dimension(900,900));//.setBounds(50, 30, 300, 50);
JPanel S = new JPanel();
S.setBackground(Color.GREEN);
S.setPreferredSize(new Dimension(900,200));
JPanel S2 = new JPanel();
S2.setBackground(Color.GREEN);
S2.setPreferredSize(new Dimension(900,200));
P.add(S,BorderLayout.NORTH);
P.add(S2,BorderLayout.NORTH);
F.add(scrollPane);
F.pack();
F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
But when I have added a single inner panel it fills all the space vertically - which is not what I want:
//P.setLayout(new BorderLayout());
Why did you set the layout to a BorderLayout? You can only add 1 component to the NORTH. Is that what you want? Read the section from the Swing tutorial on Using Layout Managers and pick a more appropriate layout manager. Bookmark the tutorial link as it provides the basics for Swing programming.
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
Why did you set these properties? These are the default values.
scrollPane.setPreferredSize(new Dimension(900,900));
Why would you set the height to be 900. You only want the scrollpane to contain components of height 200? In general you should NOT be setting the preferred size. Let the frame.pack() method do its job.
S.setBackground(Color.GREEN);
Why would you set the background color of both panels to be the same. How can you tell if the two panels get added? Make them different color for an easy visual.
In BoxLayout, there are different methods of using invisible components as filler. I don't think you will want to use a "rigid area", since I think you want to have a variable number of inner panels. You may want to try using vertical glue or custom Box.Filler.
Another solution might be to put a JPanel between your JFrame and your ScrollPane that uses a BorderLayout, and put the scrollpane in the BorderLayout.NORTH of that panel. Components in BorderLayout.NORTH get resized horizontally, but they do not get resized vertically. Essentially, they just get pushed to the top of the panel.
Edit:
I think you will want something like this:
JFrame F = new JFrame();
F.setVisible(true);
F.setLayout(new BorderLayout());
JPanel P = new JPanel(new BoxLayout(P, BoxLayout.PAGE_AXIS));
JScrollPane scrollPane = new JScrollPane(P);
JPanel S = new JPanel();
S.setBackground(Color.GREEN);
JPanel S2 = new JPanel();
S2.setBackground(Color.BLUE);
P.add(S);
P.add(S2);
F.add(scrollPane, BorderLayout.NORTH);
F.pack();
F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
You said you want it resizeable, so I don't see why you would set the preferred size of the frame. This way it will just fit to the panels inside.

How can I place a component in a JLayeredPane right below an existing component?

I have a JTextField, and right below it I want to show a JLabel placed in a JLayeredPane (I will use it for autosuggestions later on).
How can I place my JLabel in JLayeredPane right below the JTextField?
Here is some code I have, and the current result shown in the screenshot below:
public static void main(String[] args) {
JTextField field = new JTextField();
JLabel lbl = new JLabel("Hello");
lbl.setBackground(Color.YELLOW);
lbl.setOpaque(true);
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setLayout(new GridLayout(0,1));
layeredPane.add(lbl, (Integer) (JLayeredPane.POPUP_LAYER - 10));
layeredPane.setPreferredSize(field.getPreferredSize());
JPanel panel = new JPanel(new BorderLayout());
panel.add(field, BorderLayout.NORTH);
panel.add(layeredPane, BorderLayout.SOUTH);
JFrame frame = new JFrame();
frame.add(panel);
frame.setSize(200, 360);
frame.setVisible(true);
}
Second try:
public static void main(String[] args) {
JTextField field = new JTextField();
JLabel lbl = new JLabel("Hello");
lbl.setBackground(Color.YELLOW);
lbl.setOpaque(true);
lbl.setBounds(field.getBounds().x, field.getBounds().y,
field.getBounds().width, field.getBounds().height);
JPanel popPanel = new JPanel(new BorderLayout());
popPanel.add(lbl, BorderLayout.NORTH);
popPanel.setLocation(field.getLocation().x+10, field.getLocation().y+20);
popPanel.setPreferredSize(field.getPreferredSize());
JFrame frame = new JFrame();
JLayeredPane layeredPane = frame.getRootPane().getLayeredPane();
layeredPane.setLayout(new GridLayout(0,1));
layeredPane.add(popPanel, (Integer) (JLayeredPane.POPUP_LAYER - 10));
JPanel panel = new JPanel(new BorderLayout());
panel.add(field, BorderLayout.NORTH);
frame.add(panel);
frame.setSize(200, 360);
frame.setVisible(true);
}
Add the layeredPane to the "CENTER", not the SOUTH.
However, your understanding a layed pane seems to be a little confused. You use a layered pane when you want multiple components to be displayed on top (stacked?) of one another. You are still using the layered pane in 2 dimensions which is unnecessary. YOu can just use a panel for this.
If you want to popup a list of suggestions then you should just use a JPopupMenu and position it below the text field. Read the section from the Swing tutorial on Bringing up Popup Menus.
First of all, I don't think you should use a JLayeredPane for that, but just a permanent label.
If you do use a layered pane, you'll have to compute where the text field ends (y = field.getY() + field.getHeight()) and set your JPanel at 'panel.setLocation(0, y)' inside the JLayeredPane (provided the JLayeredPane has the same starting position as the underlying JFrame). You could equivalently position the JLayeredPane at (0, y) and put the label at (0, 0) within that layered pane.
You have to make sure this is done every time the components are resized.
why not using AutoComplete ComboBox / JTextField and if you don't want to display JComboBox, then there is AutoCompleted JTextField, and for somehow reduced autosuggestions, would be better look for undecorated JDialog/Window with JTable with one TableColum and without TableHeaded in the JScrollPane, just with plain RowSorter, very simle job

Categories