Java - JLabels on ScrollPane- How to set the locations of my labels? - java

How do I set my labels in a specific locations? I want to have some in a row, and others in a column. Im trying to set them with .setBorder(new EmptyBorder(coordX, coordY, 20, 140)); but doesnt seems to work.
Here is the code (remember to replace those .png):
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class Images extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
new Images();
}
public Images() {
setTitle("myTitle");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel(new BorderLayout());
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JLabel lblNombreequipo = new JLabel("trying: ");
lblNombreequipo.setBorder(new EmptyBorder(10, 40, 10, 10));
contentPane.add(lblNombreequipo, BorderLayout.PAGE_START);
JPanel scrollPanel = new JPanel(new GridLayout(0, 1, 0, 50));
contentPane.add(new JScrollPane(scrollPanel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
for (int i = 0; i < 50; i++) {
JLabel myLabel1 = new JLabel(new ImageIcon("myImage1.png"));
myLabel1.setBorder(new EmptyBorder(100, 100, 20, 140));
scrollPanel.add(myLabel1);
JLabel myLabel2 = new JLabel(new ImageIcon("myImage2.png"));
myLabel2.setBorder(new EmptyBorder(300, 100, 20, 140));
scrollPanel.add(myLabel2);
JLabel myLabel3 = new JLabel(new ImageIcon("myImage3.png"));
myLabel3.setBorder(new EmptyBorder(100, 100, 20, 140));
scrollPanel.add(myLabel3);
}
pack();
Dimension d = getSize();
setSize(new Dimension(d.width, 250));
setLocationByPlatform(true);
setMinimumSize(getSize());
setVisible(true);
}
}

How do I set my labels in a specific locations?
You use a layout manager effectively.
JPanel scrollPanel = new JPanel(new GridLayout(0, 1, 0, 50));
This says you want a single columns of components.
Read the section from the Swing tutorial on How to Use GridLayout for more information on how the layout manager works and for working examples you can download and play with.
If GridLayout isn't what you are trying to achieve then take a look at the other section from the tutorial for the various layout managers. Maybe a GridBagLayout which is more complicated to use is what you are looking for.
Im trying to set them with .setBorder(new EmptyBorder(coordX, coordY, 20, 140));
That is not what a Border is for. A Border simply adds extra space to a component. Read the section from the Swing tutorial on How to Use Borders for more information and working example.

Related

JScrollPanel, horizontal and vertical scroll not working

I have the next code:
public class MyScroll extends JFrame {
public MyScroll() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setLayout(null);
for (int i = 0; i < 10; i++) {
JButton b = new JButton("Hello-" + i);
b.setBounds(0, i * 50, 100, 45);
panel.add(b);
b.setLayout(null);
}
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(50, 30, 100, 325);
JPanel contentPane = new JPanel(null);
contentPane.setPreferredSize(new Dimension(500, 400));
contentPane.setBackground(Color.BLUE);
contentPane.add(scrollPane);
contentPane.setLayout(null);
setContentPane(contentPane);
pack();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
}
}
And, it is rendering this:
As you can see, the vertical and horizontal scroll aren't working, but both are defined and are displaying inside the JPanel.
Can someone explain me what am I doing wrong?
This code is based on this one:
Scrolling a JPanel
But, the is not working at the moment to use the verticall scrolling
Can someone explain me what am I doing wrong?
panel.setLayout(null);
Don't use a null layout.
The scrollbars will only appear automatically when the preferred size of the component added to the scrollpane is greater than the size of the scrollpane.
It is the job of the layout manager to determine the preferred size of the panel. Since you don't use a layout manager the preferred size isn't calculated.
So the solution is to use a Layout Manager. Maybe vertical BoxLayout.

GUI wont show labels and text fields when Spring layout is added

