Victor Oliveira
Victor Oliveira

Reputation: 3713

JTabbedPane image alignment

I made a quick research to solve this problem but until now I found nothing regarding to this. I have one image into one TabbedPane object but when I try to align this image on the center of the label inside the TabbedPane it "Doesn't" work. The center alignment in this case works only for horizontal view but I want to be in the center of both vertical and horizontal. Check out the sample below:

import java.awt.BorderLayout;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import com.sh.st.gui.MainScreen;

public class test {

    JTabbedPane tabbedPane = new JTabbedPane();
    JFrame mainFrame = new JFrame();

public static void main (String[] args){

        test t = new test();

    }

    public test(){

        JPanel entrance = new JPanel();
        JLabel lbImage1;
        JMenuBar bar;
        JMenu file, registerQuery;
        ImageIcon Logo= new ImageIcon("rsc/img/imagem.jpg");        


        lbImage1= new JLabel(Logo, JLabel.CENTER);
            entrance.add(lbImage1);
                tabbedPane.addTab("Entrance", null, entrance);
                    mainFrame.getContentPane().add( tabbedPane, BorderLayout.CENTER);
                        bar= new JMenuBar();
                            file= new JMenu("File");
                                registerQuery= new JMenu("Request");
                                    mainFrame.setVisible(true);
    }   

}

I guess its not so hard to do what I want but until now as I said, I found nothing, anyone could help please? thanks in advance

Upvotes: 0

Views: 129

Answers (1)

Reimeus
Reimeus

Reputation: 159844

The JLabel alignment will only center horizontally due to the potitioning characteristics of its parent container. In this case it is the default layout for JPanel which is FlowLayout. This layout manager does not facilitate easy vertical alignment.

Provided that the JLabel lbImage1 will be the only component on the JPanel entrance, then GridLayout can be used to center the JLabel both horizontally and vertically:

entrance.setLayout(new GridLayout());

Upvotes: 2

Related Questions