Problem inserting Jtextfield inside Jpanel inside Jtabbedpane

Asked

Viewed 135 times

1

I have a Swing app where the main window consists of a JtabbedPane which contains some extended classes JPanel [Viewprincipal, Viewcadastro, Viewrelatorio, etc...].

When I add Jlabels and Jbuttons, they appear and windows work normally, but when I prompt a JTextField or JTextArea within one of those classes that extend JPanel, the Jtabbedpanel Buga and nothing appears.

If you try this.setLayout(null) Jtabbedpane and Jpanels work, but no internal components of Jpanels appear. I feel that setar null layout is very ugly.

What might be going on?

Putting as little as possible of the Viewcadastro code we have:

public class ViewCadastro extends JPanel{

        public ViewCadastro(){
            this.gettingStart();
        }

        public void gettingStart(){
            this.setBackground(Color.lightGray);    

            this.painelHeader = new JPanel();       
            this.painelHeader.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
            this.painelActions = new JPanel();
            this.painelActions.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));

            this.labTitulo = new JLabel("Titulo: ");
            this.painelHeader.add(labTitulo);
            //this.txTitulo = new JTextField();
            //this.painelHeader.add(txTitulo);

            this.btnAdicionarFilme = new JButton("Add");        

            this.painelActions.add(btnAdicionarFilme);
            this.add(painelHeader);
            this.add(painelActions);    

        }


        private JButton btnAdicionarFilme;
        private JPanel painelHeader;
        private JPanel painelActions;
        public JLabel labTitulo;
        public JTextField txTitulo; 
}

If you take the comments out, he’ll make fun of everything.

And the Main View:

public class View extends JFrame{


    public View(Control controller){
        this.setCtrl(controller);
        this.gettingStart();
    }


    public void gettingStart(){
        this.setVisible(true);
        this.setResizable(false);
        this.setSize(1000,500);


        tabs = new JTabbedPane();
        painelPrincipal = new ViewPrincipal();
        painelCadastro = new ViewCadastro();
        painelRelatorios = new ViewRelatorios();


        //Adicionando paineis

        tabs.add("Principal",painelPrincipal);
        tabs.add("Cadastros",painelCadastro);
        tabs.add("Relatórios",painelRelatorios);

        this.add(tabs);
    }

    private Control ctrl;   
    private ViewCadastro painelCadastro;
    private ViewRelatorios painelRelatorios;
    private ViewPrincipal painelPrincipal;
    private JTabbedPane tabs;
}

The main control:

public class Control {

    private View viewer;

    public Control(){
        setViewer(new View(this));
    }

    public View getViewer() {
        return viewer;
    }

    public void setViewer(View viewer) {
        this.viewer = viewer;
    }
}

Pulled by the big main pattern:

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Control();

    }

}

1 answer

1

PROBLEM SOLVED:

In the main View (not where I instate Jtextfield, but in the class that expands Jframe] I set Jframe '.setVisible(true);' at the beginning of the window startup. Just changing these commands to the end of the function has solved the problem.

Current code:

public class View extends JFrame{


    public View(Control controller){
        this.setCtrl(controller);
        this.gettingStart();
    }


    public void gettingStart(){



        tabs = new JTabbedPane();
        painelPrincipal = new ViewPrincipal();
        painelCadastro = new ViewCadastro();
        painelRelatorios = new ViewRelatorios();


        //Adicionando paineis

        tabs.add("Principal",painelPrincipal);
        tabs.add("Cadastros",painelCadastro);
        tabs.add("Relatórios",painelRelatorios);

        this.add(tabs);

        this.setVisible(true);
        this.setResizable(false);
        this.setSize(1000,500);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private Control ctrl;   
    private ViewCadastro painelCadastro;
    private ViewRelatorios painelRelatorios;
    private ViewPrincipal painelPrincipal;
    private JTabbedPane tabs;

    public Control getCtrl() {
        return ctrl;
    }

    public void setCtrl(Control ctrl) {
        this.ctrl = ctrl;
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.