I used Grid layout first, Then i realized i wanted to use spring layout instead. When i add Spring layout Panel nothing wants to show
public class ComplexWindow extends JFrame {
public ComplexWindow() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(10, 10, 300, 420);
JPanel mainPanel = (JPanel) getContentPane();
mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
mainPanel.setLayout(new BorderLayout());
SpringLayout layout = new SpringLayout();
JPanel textPanel = new JPanel(layout);
JLabel lblName = new JLabel("Ime:");
textPanel.add(lblName);
JTextField txtName = new JTextField();
txtName.setColumns(10);
textPanel.add(txtName);
JLabel lblSurname = new JLabel("Prezime:");
textPanel.add(lblSurname);
JTextField txtSurname = new JTextField();
txtSurname.setColumns(10);
textPanel.add(txtSurname);
layout.putConstraint(SpringLayout.EAST, lblName, 5,
SpringLayout.WEST, txtName);
layout.putConstraint(SpringLayout.SOUTH, lblName, 5,
SpringLayout.NORTH, lblSurname);
layout.putConstraint(SpringLayout.SOUTH, txtName, 5,
SpringLayout.NORTH, txtSurname);
layout.putConstraint(SpringLayout.EAST, lblSurname, 5,
SpringLayout.WEST, txtSurname);
mainPanel.add(textPanel, BorderLayout.NORTH);
}
public static void main(String [] args) {
SwingUtilities.invokeLater(() -> {
ComplexWindow window = new ComplexWindow();
window.pack();
window.setVisible(true);
});
}
}
As this documentation says, SpringLayout is not for manual laying out of components:
https://docs.oracle.com/javase/tutorial/uiswing/layout/spring.html
SpringLayout is, however, very low-level and as such you really should
only use it with a GUI builder, rather than attempting to code a
spring layout manager by hand.
And also, a utility class used in above tutorial layout.SpringUtilities is not included in JDK.
So, I recommend you use GridBagLayout for this.

how do I add a JLabel to a specific location into my ScrollPane?

Im trying to show images and/or labels in my JPanel. The thing is, I want to show many JLabels in custom locations but currently I can only show like 4 labels. I understand I need to use ScrollPane. I have tried using it but its not letting me alocate them with customs locations. (By custom locations I mean using coords to place my label.)
Here is the code WITHOUT using ScrollPane:
import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
public class Images extends JFrame{
private JPanel contentPane;
public static void main(String[] args) {
Images image=new Images();
}
public Images(){
setVisible(true);
setTitle("myTitle");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 800);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNombreequipo = new JLabel("trying: ");
lblNombreequipo.setBounds(147, 110, 145, 14);
contentPane.add(lblNombreequipo);
int coordY=150;
for(int i=0;i<10;i++){
JLabel myLabel = new JLabel("attempt: "+i);
myLabel.setBounds(147,coordY, 145, 14);
contentPane.add(myLabel);
coordY=coordY+150;
}
}
}
Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them along with layout padding and borders for white space.
Here is an example. Adjust the layout padding and component border parameters to provide the 'exact' requirement & note further comments in the code.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class Images extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
new Images();
}
public Images() {
setTitle("myTitle");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// setBounds(100, 100, 800, 800); call pack() instead
contentPane = new JPanel(new BorderLayout());
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
//contentPane.setLayout(null); // never do that!
JLabel lblNombreequipo = new JLabel("trying: ");
lblNombreequipo.setBorder(new EmptyBorder(10, 40, 10, 10));
//lblNombreequipo.setBounds(147, 110, 145, 14);
contentPane.add(lblNombreequipo, BorderLayout.PAGE_START);
//int coordY = 150;
JPanel scrollPanel = new JPanel(new GridLayout(0, 1, 0, 50));
contentPane.add(new JScrollPane(scrollPanel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
for (int i = 0; i < 10; i++) {
JLabel myLabel = new JLabel("attempt: " + i);
myLabel.setBorder(new EmptyBorder(20, 140, 20, 140));
//myLabel.setBounds(147, coordY, 145, 14);
scrollPanel.add(myLabel);
//coordY = coordY + 150;
}
pack();
// now the preferred size has been calculated, we can shorten the height
Dimension d = getSize();
setSize(new Dimension(d.width, 250));
setLocationByPlatform(true);
setMinimumSize(getSize());
setVisible(true); //should be last
}
}

How to set a border around a specific JPanel in the JFrame?

I'm kinda bad at explaining but I'll do my best.
I've basically been trying to add a border around my JLabel, JTextField and JButton components within my JPanel, however the border is expanding according to size.
This is my code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LoginPanel
{
private JPanel loginPanel = new JPanel();
private JFrame loginFrame = new JFrame();
public LoginPanel()
{
loginPanel.setLayout(new GridBagLayout());
GridBagConstraints gridBagConstraints = new GridBagConstraints();
JTextField textLogin = new JTextField(10);
JPasswordField password = new JPasswordField(10);
JButton login = new JButton("Login");
JButton register = new JButton("Register");
gridBagConstraints.insets = new Insets(0,0,0,0);
gridBagConstraints.gridy = 0;
gridBagConstraints.gridx = 0;
loginPanel.setBorder(BorderFactory.createTitledBorder("Login"));
loginPanel.add(new JLabel("E-Mail"), gridBagConstraints);
gridBagConstraints.gridy++;
loginPanel.add(textLogin, gridBagConstraints);
gridBagConstraints.gridy++;
loginPanel.add(new JLabel("Password"), gridBagConstraints);
gridBagConstraints.gridy++;
loginPanel.add(password, gridBagConstraints);
gridBagConstraints.gridy++;
loginPanel.add(login, gridBagConstraints);
gridBagConstraints.gridy++;
loginPanel.add(register, gridBagConstraints);
loginFrame.pack();
loginFrame.add(loginPanel);
loginFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
loginFrame.setLocationRelativeTo(null);
loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loginFrame.setVisible(true);
}
public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception e)
{
e.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
new LoginPanel();
}
});
}
}
This is the outcome:
This is what I want the border to be like:
Hope the images explain more, I'll respond and provide any more questions needed.
A frame has a BorderLayout by default. A component added to a border layout with no constraint is put in the CENTER. The component in the center of of a border layout will be stretched to the full width and height of the available space.
It can be fixed by changing:
loginFrame.pack();
To:
loginFrame.setLayout(new GridBagLayout());
loginFrame.pack();
Since a single component added to a GridBagLayout with no constraint will be centered.
Other tips:
The GUI does not appear here, like in your screenshot. Instead the components are closer together and the titled border is snug up against them.
The first can be fixed by changing:
gridBagConstraints.insets = new Insets(0, 0, 0, 0);
To:
gridBagConstraints.insets = new Insets(5, 5, 5, 5);
The 2nd can be fixed by making the border a compound border that combines an EmptyBorder with the TitledBorder.
Border border = new CompoundBorder(
new TitledBorder("Login"),
new EmptyBorder(40, 50, 40, 50));
loginPanel.setBorder(border);
//loginPanel.setBorder(BorderFactory.createTitledBorder("Login"));
Use a second GridBagLayout for your frame:
...
loginFrame.setLayout(new GridBagLayout());
loginFrame.add(loginPanel, new GridBagConstraints(
0, 0,
1, 1,
0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.NONE,
new Insets(0, 0, 0, 0), 40, 20));
...
JFrame uses a BorderLayout by default - its CENTER component will use all available space. Using the GridBagLayout you have better control over space usage.

