Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding JPanel to Jframe dynamically

Tags:

java

swing

jpanel

I have a form on my Java application which basically is to provide the user with a list of data from a DB query. one idea was to use a table and populate each row with data from my resultset. However when designing the UI my team and i decided that it didnt look as smooth as we wanted. So we thought we would look into creating a custom view of the results in a panel. we wanted it to look something like: Desired custom view

So instead of rows of a table it will look like this with one for each enquiry on the resultset.

The problem i am having is coding this. I spent a good deal of time trying to workout how to add a component to a JForm. as netbeans seem to by default set up the ui as a grouplayout? so I worked out how to add 1 panel using:

javax.swing.JLabel idLbl;
 javax.swing.JLabel jLabel1;
 javax.swing.JLabel jLabel3;
 javax.swing.JLabel jLabel5;
 javax.swing.JLabel jLabel7;
 javax.swing.JPanel jPanel1;
 javax.swing.JLabel prefContactLbl;
 javax.swing.JLabel propertyLabel;
    jPanel1 = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    idLbl = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    propertyLabel = new javax.swing.JLabel();
    jLabel5 = new javax.swing.JLabel();
    contactLabel = new javax.swing.JLabel();
    jLabel7 = new javax.swing.JLabel();
    prefContactLbl = new javax.swing.JLabel();

    jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

    jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
    jLabel1.setText("Enquiry Id:");
    jLabel1.setName("jLabel"+i);

    idLbl.setText("jLabel2");

    jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12));
    jLabel3.setText("Property:");

    propertyLabel.setText("A property Address in some town with a postcode");

    jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12));
    jLabel5.setText("Contact:");

    contactLabel.setText("A Persons Name ( 01010100011)");

    jLabel7.setFont(new java.awt.Font("Tahoma", 1, 12));
    jLabel7.setText("Prefered Contact:");

    prefContactLbl.setText("Email/Phone");

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(jLabel1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(idLbl)
                    .addGap(18, 18, 18)
                    .addComponent(jLabel3)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(propertyLabel))
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(jLabel7)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(prefContactLbl)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jLabel5)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(contactLabel)))
            .addContainerGap(20, Short.MAX_VALUE))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1)
                .addComponent(idLbl)
                .addComponent(jLabel3)
                .addComponent(propertyLabel))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel7)
                .addComponent(prefContactLbl)
                .addComponent(jLabel5)
                .addComponent(contactLabel))
            .addContainerGap())
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
     getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(10 , 10, 10)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                 .addContainerGap(100, Short.MAX_VALUE))
    );

    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(570, Short.MAX_VALUE))
    );

The main problem I am faced is adding more than 1 dynamically I am completly unsure how to go about it as all i can find is to set the layout not update it. And I cant set how many .addcomponent as its dynamic. Really confused on how to go about this.

Sorry if its hard to work out what I am trying to get across easier to think of it but putting it into words is a nightmare.

like image 889
Vade Avatar asked Dec 28 '22 23:12

Vade


1 Answers

Starting off by encapsulating things a bit more will make your life much easier. Make a subclass of JPanel that represents the view for one enquiry entry: EnquiryPanel. The EnquiryPanel would have code in it that nearly matches what you pasted above, except that it will fill itself in from the constructor parameters that you pass in and it will be a JPanel itself.

public class EnquiryPanel extends JPanel {
   public EnquiryPanel(Result dbResult) {
     // your layout code from above, but this is the content pane and
    // the fields are populated from the dbResult object
   }
}

You can then dynamically add instances of EnquiryPanel to a container panel whose layout is a vertical BoxLayout. I call this container panel ResultsPanel below.

public class ResultsPanel extends JPanel {
  public ResultsPanel() {
    // Layout our contents vertically
    BoxLayout layout = new BoxLayout(this, BoxLayout.Y_AXIS);
    this.setLayout(layout);
  }
  // method to iterate over the results from the database
  // construct an EnquiryPanel and add that EnquiryPanel to ourselves
  public void layoutEnquiryResults(ResultCollection results) {
     for (Result result : results) {
        EnquiryPanel eqPanel = new EnquiryPanel(result);
        add(eqPanel);
     }
     revalidate();
  }
}

Finally you would add an instance of ResultsPanel to your JFrame.

ResultsPanel resultsPanel = new ResultsPanel();
resultsPanel.layoutEnquiryResults(myResultSet);
getContentPane().add(resultsPanel);
like image 84
Matt Wonlaw Avatar answered Jan 11 '23 10:01

Matt Wonlaw