Add Jcomponents to a Jdialog/Jframe

Asked

Viewed 229 times

0

So I have three JLabel and JTextField to add in one panel, and two JButton and a JTable to add in another panel, so that one stays below the other. I create a more generic panel (pnlPrincipal) to set their positions through the BorderLayout., I add everything right, only in JDialog only individual components appear.

Stretch where I add the panels :

    pnlPrincipal = new JPanel(new BorderLayout()); //Painel Principal
    getContentPane().add(BorderLayout.CENTER, pnlPrincipal);

    pnlFunc = new JPanel(new BorderLayout()); //Painel dos dados do funcionário
    pnlFunc.setLayout(null);
    pnlFunc.setPreferredSize(new Dimension (610, 150));
    pnlPrincipal.add(BorderLayout.NORTH, pnlFunc);

    pnlDep = new JPanel(new BorderLayout()); //Painel do grid
    pnlDep.setLayout(null);
    pnlDep.setPreferredSize(new Dimension (610, 300));
    pnlDep.setBorder(new TitledBorder("Dependentes"));
    pnlPrincipal.add(BorderLayout.SOUTH, pnlDep);

After that part of the code, I give new to all the components I will use in pnlFunc, and then add them to the panel:

    pnlFunc.add(lblNome);
    pnlFunc.add(lblCpf);
    pnlFunc.add(lblEnd);
    pnlFunc.add(txtNome);
    pnlFunc.add(txtCpf);
    pnlFunc.add(txtEnd);

After that stretch, I give new in all the components that I will use in pnlDep:

    btnAdd = new JButton("Adicionar");
    btnAdd.setBounds(25, 110, 100, 20);
    btnRemove = new JButton("Remover");
    btnRemove.setBounds(135, 110, 100, 20);
    pnlDep.add(btnAdd);
    pnlDep.add(btnRemove);

    scr = new JScrollPane();
    dtm = new DefaultTableModel(new String[]{"Nome","Data de nascimento"},10);
    grid = new JTable(dtm);

    grid.getTableHeader().setReorderingAllowed(false);
    grid.setCellSelectionEnabled(true);
    grid.setRowSelectionAllowed(false);

    grid.getColumnModel().getColumn(0).setPreferredWidth(200);
    grid.getColumnModel().getColumn(0).setResizable(false);

    grid.getColumnModel().getColumn(1).setPreferredWidth(200);
    grid.getColumnModel().getColumn(1).setResizable(false);

    scr.setViewportView(grid);
    pnlDep.add(BorderLayout.CENTER, scr);

Then I add two more separate buttons in the window, and they are the only ones that appear in the window, the rest of the elements do not appear ... Note: The window size is: 650x500 !

  • At what point are you adding the pnlDep no dialog? Also, pnlDep.add(btnRemove);, you should set the position in the layout.

  • In the fourth row :getContentPane().add(BorderLayout.CENTER, pnlDep);, but the method .setBounds(); of btnRemove doesn’t do it for me anymore ? '-'

  • Who do you add first: the Jpanel or the scrollpane? The latter will take up all the space for you if no size is set. Add as you are adding all components in the dialog as well, the problem may be the order.

  • @Diegof See my question, I changed some things !

  • When the sense of it? pnlFunc = new JPanel(new BorderLayout()); 
 pnlFunc.setLayout(null);

  • Try to give a pack() after adding all panels in the Frame/Dialog.

Show 1 more comment
No answers

Browser other questions tagged

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