Java strechable JTextfield

I'm currently working on a browser in Java. I want to have a back button on the top left and to its right a JTextfield with the URL. I want the button to always have the same size but the textfield to change it's width to match the JFrame's width. It doesn't work with BorderLayout and I've tried this:
SpringLayout sl = new SpringLayout();
setLayout(sl);
sl.putConstraint(SpringLayout.WEST, back, 5, SpringLayout.WEST, this);
sl.putConstraint(SpringLayout.NORTH, back, 5, SpringLayout.NORTH, this);
sl.putConstraint(SpringLayout.WEST, addressBar, 5, SpringLayout.EAST, back);
sl.putConstraint(SpringLayout.NORTH, addressBar, 5, SpringLayout.NORTH, this);
sl.putConstraint(SpringLayout.SOUTH, back, 25, SpringLayout.NORTH, this);
sl.putConstraint(SpringLayout.SOUTH, addressBar, 25, SpringLayout.NORTH, this);
sl.putConstraint(SpringLayout.EAST, addressBar, 5, SpringLayout.EAST, this);
add(back);
add(addressBar);
where "back" is a JButton and addressBar a JTextField. The button seems to work but the addressBar just doen't draw at all.
Any suggestions?
There are many ways to solve this, and one in fact involves BorderLayout by nesting JPanels. Put the button into a BorderLayout.WEST position of a BorderLayout using container, but the JTextField BorderLayout.CENTER in the same container, and then put that container into the main container BorderLayout.CENTER.
GridBagLayout could also solve this, but again, often the best/simplest solution will involve nesting JPanels (for your containers), each with its own layout manager.
Edit
For example:
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.*;
public class BrowserFoo {
private static void createAndShowGui() {
JPanel topPanel = new JPanel(new BorderLayout(2, 2));
topPanel.add(new JButton("Back"), BorderLayout.WEST);
topPanel.add(new JTextField(20), BorderLayout.CENTER);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(topPanel, BorderLayout.NORTH);
mainPanel.add(Box.createRigidArea(new Dimension(400, 400)));
JFrame frame = new JFrame("BrowserFoo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Note that if you re-size this GUI, the textarea and button remain in proper location.

Categories

Resources