0

enter image description here

I am trying to make a desktop application in Java Swing. I am trying to create image slider in frame and I got it. Now problem in that I want to set the specific area for imagelabel in that frame. How can I do this? I want to set imagelabel in left side. I am posting my snapshot which I am getting after running my program.

Here is my code

here i  am getting like this

public class ImageSlider extends JPanel implements ActionListener {

    private static final int MAX = 20;
    private static final Font sans = new Font("SansSerif", Font.PLAIN, 16);
    private static final Border border =
        BorderFactory.createMatteBorder(4, 16, 4, 16, Color.BLUE);

    private List<String> list = new ArrayList<String>(MAX);
    private List<ImageIcon> cache = new ArrayList<ImageIcon>(MAX);
    private JLabel imageLabel = new JLabel();
     //label = new JLabel( image, SwingConstants.CENTER);
    private JButton prevButton = new JButton();
    private JButton nextButton = new JButton();
    private JComboBox favorites;

    public ImageSlider() {
        this.setLayout(new BorderLayout());

        list.add("c.jpg");
        list.add("a0.png");
        list.add("yellow.png");
         list.add("a0.png");
         list.add("c.jpg");

        for (int i = 0; i < list.size(); i++) cache.add(i, null);
 ImageIcon image = new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\a0.png");
        JLabel titleLabel = new JLabel(image, SwingConstants.CENTER);
       // titleLabel.setText("ImageSlider");
        titleLabel.setHorizontalAlignment(JLabel.CENTER);
        titleLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 24));
        titleLabel.setBorder(border);
        this.add(titleLabel, BorderLayout.NORTH);

        imageLabel.setIcon(getImage(0));
        imageLabel.setAlignmentX(LEFT_ALIGNMENT);
        imageLabel.setHorizontalAlignment(JLabel.CENTER);
        imageLabel.setBorder(border);
        this.add(imageLabel, BorderLayout.CENTER);

        favorites = new JComboBox(
            list.toArray(new String[list.size()]));
        favorites.setActionCommand("favs");
        favorites.addActionListener(this);

        prevButton.setText("\u22b2Prev");
        prevButton.setFont(sans);
        prevButton.setActionCommand("prev");
        prevButton.addActionListener(this);

        nextButton.setText("Next\u22b3");
        nextButton.setFont(sans);
        nextButton.setActionCommand("next");
        nextButton.addActionListener(this);

        JPanel controlPanel = new JPanel();
        controlPanel.add(prevButton);
        controlPanel.add(favorites);
        controlPanel.add(nextButton);
        controlPanel.setBorder(border);
        this.add(controlPanel, BorderLayout.SOUTH);
    }

    public void actionPerformed(ActionEvent ae) {
        String cmd = ae.getActionCommand();
        if ("favs".equals(cmd)) {
            int index = favorites.getSelectedIndex();
            ImageIcon image = getImage(index);
            imageLabel.setIcon(image);
            if (image != null) imageLabel.setText("");
            else imageLabel.setText("Image not available.");
        }
        if ("prev".equals(cmd)) {
            int index = favorites.getSelectedIndex() - 1;
            if (index < 0) index = list.size() - 1;
            favorites.setSelectedIndex(index);
        }
        if ("next".equals(cmd)) {
            int index = favorites.getSelectedIndex() + 1;
            if (index > list.size() - 1) index = 0;
            favorites.setSelectedIndex(index);
        }
    }

    public JButton getDefault() { return nextButton; }

    // Return the (possibly cached) image having the given index.
    private ImageIcon getImage(int index) {
        ImageIcon image = cache.get(index);
        if (image != null) return image;
        String name = "/images/" + list.get(index);
        URL url = ImageSlider.class.getResource(name);
        if (url != null) {
            image = new ImageIcon(url);
        }
        cache.set(index, image);
        return image;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                ImageSlider go = new ImageSlider();
                frame.add(go);
                frame.setTitle("ImageSlider");
              //  frame.setSize(400, 300);
                      frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 
                      frame.setUndecorated(true);
                frame.setVisible(true);
                go.getDefault().requestFocusInWindow();
            }
        });
    }
}

How can I achieve my goal?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    I don't even see any question mark. – Smutje Mar 10 '14 at 08:14
  • use setBounds(int,int,int,int); – Benjamin Mar 10 '14 at 08:14
  • 4
    @user3313050 NO, `setBounds` is NOT the solution. – Paul Samsotha Mar 10 '14 at 08:16
  • In reply to the quite wrong (bad direction to go in, fragile) direction suggested by @user3313050, I will add. Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Mar 10 '14 at 08:17
  • Use a layout manager capable of providing that support, GridBagLayout, GridLayout and BorderLayout come to mind. You might need to set the horizontal position property of the label itself – MadProgrammer Mar 10 '14 at 08:24

1 Answers1

2

The easiest way to achieve this is to put the imageLabel into a JPanel with a FlowLayout. Then add that panel to the bigger BorderLayout.

So change:

this.add(imageLabel, BorderLayout.CENTER);

To something like:

JPanel imageConstrain = new JPanel(new FlowLayout(SwingConstants.LEFT));  
imageConstrain.add(imageLabel);
this.add(imageConstrain, BorderLayout.CENTER);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Add in the possible use of JLabel#setHorizontalAlignment and win the giant teddy – MadProgrammer Mar 10 '14 at 08:25
  • @MadProgrammer Oh yeah, that would be better. Just habit I suppose, that makes me say 'drop it in another container'. ..but I was going for the 'big scary looking clown' - teddy bears are for wooses. ;) – Andrew Thompson Mar 10 '14 at 08:28
  • 1
    As seen [here](http://stackoverflow.com/q/22292865/2587435) I think the OP wants to add another panel, I believe to the right, so along with your answer with the nested `JPanel`, The OP may just want `BorderLayout` instead of `FlowLayout`, then add the `imageLabel` to `CENTER` and other panel to `EAST`. +1 for nested `JPanel` – Paul Samsotha Mar 10 '14 at 08:30