0

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); 
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
stellsh
  • 61
  • 3
  • 2
    OK, you're asking about a visual problem but have not posted a visual representation of what you're trying to do. Please post an image or a link to an image that shows what you're trying to achieve and what you're currently getting. This will likely improve the quality of answer that you might get. – Hovercraft Full Of Eels Jun 10 '17 at 19:27
  • 1
    *"(remember to replace those .png):"* One way to get image(s) for an example is to **hot link** to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). BTW - I was expecting this question to be about resizing labels (or the images in them). Rather than titled much like the [previous question](https://stackoverflow.com/questions/44465169/how-do-i-add-a-jlabel-to-a-specific-location-into-my-scrollpane/44468739#44468739)! – Andrew Thompson Jun 10 '17 at 20:08

1 Answers1

0

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • It may be what Im looking for. How do I add GridBagLayout to my ScrollPane? Im having problems to implement it. – stellsh Jun 11 '17 at 00:19
  • @stellsh, You don't add a GridBagLayout to a JScrollPane. You set the layout of a JPanel to use the GridBagLayout and then and the panel to the scrollpane. You have been given the tutorial link. Read the tutorial. Download the working examples and play with it to get your desired layout. Your question is not clear enough to provide any detailed help. – camickr Jun 11 '17 at 01:50