The JButtons do not occupy the full width of the JPanel with GridBagLayout, how do I solve it?

1

I have a problem that when I load the buttons with the SQLite data when adding them to the panel with a While traversing the DB, I do not add them to them in the form of a vertical list, nor does it expand them to the size of the JPanel. contains, Even though I put what is necessary .. Can someone help me? If so, thank you in advance ..

Here's the While:

public void cargarBtnCustomers(){
    try{
        Connection miConexion = DriverManager.getConnection("jdbc:sqlite:customers_1.db");
    Statement miStatement = miConexion.createStatement();
    ResultSet miResultSet = miStatement.executeQuery("SELECT * FROM customers_1");

    while(miResultSet.next()){
            //pnlIns.add(new JButton(new PersonAction(new Person(miResultSet.getString("name"), miResultSet.getString("identification")))).setBackground(Color.yellow));
            Person p = new Person(miResultSet.getString("name"),
            miResultSet.getString("ruc"), miResultSet.getString("repLegal"),
            miResultSet.getString("nombreContactoEmpresa"), miResultSet.getString("correoContactoEmpresa"),
            miResultSet.getString("numeroContacto"), miResultSet.getString("cedulaRepLegal"),
            miResultSet.getString("avisoOperaciones"), miResultSet.getString("nit"),
            miResultSet.getString("noContribuyenteMuniPa"), miResultSet.getString("contraMuniPa"),
            miResultSet.getString("obligacionFiscal"));
            // create a PersonAction with the recently created person as parameter
            PersonAction pa = new PersonAction(p);
            // create the JButton passing the PersonAction as parameter
            myBtn jb = new myBtn(pa);
            // set the background of the JButton
            jb.setText(miResultSet.getString("name"));

            //DESDE AQUI EMPIEZAN MIS PROBLEMAS

            GridBagConstraints c = new GridBagConstraints();

            iterador++;

            c.fill = GridBagConstraints.HORIZONTAL;
            c.weightx = 2.0;
            c.weighty = 2.0;
            c.ipadx = 930;
            //c.gridwidth = 3;
            c.anchor = GridBagConstraints.LINE_START;
            //c.insets = new Insets(0,5,0,5);
            c.gridx = iterador;
            c.gridy = 1;


            pnlClientes.add(jb, c);
            pnlClientes.revalidate();
            pnlClientes.repaint();

            c = new GridBagConstraints();
        }
    }catch(Exception e){
        System.out.println(e);
    }

}

    
asked by Extibax 29.09.2018 в 03:23
source

2 answers

0

I found a solution by doing the JScrollPane and the plnClientes (I mean the container of the JButtons) from zero to pure code, I was using the NetBeans designer and apparently I modified each of the Panels that were introduced to a ScrollPane, I would like know why and how to solve that using the NetBeans ..

What I did was create an individual class from the JPanelClients:

public class pnlClientes extends JPanel{



public pnlClientes() {

    setLayout(new GridBagLayout());
    setBackground(Color.red);//Le agregue para ver el JPanel
    setVisible(true);

    JScrollPane pnlScroll = new JScrollPane();
    JPanel pnlClientes = new JPanel();
    setOpaque(false);

    pnlScroll.setViewportView(pnlClientes);

    pnlScroll.add(pnlClientes);
    pnlScroll.getViewport().setOpaque(false);

}

}

Then add it to the main class:

public void cargarBtnCustomers(){
    try{
        Connection miConexion = DriverManager.getConnection("jdbc:sqlite:customers_1.db");
    Statement miStatement = miConexion.createStatement();
    ResultSet miResultSet = miStatement.executeQuery("SELECT * FROM customers_1");

    while(miResultSet.next()){
            //pnlIns.add(new JButton(new PersonAction(new Person(miResultSet.getString("name"), miResultSet.getString("identification")))).setBackground(Color.yellow));
            Person p = new Person(miResultSet.getString("name"),
            miResultSet.getString("ruc"), miResultSet.getString("repLegal"),
            miResultSet.getString("nombreContactoEmpresa"), miResultSet.getString("correoContactoEmpresa"),
            miResultSet.getString("numeroContacto"), miResultSet.getString("cedulaRepLegal"),
            miResultSet.getString("avisoOperaciones"), miResultSet.getString("nit"),
            miResultSet.getString("noContribuyenteMuniPa"), miResultSet.getString("contraMuniPa"),
            miResultSet.getString("obligacionFiscal"));
            // create a PersonAction with the recently created person as parameter
            PersonAction pa = new PersonAction(p);
            // create the JButton passing the PersonAction as parameter
            myBtn jb = new myBtn(pa);
            // set the background of the JButton
            jb.setText(miResultSet.getString("name"));




            GridBagConstraints c = new GridBagConstraints();

            iterador++;

            c.fill = GridBagConstraints.HORIZONTAL;
            c.weightx = 2.0; 
            c.insets = new Insets(2, WIDTH, 2, HEIGHT);
            c.gridy = iterador; 
            pnlC.add(jb, c);
            pnlC.revalidate();
            pnlC.repaint();

            c = new GridBagConstraints();


        }
    }catch(Exception e){
        System.out.println(e);
    }

}

And ready, I'm so:

Any recommendations to me:)

    
answered by 30.09.2018 / 17:05
source
1

Update:

By borrowing the code you provide:

  //...

  while(miResultSet.next()){
      //pnlIns.add(new JButton(new PersonAction(new Person(miResultSet.getString("name"),   miResultSet.getString("identification")))).setBackground(Color.yellow));
      Person p = new Person(miResultSet.getString("name"),
      miResultSet.getString("ruc"), miResultSet.getString("repLegal"),
      miResultSet.getString("nombreContactoEmpresa"),
      miResultSet.getString("correoContactoEmpresa"), 
      miResultSet.getString("numeroContacto"),  
      miResultSet.getString("cedulaRepLegal"), 
      miResultSet.getString("avisoOperaciones"), 
      miResultSet.getString("nit"), 
      miResultSet.getString("noContribuyenteMuniPa"), 
      miResultSet.getString("contraMuniPa"), 
      miResultSet.getString("obligacionFiscal"));
      // create a PersonAction with the recently created person as parameter
      PersonAction pa = new PersonAction(p);
      // create the JButton passing the PersonAction as parameter
      myBtn jb = new myBtn(pa);
      // set the background of the JButton
      jb.setText(miResultSet.getString("name"));

      GridBagConstraints c = new GridBagConstraints();

        iterador++;

        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 2.0; //Toma el ancho total del contenedor
        c.gridy = iterador; //Agrega filas al GridBagLayout
        pnlClientes.add(jb, c);
  }
  //...

    
answered by 29.09.2018 в 08:44