user3646130
user3646130

Reputation: 93

JPanel won't appear in JTabbedPane?

My project has two tabs, 1 is called "netwerken" and the other is called routers. In my "netwerken" tab i made a reference to a class that is called "Netwerken.java", that class extends JPanel. And in that panel i made another reference to the class "Subnets.java" that also extends JPanel. So actually I made a panel in a panel in a tab. But what did i do wrong and why won't my buttons, textfields, labels and textareas pop-up on my screen?

P.S. I made this project in Netbeans and linked the panels to each other by clicking on the Panel design and than add custom code: new "nameOfClass"();

Here is the code of my JFrame:

package subnetmanager;

public class Subnetmanager extends javax.swing.JFrame {

/**
 * Creates new form Subnetmanager
 */
public Subnetmanager() {
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    databank = new javax.swing.JLabel();
    tabladen = new javax.swing.JTabbedPane();
    netwerken = new Netwerken();
    routers = new Routers();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    databank.setText("Databank");

    javax.swing.GroupLayout netwerkenLayout = new javax.swing.GroupLayout(netwerken);
    netwerken.setLayout(netwerkenLayout);
    netwerkenLayout.setHorizontalGroup(
        netwerkenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 395, Short.MAX_VALUE)
    );
    netwerkenLayout.setVerticalGroup(
        netwerkenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 252, Short.MAX_VALUE)
    );

    tabladen.addTab("Netwerken", netwerken);

    javax.swing.GroupLayout routersLayout = new javax.swing.GroupLayout(routers);
    routers.setLayout(routersLayout);
    routersLayout.setHorizontalGroup(
        routersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 395, Short.MAX_VALUE)
    );
    routersLayout.setVerticalGroup(
        routersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 252, Short.MAX_VALUE)
    );

    tabladen.addTab("Routers", routers);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(tabladen)
        .addGroup(layout.createSequentialGroup()
            .addComponent(databank)
            .addGap(0, 0, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(databank)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(tabladen))
    );

    pack();
}

public static void main(String args[]) {
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Subnetmanager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Subnetmanager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Subnetmanager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Subnetmanager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Subnetmanager().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JLabel databank;
private javax.swing.JPanel netwerken;
private javax.swing.JPanel routers;
private javax.swing.JTabbedPane tabladen;
// End of variables declaration                   
}

Here is the code of my "netwerken"-class:

package subnetmanager;

public class Netwerken extends javax.swing.JPanel {

public Netwerken() {
    initComponents();
}

@SuppressWarnings("unchecked")                         
private void initComponents() {

    subnet = new Subnet();

    javax.swing.GroupLayout subnetLayout = new javax.swing.GroupLayout(subnet);
    subnet.setLayout(subnetLayout);
    subnetLayout.setHorizontalGroup(
        subnetLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 200, Short.MAX_VALUE)
    );
    subnetLayout.setVerticalGroup(
        subnetLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 150, Short.MAX_VALUE)
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(subnet, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(0, 200, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(subnet, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(0, 150, Short.MAX_VALUE))
    );
}                       


// Variables declaration - do not modify                     
private javax.swing.JPanel subnet;
// End of variables declaration                   
}

And last but not least is the code of my "Subnet"-class:

package subnetmanager;

public class Subnet extends javax.swing.JPanel {

/**
 * Creates new form Subnet
 */
public Subnet() {
    initComponents();
}

@SuppressWarnings("unchecked")                        
private void initComponents() {

    naamLabel = new javax.swing.JLabel();
    naam = new javax.swing.JTextField();
    wijzigNaam = new javax.swing.JButton();
    netwerkadresLabel = new javax.swing.JLabel();
    netwerkadres = new javax.swing.JTextField();
    wijzigNetwerkadres = new javax.swing.JButton();
    subnetmaskLabel = new javax.swing.JLabel();
    subnetmask = new javax.swing.JTextField();
    wijzigSubnetmask = new javax.swing.JButton();
    hostsLabel = new javax.swing.JLabel();
    jScrollPane1 = new javax.swing.JScrollPane();
    hosts = new javax.swing.JTextArea();
    voegHostToe = new javax.swing.JButton();
    verwijderHost = new javax.swing.JButton();

    naamLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
    naamLabel.setText("Naam:");

    wijzigNaam.setText("wijzig");

    netwerkadresLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
    netwerkadresLabel.setText("Netwerkadres:");

    wijzigNetwerkadres.setText("wijzig");
    wijzigNetwerkadres.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            wijzigNetwerkadresActionPerformed(evt);
        }
    });

    subnetmaskLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
    subnetmaskLabel.setText("Subnetmask:");

    wijzigSubnetmask.setText("wijzig");

    hostsLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
    hostsLabel.setText("Hosts:");

    hosts.setColumns(20);
    hosts.setRows(5);
    jScrollPane1.setViewportView(hosts);

    voegHostToe.setText("Voeg host toe");

    verwijderHost.setText("Verwijder");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(netwerkadresLabel)
                .addComponent(naamLabel)
                .addComponent(subnetmaskLabel)
                .addComponent(hostsLabel))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(voegHostToe, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(verwijderHost, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE))
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(naam)
                        .addComponent(netwerkadres)
                        .addComponent(subnetmask))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(wijzigNaam))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(10, 10, 10)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(wijzigSubnetmask)
                                .addComponent(wijzigNetwerkadres, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addComponent(jScrollPane1))
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(naamLabel)
                .addComponent(naam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(wijzigNaam))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(netwerkadresLabel)
                .addComponent(netwerkadres, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(wijzigNetwerkadres))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(subnetmaskLabel)
                .addComponent(subnetmask, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(wijzigSubnetmask))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(hostsLabel)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(voegHostToe)
                .addComponent(verwijderHost))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
}

