Page 2 of 2 First 12
  • Jump to page:
    #16
  1. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Can you explain what you are trying to do and post some code to help us understand what your problem is?
  2. #17
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0
    Originally Posted by NormR
    Can you explain what you are trying to do and post some code to help us understand what your problem is?
    i want to make a method that adds a JButton to a Tabbed Pane so i don't have to do this:
    Code:
    panel1.add(Tabs.r1);
              panel1.add(Tabs.r2);
              
              panel2.add(Tabs.r3);
              panel2.add(Tabs.r4);
              panel2.add(Tabs.r5);
              panel2.add(t3);
              panel2.add(bedrock);
              panel2.add(stone);
              panel2.add(dirt);
              panel2.add(grass);
              panel2.add(log);
              panel2.add(planks);
              panel2.add(tnt);
              panel2.add(stonebrick);
              panel2.add(netherbrick);
              
              panel3.add(t2);
              
              panel4.add(bb1);
              panel4.add(bb2);
              panel4.add(bb3);
              panel4.add(b4);
              panel4.add(b5);
              panel4.add(b6);
              panel4.add(b7);
              panel4.add(b8);
              panel4.add(b9);
              
              panel4.add(bedrock);
              panel4.add(stone);
              panel4.add(dirt);
              panel4.add(grass);
              panel4.add(log);
              panel4.add(planks);
              panel4.add(tnt);
              panel4.add(chest);
              panel4.add(stonebrick);
              panel4.add(netherbrick);
              panel4.add(wool);
              panel4.add(gravel);
              JComponent cobble = addNewButton("test","test");
              panel4.add(cobble);
              
              panel5.add(gens);
              window.add(p1);
              window.add(l1);
              window.add(type);
              p1.add(next);
              p1.add(back);
              window.add(button);
              p1.add(menu);
              window.add(b1);
              window.add(b2);
              window.add(b3);
              p1.add(l2);
              window.add(Crafting.craft1);
              window.add(Crafting.generator);
              window.add(Crafting.methods);
              window.add(Item.items1);
              window.add(Block.blocks1);
              window.add(Crafting.tp1);
              window.add(load);
              window.add(Crafting.t1);
              bg.add(Tabs.r1);
              bg.add(Tabs.r2);
              bg2.add(Tabs.r3);
              bg2.add(Tabs.r4);
              bg2.add(Tabs.r5);
              addtowin();
              firstpage();
              t3.setVisible(false);
    then i want to be able to set if the buttons should be visible or not without writing out every button because that is going to be a HUGE pain
    Code:
    ButtonTest.bedrock.setVisible(true);
            ButtonTest.stone.setVisible(true);
            ButtonTest.dirt.setVisible(true);
            ButtonTest.grass.setVisible(true);
            ButtonTest.log.setVisible(true);
            ButtonTest.planks.setVisible(true);
            ButtonTest.type.setVisible(false);
            ButtonTest.l2.setVisible(false);
            ButtonTest.bb1.setVisible(false);
            ButtonTest.bb2.setVisible(false);
            ButtonTest.bb3.setVisible(false);
            ButtonTest.b4.setVisible(false);
            ButtonTest.b5.setVisible(false);
            ButtonTest.b6.setVisible(false);
            ButtonTest.b7.setVisible(false);
            ButtonTest.b8.setVisible(false);
            ButtonTest.b9.setVisible(false);
            ButtonTest.tnt.setVisible(true);
            ButtonTest.chest.setVisible(true);
            ButtonTest.stonebrick.setVisible(true);
            ButtonTest.netherbrick.setVisible(true);
            ButtonTest.wool.setVisible(true);
            ButtonTest.gravel.setVisible(true);
            ButtonTest.b.setVisible(true);
            isTrue = true;
    after that i want to be able to set the action without typing out this(i want it to be one line):
    Code:
    public void actionPerformed(ActionEvent p)
            {
                if("b1".equals(p.getActionCommand()))
                {
                    List.True();
                    b1 = true;
                }
                if("b2".equals(p.getActionCommand()))
                {
                    List.True();
                    b2 = true;
                }
                if("b3".equals(p.getActionCommand()))
                {
                    List.True();
                    b3 = true;
                }
                if("b4".equals(p.getActionCommand()))
                {
                    List.True();
                    b4 = true;
                }
                if("b5".equals(p.getActionCommand()))
                {
                    List.True();
                    b5 = true;
                }
                if("b6".equals(p.getActionCommand()))
                {
                    List.True();
                    b6 = true;
                }
                if("b7".equals(p.getActionCommand()))
                {
                    List.True();
                    b7 = true;
                }
                if("b8".equals(p.getActionCommand()))
                {
                    List.True();
                    b8 = true;
                }
                if("b9".equals(p.getActionCommand()))
                {
                    List.True();
                    b9 = true;
                }
                if("bedrock".equals(p.getActionCommand()))
                {
                    new Recipe("Block.bedrock");
                    done();
                    List.backto();
                }
                if("stone".equals(p.getActionCommand()))
                {
                    new Recipe("Block.stone");
                    done();
                    List.backto();
                }
                if("dirt".equals(p.getActionCommand()))
                {
                    new Recipe("Block.dirt");
                    done();
                    List.backto();
                }
                if("grass".equals(p.getActionCommand()))
                {
                    new Recipe("Block.grass");
                    done();
                    List.backto();
                }
                if("log".equals(p.getActionCommand()))
                {
                    new Recipe("Block.log");
                    done();
                    List.backto();
                }
                if("planks".equals(p.getActionCommand()))
                {
                    new Recipe("Block.planks");
                    done();
                    List.backto();
                }
                if("tnt".equals(p.getActionCommand()))
                {
                    new Recipe("Block.TNT");
                    done();
                    List.backto();
                }
                if("stonebrick".equals(p.getActionCommand()))
                {
                    new Recipe("Block.stoneBrick");
                    done();
                    List.backto();
                }
                if("netherbrick".equals(p.getActionCommand()))
                {
                    new Recipe("Block.netherBrick");
                    done();
                    List.backto();
                }
                if("chest".equals(p.getActionCommand()))
                {
                    new Recipe("Block.chest");
                    done();
                    List.backto();
                }
                if("brick".equals(p.getActionCommand()))
                {
                    new Recipe("Block.brick");
                    done();
                    List.backto();
                }
                if("wool".equals(p.getActionCommand()))
                {
                    new Recipe("Block.cloth");
                    done();
                    List.backto();
                }
                if("gravel".equals(p.getActionCommand()))
                {
                    new Recipe("Block.gravel");
                    done();
                    List.backto();
                }
  4. #18
  5. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    One way to reduce the number of lines is to put the component references into an array:
    stone, dirt, grass, etc And then use a loop as shown above to get the references and call the method.
    i want it to be one line
    It will take several statements: loop and if test.
    Put the Strings: "b1", "b2", etc in an array and use a loop through that array to compare its elements with the String returned by the getActionCommand() method.

    Because there are different statements in the if, there will have to be different loops for each set of statements.
    One for when setting true and one for setting false.
    Last edited by NormR; December 20th, 2012 at 05:12 PM.
  6. #19
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0
    Originally Posted by NormR
    One way to reduce the number of lines is to put the component references into an array:
    stone, dirt, grass, etc And then use a loop as shown above to get the references and call the method.
    It will take several statements: loop and if test.
    Put the Strings: "b1", "b2", etc in an array and use a loop through that array to compare its elements with the String returned by the getActionCommand() method.

    Because there are different statements in the if, there will have to be different loops for each set of statements.
    One for when setting true and one for setting false.
    the b1 and b2 and stuff is not what i needed. all the buttons that i need return with the same lines
  8. #20
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    If you want to call the same method on all the buttons,
    Try putting references to the buttons into an array and write a loop to index through the elements in the array and call the method for each element in the array.
  10. #21
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0
    Originally Posted by NormR
    If you want to call the same method on all the buttons,
    Try putting references to the buttons into an array and write a loop to index through the elements in the array and call the method for each element in the array.
    i have no idea what you just said xD
  12. #22
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    I suggested doing 3 things. Which one are you having problems with?
    1) define an array
    2) write a for loop
    3) use array's elements to call method


    I gave sample code in post #10
  14. #23
  15. Daniel Schildsky
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2004
    Location
    KL, Malaysia.
    Posts
    1,540
    Rep Power
    1621
    Just declare a static array of JButton type, then add all button instances you created into this array right after the initialisation block (the block which instantiates all the JButton instances in your program with the keyword new ), and loop through these JButton instances one-by-one using a loop, in which you invoke the method(s) you want (them all) to be invoked whenever it is necessary to invoke the same method(s) on all JButton instances anywhere in the codes. It is just that simple.
    Last edited by tvc3mye; December 22nd, 2012 at 02:26 AM. Reason: Changed JComponent to JButton
    When the programming world turns decent, the real world will turn upside down.
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo