Trying to dynamically generate tabs on a JTabbedPane with content but getting an Exception in thread "AWT-EventQueue-0"

larrydalmeida

I am trying to dynamically generate tabs and their containing textfields based on input given by the user. The input is noTCPFlows in the code shown. But when I run the code I keep getting this exception:

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
    at java.util.ArrayList.rangeCheck(ArrayList.java:635)
    at java.util.ArrayList.get(ArrayList.java:411)
    at javax.swing.JTabbedPane.setTitleAt(JTabbedPane.java:1316)
    at myproject.ns2GUI.trafficInfoInit(ns2GUI.java:622)
    at myproject.ns2GUI.flowDetailsContinueActionPerformed(ns2GUI.java:567)
    at myproject.ns2GUI.access$200(ns2GUI.java:16)
    at myproject.ns2GUI$3.actionPerformed(ns2GUI.java:343)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

and nothing is displayed on screen.

I have shown the code below. Can someone please help?

package myproject;
import java.awt.*;
import java.awt.CardLayout;
import javax.swing.*;


public class ns2GUI extends javax.swing.JFrame {

    private CardLayout laycard;

    public int noTCPFlows,noUDPFlows,totalFlows;


    public ns2GUI() {
        initComponents();
        laycard = new CardLayout();
        mainPanel.setLayout(laycard);
        mainPanel.add("scenarioParameters",scenarioParameters);
        mainPanel.add("nodeVisualization",nodeVisualization);
        mainPanel.add("flowDetails",flowDetails);
        mainPanel.add("trafficInfo",trafficInformation);
        laycard.show(mainPanel,"scenarioParameters");
    }

    public void goScenarioParams() {
        laycard.show(mainPanel,"scenarioParameters");
    }

    public void goNodeVisualization() {
        laycard.show(mainPanel,"nodeVisualization");
    }

    public void goFlowDetails() {
        laycard.show(mainPanel,"flowDetails");
    }

    public void goTrafficInfo() {
        laycard.show(mainPanel,"trafficInfo");
    }



    private void scenarioParamsClearActionPerformed(java.awt.event.ActionEvent evt) {                                                    
        // TODO add your handling code here:
        simTime.setText(" ");
        noNodes.setText(" ");
        tSizeX.setText(" ");
        tSizeY.setText(" ");
        initEnergy.setText(" ");
        transmitPower.setText(" ");
        recPower.setText(" ");
    }                                                   

