0

I am creating a Java application which exists out of a jFrame that is empty upon start and can include an undefined amount of instances of a jPanel. image: Example picture of the interface

The idea is that every time the user presses 'Nieuw subnet aanmaken' a new instance of the object Subnet is made, and is stored in a arraylist. The user enters data through a series of pop-ups.

After the creation of the object a jPanel 'Netwerk' is created, with the goal of displaying the network's details. Is it possible that I supply that jPanel with a integer specifying the location of its correcponding 'Subnet' (the logical class, containing, subnetmask, name, ..) in the list that is kept in the jFrame, so that it can acces and write to that instance in the list?

Like:

private void jButton_NetwerkenActionPerformed(java.awt.event.ActionEvent evt) {                                                  
    //create new subnet in the arraylist at location i
    //..
    //then make a new panel and suply it with the index of its subnet object in the arraylist
    this.jPanel1.add(new Netwerk(i));
}
Bjorn De Rijcke
  • 653
  • 10
  • 23
  • You said you store the panels in an `ArrayList` how come you can't access each panel like `JPanel panel = panelsList.get(index)`? – Paul Samsotha May 29 '14 at 15:39
  • @peeskillet The logical instances (containing variables with Hosts, Names, subnetmask, etc. ) are stored in a ArrayList. The panels are created inside a jScrollpane – Bjorn De Rijcke May 29 '14 at 15:43
  • Yes, you can. Suggest that instead of sending the JPanel an integer index into a List, just send the actual Netwerk (looks like your code does that). In the long run, something other than the JFrame should probably hold the List. E.g. what if user closes the JFrame? – user949300 May 29 '14 at 15:44
  • @user949300 The thing I wanted to avoid is giving each panel a logical instance for its own, because then only the jPanel would contain correct information. And I would like to be able to export the ArrayList with logical instances out of the jFrame into MySQl so that the entire network is updated in mysql. Instead of having to let each instance of the jPanel communicate with MySQL to update the database with correct info for its own subnet. – Bjorn De Rijcke May 29 '14 at 15:48
  • This looks like the kind of Swing application where each JPanel would be backed by a [GUI model class](http://en.wikipedia.org/wiki/Model_View_Controller), and you'd keep a List of the GUI model classes for however many JPanels you display / use. – Gilbert Le Blanc May 29 '14 at 17:02
  • @GilbertLeBlanc Thanks for your input on this, though I had no idea how to implement such a design. Therefor I do some more research came up with a 'solution' myself, though I wonder if it can be considered as good code. – Bjorn De Rijcke May 29 '14 at 20:46

1 Answers1

0

I ended up fixing the issue by keeping a list of my Netwerk instances and applying a ActionListener for every button on the Netwerk panels, and keeping the logic in my jFrame, I don't know if it is good practice, but it does the job.

Code when the button 'Nieuw Subnet Aanmaken' is pressed:

private void jButton_NetwerkenActionPerformed(java.awt.event.ActionEvent evt) {                                                  
    try {
            bedrijf.addSubnet(netwerkNaam, netwerkAdres, subnetmask);
    } catch(IllegalArgumentException | NullPointerException e) {
            JOptionPane.showMessageDialog(null, "Fout: " + e.getMessage(), "Error!", JOptionPane.PLAIN_MESSAGE);
    }
    this.refreshScreen();
}   

code for the refreshScreen method:

Bedrijf bedrijf;
List<Netwerk> panelen = new ArrayList<>();

//...

    private void refreshScreen() {
    //reset list with panels
    panelen.clear();

    //init counter
    int i = 0;

    List<Subnet> subnets = bedrijf.getSubnets();

    //empty the interface
    jPanel1.removeAll();

    //create a 'Netwerk' jPanel for each subnet
    for (Subnet element : subnets) {
        Netwerk n = new Netwerk(element.getNaam(), element.getNetwerkAdres(), element.getSubnetMask(), element.getHosts());
        final int finalI = i;
        i++;

        //<editor-fold defaultstate="collapsed" desc="ActionListenersKnoppen">
        n.jButton_Naam.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_In_Panel_ActionPerformed(evt, finalI, "jButton_Naam");
            }
        });

        //... some more ActionListeners here ...

        n.jButton_VerwijderSubnet.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_In_Panel_ActionPerformed(evt, finalI, "jButton_VerwijderSubnet");
            }
        });
        //</editor-fold>

        jPanel1.add(n);
        panelen.add(n);//thanks to Oscar Ryz @ http://stackoverflow.com/questions/370310/java-get-jpanel-components
    }
    repaint();
}

A int FinalI is used to keep track of which jPanel the pressed button belongs to.

Bjorn De Rijcke
  • 653
  • 10
  • 23