Search code examples
javaswingjtablejlabel

Java - JLabel with icon not aligned (going to the right)


I'm trying to make a deck manager for a card game (Yu-Gi-Oh :D), and for now I only have a table with available cards and a panel that shows the card the user selected in a bigger size and with the card's description. MVCE:

    import java.awt.*;
    import javax.swing.*;

    public class SelectedCardPanel extends JPanel{
        private final JLabel cardArea;
        private final JTextArea cardInfo;

        public static void main(String args[]){
            java.awt.EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JFrame frame = new JFrame("Yu-Gi-Oh!");
                    frame.add(new SelectedCardPanel());
                    frame.pack();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                }
            });
        }

        public SelectedCardPanel(){
            super();

            setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
            setPreferredSize(new Dimension(200, 400));

            cardArea = new JLabel("                                 ");
            cardArea.setOpaque(true);
            cardArea.setBackground(Color.white);
            cardArea.setPreferredSize(new Dimension(200, 300));
            cardArea.setSize(cardArea.getPreferredSize());
            cardArea.setBorder(BorderFactory.createLineBorder(Color.black));

            cardInfo = new JTextArea();
            cardInfo.setEditable(false);
            cardInfo.setWrapStyleWord(true);
            cardInfo.setLineWrap(true);
            cardInfo.setPreferredSize(new Dimension(200, 100));
            cardInfo.setBorder(BorderFactory.createLineBorder(Color.black));

            add(cardArea);
            add(cardInfo);
        }

        public final void setImage(ImageIcon icon){
            cardArea.setIcon(icon);
        }
    }

But that's what happens: deck manager In the MVCE:

label to the right

The cardArea gets to the right, and I don't understand why.

The card I select in the Table goes without problems to the selected card panel. Why is the JLabel getting to the right?


Solution

  • The problem is with the BoxLayout. I would instead recommend using a BorderLayout And get rid of all the set[Preferred]Sizes. As for the text area, use the constructor JTextArea(rows, cols). For the columns you can leave at 0, and with BorderLayout, it will stretch to match the width of the image

    enter image description here

    import java.awt.*;
    import javax.swing.*;
    
    public class SelectCardPanel extends JPanel{
        private final JLabel cardArea;
        private final JTextArea cardInfo;
    
        public static void main(String args[]){
            java.awt.EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JFrame frame = new JFrame("Yu-Gi-Oh!");
                    SelectCardPanel panel = new SelectCardPanel();
                    panel.setImage(new ImageIcon(SelectCardPanel.class.getResource("images.jpg")));
                    frame.add(panel);
                    frame.pack();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                }
            });
        }
    
        public SelectCardPanel(){
            super();
    
            setLayout(new BorderLayout());
            //setPreferredSize(new Dimension(200, 400));
    
            cardArea = new JLabel();
            cardArea.setOpaque(true);
            cardArea.setBackground(Color.white);
            //cardArea.setPreferredSize(new Dimension(200, 300));
            //cardArea.setSize(cardArea.getPreferredSize());
            cardArea.setBorder(BorderFactory.createLineBorder(Color.black));
    
            cardInfo = new JTextArea(5, 0);
            cardInfo.setEditable(false);
            cardInfo.setWrapStyleWord(true);
            cardInfo.setLineWrap(true);
            //cardInfo.setPreferredSize(new Dimension(200, 100));
            cardInfo.setBorder(BorderFactory.createLineBorder(Color.black));
    
            add(cardArea);
            add(cardInfo, BorderLayout.PAGE_END);
        }
    
        public final void setImage(ImageIcon icon){
            cardArea.setIcon(icon);
        }
    }