// Variables declaration - do not modify                     
private javax.swing.JTextArea hosts;
private javax.swing.JLabel hostsLabel;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField naam;
private javax.swing.JLabel naamLabel;
private javax.swing.JTextField netwerkadres;
private javax.swing.JLabel netwerkadresLabel;
private javax.swing.JTextField subnetmask;
private javax.swing.JLabel subnetmaskLabel;
private javax.swing.JButton verwijderHost;
private javax.swing.JButton voegHostToe;
private javax.swing.JButton wijzigNaam;
private javax.swing.JButton wijzigNetwerkadres;
private javax.swing.JButton wijzigSubnetmask;
// End of variables declaration                   
}

Upvotes: 0

Views: 748

Answers (2)

Paul Samsotha
Paul Samsotha

Reputation: 208994

" I made this project in Netbeans and linked the panels to each other by clicking on the Panel design and than add custom code: new "nameOfClass"();"

I would avoid adding custom code to the auto-generated code region, unless completely necessary. In which case, you should really know what you're doing. There are many other options if you understand how to use the gui builder tool correctly.

For example, you can just drag and drop your panel forms (instead trying to programmatically instantiate them, in which case, the layout may affect what is visible and what is not - I'm not an expert in GroupLayoup, so I couldn't point you to the problem).

enter image description here


Also you don't have to use the default GroupLayout. If you understand how the different layout managers work, you can just right click on the container from the navigator or design view and select Set Layout.


You can also see an example of How to Use CardLayout with Netbeans GUI Builder if you wanted to switch the views without using tabs.


UPDATE

"I would do it also thath way, but the problem is dat i need to program my project so can i can add many subnets to my "netwerken" tab"

Then you should use a different layout manager for your netwerken panel. GroupLayout isn't the friendliest to work with when dynamically adding components. Use a layout like BoxLayout. Like I said above, just select the panel from the navigator and select Set Layout. When you want to add a new SubNet during runtime, just add it, then revalidate() and repaint() the metwerken panel.

Upvotes: 0

user1803551
user1803551

Reputation: 13427

You are creating a class which already specified a layout inside itself, and then specify a layout to it externally which overrides it.

Inside Subnet you have

    GroupLayout layout = new GroupLayout(this);
    this.setLayout(layout);

Then in Netwerken you override it with

        subnet = new Subnet();
//      GroupLayout subnetLayout = new GroupLayout(subnet);
//      subnet.setLayout(subnetLayout);
//      subnetLayout.setHorizontalGroup(subnetLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 200, Short.MAX_VALUE));
//      subnetLayout.setVerticalGroup(subnetLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 150, Short.MAX_VALUE));

Then again, inside Netwerken you have

    GroupLayout layout = new GroupLayout(this);
    this.setLayout(layout);

And you override it with

//      GroupLayout netwerkenLayout = new GroupLayout(netwerken);
//      netwerken.setLayout(netwerkenLayout);
//      netwerkenLayout.setHorizontalGroup(netwerkenLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 395, Short.MAX_VALUE));
//      netwerkenLayout.setVerticalGroup(netwerkenLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 252, Short.MAX_VALUE));

Remove the lines I commented out.

Note: you will do yourself well if you code the GUI by hand and not with an editor.

Upvotes: 1

Related Questions