    private void scenarioParamsOkActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        // TODO add your handling code here:
        goFlowDetails();
    }                                                

    private void enterDetailsActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
        srcSinkPanel.removeAll();
        srcSinkPanel.setLayout(new GridLayout(0,1));
        noTCPFlows = Integer.parseInt(noTcpFlows.getText());
        noUDPFlows = Integer.parseInt(noUdpFlows.getText());
        totalFlows = noTCPFlows + noUDPFlows;
        String tcpLabel = "TCP Flow ";
        String udpLabel = "UDP Flow ";
        String source = "source";
        String sink = "sink";
        String panel = "panel";

        int gridSize = 3;
        int i = 0;

            //loop for TCP flows
            for(int k=0;k<noTCPFlows; k++) {
                JLabel jL = new JLabel(tcpLabel + k);
                JTextField jT1 = new JTextField(source + k,10);
                JTextField jT2 = new JTextField(sink + k,10);
                jT1.setText(" ");
                jT2.setText(" ");
                String panelName = panel + i++;
                JPanel jP = new JPanel();
                jP.add(jL);
                jP.add(jT1);
                jP.add(jT2);
                jP.setLayout( new GridLayout( 1, gridSize ) );
                srcSinkPanel.add(jP);
            }

            //loop for UDP flows
            for(int j=0;j<noUDPFlows; j++) {
                JLabel jL = new JLabel(udpLabel + j);
                JTextField jT1 = new JTextField(source + j,10);
                JTextField jT2 = new JTextField(sink + j,10);
                jT1.setText(" ");
                jT2.setText(" ");
                String panelName = panel + i++;
                JPanel jP = new JPanel();
                jP.add(jL);
                jP.add(jT1);
                jP.add(jT2);
                jP.setLayout( new GridLayout( 1, gridSize ) );
                srcSinkPanel.add(jP);
            }

            //add the Source Sink panel
            srcSinkPanel.revalidate();


    }                                            

    private void flowDetailsBackActionPerformed(java.awt.event.ActionEvent evt) {                                                
        // TODO add your handling code here:
        goScenarioParams();
    }                                               

    private void flowDetailsContinueActionPerformed(java.awt.event.ActionEvent evt) {                                                    
        // TODO add your handling code here:
        goTrafficInfo();
        trafficInfoInit();
    }                                                   

    public void trafficInfoInit() {
        String tabName = "tab";
        String tcpLabel = "TCP Flow";
        String udpLabel = "UDP Flow ";
        String[] flowType = {"CBR","FTP","Exponential"};
        int countTcp = 1;
        int countUdp = 1;

        //Main Panel contents
        JPanel[] jP = new JPanel[noTCPFlows];
        JLabel[] jL =new JLabel[noTCPFlows];
        JComboBox[] jCb = new JComboBox[noTCPFlows];

        //option panels
        JPanel[] cbrParams = new JPanel[noTCPFlows];
        JPanel[] expoParams = new JPanel[noTCPFlows];
        JPanel[] ftpParams = new JPanel[noTCPFlows];

        //CBR Contents
        JLabel[] jL1Cbr = new JLabel[noTCPFlows];
        JLabel[] jL2Cbr = new JLabel[noTCPFlows];
        JLabel[] jL3Cbr = new JLabel[noTCPFlows];
        JLabel[] jL4Cbr = new JLabel[noTCPFlows];
        JTextField[] cbrPacketSize = new JTextField[noTCPFlows];
        JTextField[] cbrRate = new JTextField[noTCPFlows];
        JTextField[] cbrInterPacketInt = new JTextField[noTCPFlows];
        JTextField[] cbrMaxPackets = new JTextField[noTCPFlows];

        //Expo contents
        JLabel[] jL1Expo = new JLabel[noTCPFlows];
        JLabel[] jL2Expo = new JLabel[noTCPFlows];
        JLabel[] jL3Expo = new JLabel[noTCPFlows];
        JLabel[] jL4Expo = new JLabel[noTCPFlows];
        JTextField[] expoPacketSize = new JTextField[noTCPFlows];
        JTextField[] expoRate = new JTextField[noTCPFlows];
        JTextField[] expoBurstTime = new JTextField[noTCPFlows];
        JTextField[] expoIdealTime = new JTextField[noTCPFlows];

        //FTP Contents
        JLabel[] jL1Ftp = new JLabel[noTCPFlows];
        JLabel[] jL2Ftp = new JLabel[noTCPFlows];
        JTextField[] ftpMaxPackets = new JTextField[noTCPFlows];
        JTextField[] ftpMoreCountPackets = new JTextField[noTCPFlows];

        JPanel[] jP2 = new JPanel[noTCPFlows];
        CardLayout[] layPanelCard = new CardLayout[noTCPFlows];

        for( int i = 0; i<noTCPFlows; i++) {

            //create a new tab & set title
            jP[i] = new JPanel();
            jP[i].setLayout(new GridLayout(2,2));

            countTcp++;
            //create the flow type label and combo box
            jL[i] = new JLabel("Flow Type:");
            jCb[i] = new JComboBox(flowType);

            //create the options panels

            //CBR Panel
            cbrParams[i] = new JPanel();
            jL1Cbr[i] = new JLabel("Packet Size");
            cbrPacketSize[i] = new JTextField();
            jL2Cbr[i] = new JLabel("Rate");
            cbrRate[i] = new JTextField();
            jL3Cbr[i] = new JLabel("Inter Packet Interval");
            cbrInterPacketInt[i] = new JTextField();
            jL4Cbr[i] = new JLabel("Maximum Packets");
            cbrMaxPackets[i] = new JTextField();
            cbrParams[i].add(jL1Cbr[i]);
            cbrParams[i].add(cbrPacketSize[i]);
            cbrParams[i].add(jL2Cbr[i]);
            cbrParams[i].add(cbrRate[i]);
            cbrParams[i].add(jL3Cbr[i]);
            cbrParams[i].add(cbrInterPacketInt[i]);
            cbrParams[i].add(jL4Cbr[i]);
            cbrParams[i].add(cbrMaxPackets[i]);

            //Expo Panel
            expoParams[i] = new JPanel();
            jL1Expo[i] = new JLabel("Packet Size");
            expoPacketSize[i] = new JTextField();
            jL2Expo[i] = new JLabel("Rate");
            expoRate[i] = new JTextField();
            jL3Expo[i] = new JLabel("Burst Time");
            expoBurstTime[i] = new JTextField();
            jL4Expo[i] = new JLabel("Ideal Time");
            expoIdealTime[i] = new JTextField();
            expoParams[i].add(jL1Expo[i]);
            expoParams[i].add(cbrPacketSize[i]);
            expoParams[i].add(jL2Expo[i]);
            expoParams[i].add(cbrRate[i]);
            expoParams[i].add(jL3Expo[i]);
            expoParams[i].add(cbrInterPacketInt[i]);
            expoParams[i].add(jL4Expo[i]);
            expoParams[i].add(cbrMaxPackets[i]);

            //FTP Panel
            ftpParams[i] = new JPanel();
            jL1Ftp[i] = new JLabel("Max Number Packets");
            ftpMaxPackets[i] = new JTextField();
            jL2Ftp[i] = new JLabel("More Count Packets");
            ftpMoreCountPackets[i] = new JTextField();


            //create a CardLayout for the options presented
            layPanelCard[i] = new  CardLayout();
            jP2[i].setLayout(layPanelCard[i]);
            jP2[i].add("cbrParams",cbrParams[i]);
            jP2[i].add("expoParams",expoParams[i]);
            jP2[i].add("ftpParams",ftpParams[i]);
            layPanelCard[i].show(jP2[i],"cbrParams");

            //add it all to the tab
            jP[i].add(jL[i]);
            jP[i].add(jCb[i]);
            jP[i].add(jP2[i]);

            //add the tab to the tabbedPane

            trafficInfo.addTab(tabName+i,jP[i]);
           //trafficInfo.setTitleAt(countTcp,tcpLabel);

        }
        trafficInfo.updateUI();
    }

    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(ns2GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ns2GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ns2GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ns2GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

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

    // Variables declaration - do not modify                     

}
larrydalmeida

Fixed! I had forgotten to initialize the JPanel after the JPanel array declaration.

//did this
JPanel[] jP2 = new JPanel[noTCPFlows];

//rest of the code

//forgot to add this line
JPanel jP2[i] = new JPanel();

Initializing the JPanel fixed all the issues. Thank you for your suggestions!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Trying to dynamically generate tabs on a JTabbedPane with content but getting an Exception in thread "AWT-EventQueue-0"

From Dev

Exception in thread "AWT-EventQueue-0" NullPointerException error in Swing

From Dev

Exception in thread "AWT-EventQueue-0" NullPointerException error in Swing

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException

From Dev

Shws exception in thread "AWT-EventQueue-0" when button is clicked

From Dev

Java error in my code Exception in thread "AWT-EventQueue-0"

From Dev

What is this: Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError?

From Dev

Matlab reports "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException"

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException [NetBeans]

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException if-statement

From Dev

Exception in thread "AWT-EventQueue-0" javax.speech.EngineStateError: Invalid EngineState

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: javax.swing.JButton

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "11101110110100011110111011010001"

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException in basic Java GUI interest calculator

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException Login form error

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException For Loops

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Comparison method violates its general contract

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException (Panel displays before fully loaded?)

From Dev

NetBeans Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: DSA

From Dev

Exception in thread "AWT-EventQueue-0" org.hibernate.QueryException: could not resolve property: Id of:

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 8

From Dev

java parseint - Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 100

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException and JTable problems

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException in GUI?

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "FALSE"

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException and how to fix it?

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: org.apache.xmlbeans.XmlOptions.put(Ljava/lang/Object;)V

From Dev

Exception in thread "AWT-EventQueue-0" java.lang.IllegalMonitorStateException at java.lang.Object.notify(Native Method)

Related Related

  1. 1

    Trying to dynamically generate tabs on a JTabbedPane with content but getting an Exception in thread "AWT-EventQueue-0"

  2. 2

    Exception in thread "AWT-EventQueue-0" NullPointerException error in Swing

  3. 3

    Exception in thread "AWT-EventQueue-0" NullPointerException error in Swing

  4. 4

    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException

  5. 5

    Shws exception in thread "AWT-EventQueue-0" when button is clicked

  6. 6

    Java error in my code Exception in thread "AWT-EventQueue-0"

  7. 7

    What is this: Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError?

  8. 8

    Matlab reports "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException"

  9. 9

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException [NetBeans]

  10. 10

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException if-statement

  11. 11

    Exception in thread "AWT-EventQueue-0" javax.speech.EngineStateError: Invalid EngineState

  12. 12

    Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: javax.swing.JButton

  13. 13

    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "11101110110100011110111011010001"

  14. 14

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException in basic Java GUI interest calculator

  15. 15

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException Login form error

  16. 16

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException For Loops

  17. 17

    Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Comparison method violates its general contract

  18. 18

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException (Panel displays before fully loaded?)

  19. 19

    NetBeans Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: DSA

  20. 20

    Exception in thread "AWT-EventQueue-0" org.hibernate.QueryException: could not resolve property: Id of:

  21. 21

    Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 8

  22. 22

    java parseint - Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""

  23. 23

    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 100

  24. 24

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException and JTable problems

  25. 25

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException in GUI?

  26. 26

    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "FALSE"

  27. 27

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException and how to fix it?

  28. 28

    Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: org.apache.xmlbeans.XmlOptions.put(Ljava/lang/Object;)V

  29. 29

    Exception in thread "AWT-EventQueue-0" java.lang.IllegalMonitorStateException at java.lang.Object.notify(Native Method)

HotTag

Archive