Search code examples
javaswingjpaneljtabbedpane

JTabbedPane not showing until I minimize the window


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

public class ModuloAdministracion extends JFrame {

JTabbedPane tabbedPane;
JPanelProfesores jPanelProfesores;
JPanelCursos jPanelCursos;
JPanelAlumnos jPanelAlumnos;


public static void main(String[] args) {
    new ModuloAdministracion();
}

public ModuloAdministracion(){
    setInicio();
    tabbedPane=new JTabbedPane();

    jPanelProfesores=new JPanelProfesores();
    jPanelCursos=new JPanelCursos();
    jPanelAlumnos=new JPanelAlumnos();

    tabbedPane.setFont(new Font("Arial", Font.BOLD, 20));

    Color myColor=Color.decode("#345FE3");
    Color myColor2=Color.decode("#FBFCFC");

    tabbedPane.setBackground(myColor);
    tabbedPane.setForeground(myColor2);

    tabbedPane.add("Profesores", jPanelProfesores);
    tabbedPane.add("Cursos", jPanelCursos);
    tabbedPane.add("Alumnos", jPanelAlumnos);
    getContentPane().add(tabbedPane);
    tabbedPane.repaint();
}

public void setInicio(){
    setTitle("Modulo de administracion");
    setVisible(true);
    //setLayout(null);
    setLocation(125, 50);
    setSize(1100, 650);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setResizable(false);

}

public void addLabel(String titulo, int x, int y, int width, int height){
    JLabel anadirLabel=new JLabel(titulo);
    anadirLabel.setBounds(x,y,width,height);
    anadirLabel.setFont(new Font("Arial", Font.BOLD, 50));
    Color myColor = Color.decode("#345FE3");
    anadirLabel.setForeground(myColor);
    add(anadirLabel);
    repaint();
}

public void addLabel1(String titulo, int x, int y, int width, int height){
    JLabel anadirLabel=new JLabel(titulo);
    anadirLabel.setBounds(x,y,width,height);
    anadirLabel.setFont(new Font("Arial", Font.BOLD, 25));
    Color myColor = Color.decode("#FBFCFC");
    anadirLabel.setForeground(myColor);
    add(anadirLabel);
    repaint();
}

public JTextField addTextfield(String texto, int x, int y, int width, int height){
    JTextField txtUser=new JTextField(texto);
    txtUser.setBounds(x,y,width,height);
    txtUser.setFont(new Font("Arial", Font.PLAIN, 18));
    Color myColor = Color.decode("#1C2833");
    txtUser.setBackground(myColor);
    Color myColor2 = Color.decode("#FBFCFC");
    txtUser.setForeground(myColor2);
    add(txtUser);
    repaint();
    return txtUser;
}

public JPasswordField addPasswordField(String texto, int x, int y, int width, int height){
    JPasswordField passUser=new JPasswordField(texto);
    passUser.setBounds(x,y,width,height);
    passUser.setFont(new Font("Arial", Font.PLAIN, 18));
    Color myColor = Color.decode("#1C2833");
    passUser.setBackground(myColor);
    Color myColor2 = Color.decode("#FBFCFC");
    passUser.setForeground(myColor2);
    add(passUser);
    repaint();
    return passUser;
}

}

I have been working on a project, but I am having some issues with the JTabbedPane which is a new component for me. I created separate windows that contain my JPanel containers and they were added but when I run the program, but it just shows a blank window until I click on minimize the window. At the moment it doesn't contain events or does anything especial, but I have no idea why it doesn't show up initially when I run the program.

  1. Running component
  2. After minimizing component

Solution

  • setInicio();
    

    Components need to be added to the frame BEFORE the frame is made visible.

    The above code needs to be moved to the end of your constructor.