Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Problem with tables !

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with tables !

    i have this code

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
     
    public class komvoi
    {
        private JFrame frame;
        private Container contentPane;
        private JButton bt1, bt2;
        private JTextField tf1,tf2;
        private JLabel label,label2,label3,label4,label5,label6;
        private JMenuBar menuBar;
        private JMenu fileMenu;
        private JMenuItem saveas; 
        private JMenuItem Dijkstra; 
        private JMenuItem newaddItem;    
        private JMenuItem quitItem;
        private JPanel  centerPanel,innorthPanel,northpanel,inCenterPanel, southPanel,centerPanel2,inCenterPanel2,innorthPanel2,northpanel2;
         private  JTable table1,table2;
         private int pinA[][];
         private int pinC[][];
         private int x;// arithmos komvwn
         private int s;//arxikos komvos
         private int pind[];//pinakas me tis arxikes apostaseis     
         private int pinap[];//pinakas arxikopoihseis twn timwn patera
         private int pinSS[];//SS(j)=0,2,1 ������� �� �� �� � ������ j 
         private int pinT[];
         //  %������ ��� ������, ��� �������� ��� ������� � ���
     
     
     
        public komvoi()
        {
            makeFrame2();
            pind= new int[x];
            pinap= new int[x];
            pinSS= new int[x];
     
     
     
        }
        public void makeFrame2()
        {
            Font f;
     
            frame = new JFrame("solver ");
     
            contentPane = frame.getContentPane();
            contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10)); 
            contentPane.setBackground(Color.blue);
     
            menuBar = new JMenuBar();
           frame.setJMenuBar(menuBar);
           fileMenu = new JMenu("File");
           menuBar.add(fileMenu);
     
              newaddItem = new JMenuItem("new");
              fileMenu.add(newaddItem);
     
              saveas = new JMenuItem("Save as");
              fileMenu.add(saveas);
     
              quitItem = new JMenuItem("quit�");
              fileMenu.add(quitItem);
     
              fileMenu = new JMenu("Shortest path");
              menuBar.add(fileMenu);
              Dijkstra = new JMenuItem("Dijkstra Algorithm");
              fileMenu.add(Dijkstra);
     
           Font f1;
       northpanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10)); 
                            innorthPanel = new JPanel(new GridLayout(2,2,5,5));
     
                            label2 = new JLabel("give the number of nodes "); 
                            f1 = new Font("TimesRoman", Font.BOLD, 16);
                            label2.setFont(f1);
                            innorthPanel.setBackground(Color.blue);
                            northpanel.setBackground(Color.blue);
                            label2.setForeground(Color.red);
     
                          innorthPanel.add(label2);
     
                            tf1 = new JTextField();
                            tf1.setSize(5,5);
     
                            innorthPanel.add(tf1);
     
                           label6= new JLabel("give the initial node");
                              f1 = new Font("TimesRoman", Font.BOLD, 16);
                            label6.setFont(f1);
                            label6.setForeground(Color.red);
                           innorthPanel.add(label6);
     
     
                                  tf2 = new JTextField();
                            tf2.setSize(5,5);
                           innorthPanel.add(tf2);
     
                            northpanel.add(innorthPanel);
     
                          contentPane.add(northpanel, BorderLayout.NORTH);
     
            centerPanel = new  JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10)); 
                inCenterPanel= new JPanel(new GridLayout(0,1,5,5));
                bt1 = new JButton("ok");
                inCenterPanel .add(bt1);
                centerPanel.setBackground(Color.blue);
                 inCenterPanel.setBackground(Color.blue);
                bt1.setSize(5,5);
                centerPanel.add(inCenterPanel);
           contentPane.add(centerPanel, BorderLayout.CENTER);
     
     
           southPanel = new  JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10)); 
                 label3 = new JLabel("");
                 southPanel.add(label3);
                 southPanel.setBackground(Color.blue);
     
            contentPane.add(southPanel, BorderLayout.WEST);
     
       bt1.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e)
                    {
     
                         contentPane.setLayout(new GridLayout(0,1));
     
                             String q =   tf1.getText();
     
                           x = Integer.parseInt(q);
     
                           String a =   tf2.getText();
     
                            s= Integer.parseInt(a);
     
     
     
                        if (x<0){
                        label3.setText("give a number >0");
                        }
                        else if (x==0){
                            label3.setText("give a number >0");
                        }
                        else if (x>0) {
     
                             label2.setVisible(false);
                                   innorthPanel.remove(label2);
                                 innorthPanel.validate();
                        label3.setVisible(false);
                                 southPanel.remove(label3);
                                 southPanel.validate();
                        tf1.setVisible(false);
                                  innorthPanel.remove( tf1);
                                   innorthPanel.validate();
                        bt1.setVisible(false);
                                inCenterPanel.remove(bt1);
                                inCenterPanel.validate();
                        contentPane.setBackground(Color.blue);
     
                        contentPane.remove(southPanel);
                         contentPane.remove(northpanel);
                         contentPane.remove(centerPanel);
     
                            centerPanel = new  JPanel(new FlowLayout(FlowLayout.CENTER)); 
                                     inCenterPanel= new JPanel(new GridLayout(1,2));
                                    label4 = new JLabel("A= "); 
                                    inCenterPanel.add(label4);
                                    table1= new JTable(x,x);
                                    table1.setBackground(Color.red);
                                    inCenterPanel.add( table1);
                                    centerPanel.setBackground(Color.blue);
                                    inCenterPanel.setBackground(Color.blue);
                                    centerPanel.add(inCenterPanel);
                             contentPane.add(centerPanel, BorderLayout. NORTH);
     
                                  northpanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); 
                                         innorthPanel = new JPanel(new GridLayout(1,2));
                                         label5 = new JLabel("C= "); 
                                         innorthPanel.add(label5);
                                         table2= new JTable(x,x);
                                         table2.setBackground(Color.red);
                                         innorthPanel.add( table2);
                                         northpanel.add(innorthPanel);
                                         innorthPanel.setBackground(Color.blue);
                                         northpanel.setBackground(Color.blue);
                                  contentPane.add(northpanel, BorderLayout.CENTER);
     
     
     
     
     
                                   southPanel = new  JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10)); 
                                            bt1 = new JButton("ok");
                                            southPanel .add(bt1);
                                            southPanel.setBackground(Color.blue);
                                            contentPane.add(southPanel, BorderLayout.SOUTH);
                                        bt1.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e)
                    {
     
                            Dijkstra ();
     
                                     }
            });
     
     
                        }
     
     
                            }
            });
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
              frame.pack();
              frame.setSize(500 ,300);
              frame.setVisible(true);
        }
     
     
                    public void Dijkstra ()
                        {
     
                            pinA = new int[x][x];
                            //System.out.println("x = " + x);
                            for(int i = 0; i<x; i++)
                            {
                                for(int j=0; j<x; j++)
                                {
     
                                    pinA[i][j] = Integer.parseInt((String)table1.getValueAt(i,j));
                                    //System.out.println("table1 i= " + i + " j= " + j + "---- " + pinA[i][j]);
                                }
                            }
     
                            boolean flag = true;
                              for (int i=0; i<x; i++)
                              {
                                for(int j=0; j<x; j++)
                            {
                              if  (  i==j && pinA[i][j]!=0){
                                    System.out.println("ta diagvnia stoixeia tou pinaka A einai diafwra tou midenos ");
                                     flag = false;
                                }
     
                            }
                        }
                            for(int i = 0; i<x; i++)
                            {
                                  for(int j=0; j<x; j++)
                                {
                                    if ( (pinA[i][j]!=0) && (pinA[i][j]!=1) ){
     
     
                                    System.out.println(" o pinakas A den einai mitra geitonias ");
     
     
                                    }
     
                             }
                         }
     
     
     
                            for(int i = 0; i<x; i++)
                            {
                                for(int j=0; j<x; j++)
                                {
                                   System.out.print(pinA[i][j] + ", ");
                                }
                                System.out.println();
                            }
     
     
                            pinC = new int[x][x];
                            for(int i = 0; i<x; i++)
                            {
                                for(int j=0; j<x; j++)
                                {
                                   pinC[i][j] = Integer.parseInt((String)table2.getValueAt(i,j));
                                }
                            }
     
                               for(int i = 0; i<x; i++)
                            {
                                for(int j=0; j<x; j++)
                                {
                                   System.out.print(pinC[i][j] + ", ");
                                }
                                System.out.println();
                            }
     
     
                        for(int i=0; i<x; i++)
                        {
                        pind[i]= 1000; 
                        pind[s]=s;
                         System.out.print(pind[i] + ", ");
                       }
                        for(int i=0; i<x; i++)
                        {
                        pinap[i]= 0;  
                         pinap[s]=s;
                         System.out.print(pinap[i] + ", ");
                       }
     
                         for(int i=0; i<x; i++)
                        {
                        pinSS[i]= 1;  
                        pinSS[s]=0;
                         System.out.print(pinSS[i] + ", ");
                       }
     
     
     
     
                       pinT[s]=s;
     
     
                        }
     
     
     
    }

    And it have this error :



    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0
    at komvoi.Dijkstra(komvoi.java:309)
    at komvoi$1$1.actionPerformed(komvoi.java:202)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2012)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2335)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:404)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:253)
    at java.awt.Component.processMouseEvent(Component.jav a:6203)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:596 8)
    at java.awt.Container.processEvent(Container.java:210 5)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4564)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2163)
    at java.awt.Component.dispatchEvent(Component.java:43 90)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4461)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4125)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4055)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2149)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:43 90)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:649)
    at java.awt.EventQueue.access$000(EventQueue.java:96)
    at java.awt.EventQueue$1.run(EventQueue.java:608)
    at java.awt.EventQueue$1.run(EventQueue.java:606)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:105)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:116)
    at java.awt.EventQueue$2.run(EventQueue.java:622)
    at java.awt.EventQueue$2.run(EventQueue.java:620)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:105)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 619)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:275)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:200)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:190)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:185)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:177)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:138)


    Could someone help me?
    Last edited by copeg; July 22nd, 2011 at 09:16 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problem with tables !

    For future reference, please use the code tags.

    What do you think that exception means? Did you go to the line where the exception is being thrown and look at what might be causing it? Add some println statements prior to the line the exception is being thrown on to evaluate what could be causing it

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Problem with tables !

    I suggest Googling the exception although it is pretty self explanatory.

    Array Index Out Of Bounds....
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. scrolling in jsp tables
    By ravindraneeluri in forum Member Introductions
    Replies: 1
    Last Post: May 25th, 2011, 09:17 AM
  2. How to update 2 tables in database using JDBC
    By nrao in forum JDBC & Databases
    Replies: 0
    Last Post: November 11th, 2010, 07:05 PM
  3. Creating Excel Pivot Tables using Java
    By prasad.virgo in forum Threads
    Replies: 4
    Last Post: August 4th, 2010, 12:20 PM
  4. Data tables and such like.
    By ShaunB in forum Java Theory & Questions
    Replies: 1
    Last Post: December 30th, 2009, 06:10 PM
  5. Constructors, Hash Tables, & Linked Lists
    By illusion887 in forum Collections and Generics
    Replies: 2
    Last Post: December 3rd, 2009, 03:46 AM