Page 2 of 2 FirstFirst 12
Results 21 to 33 of 33
  1. #21
    macwadu is offline Member
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default

    my is this

    Java Code:
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    
    package Project;
    
    import java.awt.Dimension;
    import java.awt.Point;
    import java.awt.Toolkit;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.util.Iterator;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    
    /**
    *
    * @author x01168
    */
    
    public class f extends JFrame {
      static Toolkit toolkit, toolkit1;
      static JPanel panel;
      static JButton A1, Button_array[];
      private static final int BUTTON_DISTANCE = 20;
      static Integer aux=0, aux1=0;
      static Integer WINDOWS_WITH=1200,WINDOWS_HEIGHT=750,BUTTON_WITH=100,BUTTON_HEIGHT=20, PosX, PosY;
      //static Integer Button_Clicked;
      static JButton[] client_array, client_array1,client_array_l2;
      static JButton[][] cli = new JButton[200][200];
    //  static JButton[][] cli;
     static Integer[][] client_array_l3;
    
      static int buttonPressed;
      static String name_buttonclicked;
      static Point pos_buttonclicked;
      static MouseEvent e;
    
    
      public void fs(){
    
          PosX = 50;
          PosY = 60;
         // cli = new JButton[200][200];
    
    
          setTitle("Teste xpto");
          setSize(WINDOWS_WITH, WINDOWS_HEIGHT);
    
          toolkit = getToolkit();
          Dimension size = toolkit.getScreenSize();
          setLocation((size.width - getWidth())/2, (size.height - getHeight())/2);
          setDefaultCloseOperation(EXIT_ON_CLOSE);
    
          panel = new JPanel();
          getContentPane().add(panel);
    
          panel.setLayout(null);
    
          A1 = new JButton(Conn.b);
          A1.setBounds(WINDOWS_WITH/2-50, PosY, BUTTON_WITH, BUTTON_HEIGHT);
    
          A1.addMouseListener(new MouseListener() {
    
    
          Integer i = 0;
              public void mouseClicked(MouseEvent e) {
    
                  System.out.println("----------------------------------");
                  System.out.println("Nivel 2 - "+Conn.t4);
    
                  //diz qual o botão clicked
                  //for(buttonPressed=0; client_array[buttonPressed] != e.getSource(); buttonPressed++){}
                  //System.out.println("button number "+buttonPressed+" Name: "+client_array[buttonPressed].getText() +" was pressed");
                  name_buttonclicked=A1.getText();      //client_array[buttonPressed].getText();
                  pos_buttonclicked=A1.getLocation();  //          client_array[buttonPressed].getLocation();
    
                  Conn c = new Conn();
    
                  System.out.println("-->name_buttonclicked: "+name_buttonclicked);
    
                  c.Query_Button_Selected(); // Show data of the button selected
                  System.out.println(name_buttonclicked+" ID: "+ Conn.t1+" PID: "+ Conn.t2+ " Name: "+ Conn.t3+" Level: "+ Conn.t4+ " Value: "+ Conn.t5);
    
                  c.Query_Count_PID_Next_Button_Selected();
                  System.out.println("num_pid: "+ Conn.num_pid);
    
                  c.Query_PID_Next_Button_Selected();
                  cli = new JButton[Conn.t4][Conn.num_pid];
                  //System.out.println("xzcasd1 "+Conn.string_array_PIDName[0]);
    
                  cria();
    
    
              }
    
              public void mousePressed(MouseEvent e) {
                  System.out.println("mousePressed");
              }
    
              public void mouseReleased(MouseEvent e) {
                  System.out.println("mouseReleased");
              }
    
              public void mouseEntered(MouseEvent e) {
                  System.out.println("mouseEntered");
                 // if(client_array[1] == "")
              }
    
              public void mouseExited(MouseEvent e) {
                  System.out.println("mouseExited");
              }
          });
    
    
          panel.add(A1);
          panel.repaint();
    
    
      }
    
    
      public void cria(){
    
       Conn c = new Conn();
    
       c.Query_Visibility_Next_Level();
       System.out.println("Level: " +Conn.t4);
    
    //   for(Integer i=0;i<=Conn.num_visible;i++){
    
         if(Conn.num_visible.equals(0)){  //verificar se exsitem button nivel x abertos
    
               CreateButtons(cli);
    
     }else{
             System.out.println("NomeBotão = BotãoClicked:  "+Conn.t3 +"="+name_buttonclicked );
            // System.out.println("11client_array_l2[aux1-1]: "+client_array_l2[aux1-1]);
             if(Conn.t3.equals(name_buttonclicked)){    // mesmo botão
    
                 Remove_Same_Buttons_();
    
             }else{
    
                 System.out.println("OUTRO");
             }
    
         //update database visibility to 0
         //panel.remove(client_array_l2[aux1]);
         //aux1++;
         panel.repaint();
     //}
     }
    
       }
    
    public void CreateButtons( JButton[][] cli){
       Integer aux2 = 0,y=0;
       Integer total1 = (Conn.num_pid*BUTTON_WITH + (Conn.num_pid-1)*BUTTON_DISTANCE)/2;
       Integer qwerty1 = 120;
       //cli = new JButton[200][200];
    
       Conn c = new Conn();
    
       System.out.println("Estao todos fechados");
       System.out.println("xzcasd2 "+Conn.string_array_PIDName[aux1]);
    
     // client_array_l3 = new Integer[10][10];
     // client_array_l3[Conn.t4][y] = Conn.num_pid;
     // System.out.println("client_array_l3 = new Integer[Conn.t4][aux2]:   "+client_array_l3[Conn.t4][y]);
    
     
    
       while(Conn.num_pid != aux2 ){
        System.out.println("xzcasd2 "+Conn.string_array_PIDName[aux2]);
        cli[Conn.t4][aux2] = new JButton(Conn.string_array_PIDName[aux2]);
        //client_array_l2[aux2].setName("b1"+aux2);
    
    System.out.println("Conn.num_pid: " +Conn.num_pid);
    System.out.println("aux2: " +aux2);
    System.out.println("Conn.string_array_PIDName[aux2]: "+Conn.string_array_PIDName[aux2]);
    
       //System.out.println("xzcasd3 "+client_array_l2[aux1]);
       if(aux2 == 0){   //(Posição Botão Pai) - (total do espaço ocupado pelos num de botoes e espaços entre eles)
           cli[Conn.t4][aux2].setBounds(pos_buttonclicked.x-total1, pos_buttonclicked.y+50, BUTTON_WITH, BUTTON_HEIGHT);
       }else{
           cli[Conn.t4][aux2].setBounds(pos_buttonclicked.x-total1+qwerty1,pos_buttonclicked.y+50, BUTTON_WITH, BUTTON_HEIGHT);
    
           qwerty1 +=120;
       }
    
       // System.out.println(qwerty);
       //proximo botão fica BUTTON_WITH + BUTTON_DISTANCE
       cli[Conn.t4][aux2].addMouseListener(new MouseListener() {
    
               public void mouseClicked(MouseEvent e) {
                   Conn c = new Conn();
                   System.out.println("----------------------------------");
                   System.out.println("Level: " +c.t4);
                   System.out.println("mouse Clicked");
    
    
    
                   //diz qual o botão clicked
                   for(buttonPressed=0; cli[Conn.t4][buttonPressed] != e.getSource(); buttonPressed++){}
                   System.out.println("button number "+buttonPressed+" Name: "+cli[Conn.t4][buttonPressed].getText() +" was pressed");
                   name_buttonclicked=cli[Conn.t4][buttonPressed].getText();
                   pos_buttonclicked=cli[Conn.t4][buttonPressed].getLocation();
    
                   System.out.println("-->name_buttonclicked: "+name_buttonclicked);
    
                   c.Query_Button_Selected();
                   System.out.println(name_buttonclicked+" ID: "+ Conn.t1+" PID: "+ Conn.t2+ " Name: "+ Conn.t3+" Level: "+ Conn.t4+ " Value: "+ Conn.t5);
    
                   c.Query_Count_PID_Next_Button_Selected();
                   System.out.println("num_pid: "+ Conn.num_pid);
    
                   c.Query_PID_Next_Button_Selected();
                   cli = new JButton[Conn.t4][Conn.num_pid];
                   //System.out.println("xzcasd1 "+Conn.string_array_PIDName[0]);
    
                   cria();
    
               }
    
               public void mousePressed(MouseEvent e) {
                System.out.println("mouse Pressed");
               }
    
               public void mouseReleased(MouseEvent e) {
                 System.out.println("mouse Released");
               }
    
               public void mouseEntered(MouseEvent e) {
                 System.out.println("mouse Entered");
               }
    
               public void mouseExited(MouseEvent e) {
                 System.out.println("mouse Exited");
               }
    
               });
    
    
       c.Query_Update_Visibility_Next_Level();  // actualiza a BD con Visibility = 1
    
       panel.add(cli[Conn.t4][aux2]);
    
       y++;
       aux2++;
       panel.repaint();
       }
    
    System.out.println("QQQQQQQ: "+cli[0][0]);
    System.out.println("QQQQQQQ: "+cli[0][1]);
    System.out.println("QQQQQQQ: "+cli[1][0]);
    System.out.println("QQQQQQQ: "+cli[1][1]);
    }
    
    
    public void Remove_Same_Buttons_(){
         Integer a= 0, auxq;
         Conn c = new Conn();
                    System.out.println("MESMO -- Conn.num_visible: " + Conn.num_visible);
    
               
                    System.out.println("QQQQQQQ: "+cli[0][0]);
                    System.out.println("QQQQQQQ: "+cli[0][1]);
                    System.out.println("QQQQQQQ: "+cli[1][0]);
                    System.out.println("QQQQQQQ: "+cli[1][1]);
    
                  // System.out.println("MESMO -- aux1: " + aux2);
                   //System.out.println("client_array_l2[aux1]: "+client_array_l2[aux1]);
               // while(Conn.num_visible != a ){
                     //    System.out.println("MESMO -- client_array_l2[aux1]: " + client_array_l2[aux1]);
               //      System.out.println("array_backup[a]: "+array_backup[a]);
                 //  System.out.println("Elementos a remover"+client_array_l3[Conn.t4][Conn.t4]);
                 //  auxq=client_array_l3[Conn.t4][Conn.t4];
                   // panel.remove(client_array_l2[]);
                   //  System.out.println("MESMO");
              //       a++;
              //   }
                 c.Query_Update_Remove_Visibility_Next_Level();
    
                //aux1++;
                 panel.repaint();
    
    }
    
    
    
    }

  2. #22
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    What arrays and what method are you having the problems with?

  3. #23
    macwadu is offline Member
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default

    My problem now is that i change one normal array of buttons to a multidimensional array of buttons like this cli[Conn.t4][aux2] so i can save the button with a position.

    But when i pass the array as a parameter its giving me this error and i dont know how to solve it

    Picasa Web Albums - tiago.l.nobre


    i initialize the array global like this static JButton[][] cli = new JButton[200][200];

  4. #24
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    One solution would be not to use anonymous classes for the listeners. If you create your own class, you can pass a reference to the array to the class and the listener method can access it.
    Or make the variable a class member instead of local,
    or make the variable final as the error message suggests.

  5. #25
    macwadu is offline Member
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default

    Or make the variable a class member instead of local

    how can i do this to see if it works, can you help me?

  6. #26
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    Move the definition out of the method to the class level.

    Comments on your program coding:
    use a more meaningful name for the class. A single lowercase letter is bad.
    Don't use the same variable name for local variables as for class members. For example the variable: cli is local to the following:
    public void CreateButtons( final JButton[][] cli) {

    I added a final there to make the compiler happy with the usage of cli.


    Another problem, the variable cli is very hard to search for in your program because those 3 letters are found in many places.
    Last edited by Norm; 08-27-2010 at 10:51 PM.

  7. #27
    macwadu is offline Member
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default

    I´m newbie with java i dont understand what do you mean with moving the definition out of the method to the class level

    can you give me a example



    the name of the class is to change, it started with a test and i forgot to change the name

  8. #28
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    public void CreateButtons( final JButton[][] cli) {
    I think this will make the local variable final.

  9. #29
    macwadu is offline Member
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default

    I put the multidimensional array in global only and i remove it from parameter in the function. And my output was this


    Level: 0

    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    Conn.num_pid: 9
    Conn.string_array_PIDName[aux2]: TMN
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    elem_array[0][0]: javax.swing.JButton[,20,110,100x20,invalid,alignmentX=0.0,alignmentY=0 .5,border=javax.swing.plaf.BorderUIResource$Compou ndBorderUIResource@192d342,flags=296,maximumSize=, minimumSize=,preferredSize=,defaultIcon=,disabledI con=,disabledSelectedIcon=,margin=javax.swing.plaf .InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rol loverEnabled=true,rolloverIcon=,rolloverSelectedIc on=,selectedIcon=,text=TMN,defaultCapable=true]
    elem_array[0][1]: javax.swing.JButton[,140,110,100x20,invalid,alignmentX=0.0,alignmentY= 0.5,border=javax.swing.plaf.BorderUIResource$Compo undBorderUIResource@192d342,flags=296,maximumSize= ,minimumSize=,preferredSize=,defaultIcon=,disabled Icon=,disabledSelectedIcon=,margin=javax.swing.pla f.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rol loverEnabled=true,rolloverIcon=,rolloverSelectedIc on=,selectedIcon=,text=MT,defaultCapable=true]
    elem_array[1][0]: null -> here is ok because i haven't clicked the second time so it hasn't been filed with values
    elem_array[1][1]: null

    ----------------------------------
    Level: 1
    Estao todos fechados
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    Conn.num_pid: 3
    Conn.string_array_PIDName[aux2]: PPS
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    elem_array[0][0]: null -> the problem is here beause it should have the values that he as in Level 0
    elem_array[0][1]: null
    elem_array[1][0]: javax.swing.JButton[,-30,160,100x20,invalid,alignmentX=0.0,alignmentY=0. 5,border=javax.swing.plaf.BorderUIResource$Compoun dBorderUIResource@192d342,flags=296,maximumSize=,m inimumSize=,preferredSize=,defaultIcon=,disabledIc on=,disabledSelectedIcon=,margin=javax.swing.plaf. InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rol loverEnabled=true,rolloverIcon=,rolloverSelectedIc on=,selectedIcon=,text=PPS,defaultCapable=true]
    elem_array[1][1]: javax.swing.JButton[,90,160,100x20,invalid,alignmentX=0.0,alignmentY=0 .5,border=javax.swing.plaf.BorderUIResource$Compou ndBorderUIResource@192d342,flags=296,maximumSize=, minimumSize=,preferredSize=,defaultIcon=,disabledI con=,disabledSelectedIcon=,margin=javax.swing.plaf .InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rol loverEnabled=true,rolloverIcon=,rolloverSelectedIc on=,selectedIcon=,text=PPLX,defaultCapable=true]

  10. #30
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    You need to debug the code to see why the values of the arrays are not what you want.
    Try adding more println()s to display the contents when they are set and changed.

  11. #31
    macwadu is offline Member
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default

    The problem was that i have been creating the multidimensional array two times. When i clicked in a button i was creating another array with the same name so the values inserted before weren't saved.

    Tanks for helping me out, and sorry for my ignorance, and your patience.

    Now i'm going to try to remove from the array :)

  12. #32
    gafa's Avatar
    gafa is offline Senior Member
    Join Date
    Jun 2010
    Posts
    107
    Rep Power
    0

    Smile

    Quote Originally Posted by macwadu View Post
    static JButton[][] client_array_l2;

    client_array_l2 = new JButton[200][200];

    client_array_l2[Conn.t4][aux2] = new JButton(Conn.string_array_PIDName[aux2]);



    what you are saing is that i have to inicialize the JButton[][] with some int?
    If it is a method body the problem may occure because of not initilized static JButton[][] client_array_l2 array I guess...

    You can just make the array from a local array to a field array to have default elements' values :)

  13. #33
    gafa's Avatar
    gafa is offline Senior Member
    Join Date
    Jun 2010
    Posts
    107
    Rep Power
    0

    Default

    And wait... I could notice that you trying to share static array[][] among different one level arrays[]...
    You must know that static object cannot be cloned :)
    It is a common problem which one can often meet in Graphics programming ... A typical logical error

    I guess you have to init from

    static JButton[][] client_array_l2; to

    JButton[][] client_array_l2;

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Non functional radio buttons and messed up array
    By flameofSuzaku in forum New To Java
    Replies: 2
    Last Post: 01-10-2011, 05:38 PM
  2. Help with Buttons
    By wld4ubabay in forum New To Java
    Replies: 20
    Last Post: 05-17-2010, 08:13 AM
  3. Replies: 2
    Last Post: 04-30-2010, 03:06 AM
  4. Where To Get Pop Up Buttons
    By hitmen in forum AWT / Swing
    Replies: 7
    Last Post: 03-26-2009, 04:05 AM
  5. How to use SWT Buttons
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 04:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •