need suggestion about "new JRadioButton"


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: need suggestion about "new JRadioButton"

Hybrid View

  1. #1
    Join Date
    Aug 2006
    Posts
    18

    need suggestion about "new JRadioButton"

    hello,
    i'm new in java, and i need suggestion about things below:

    which is better:

    to use
    Code:
    private void try {
      radiobutton1.setSelected(true);
      radiobutton2.setSelected(false);
      radiobutton3.setSelected(false);
    }
    or this one:
    Code:
    private void setSelectedRadioButtons(JRadioButton[] rb, boolean[] b) {
      for (int i=0; i<=rb.length; i++) {
        rb[i].setSelected(b[i]);
      }
    }
    
    private void try {
      setSelectedRadioButtons( 
        new JRadioButton[] { radiobutton1, radiobutton2, radiobutton3 },
        new boolean[] { true, false, false }
      }
    }
    currently, i'm using the latter in my program, but i'm afraid that it takes lots of memory or
    it's a "not suggested" line of code.

    if the latter is ok, then i will use it for other components and properties

    thank you
    Last edited by efm; 08-30-2006 at 05:29 AM.

  2. #2
    Join Date
    Dec 2005
    Location
    Arrr
    Posts
    32
    The idea of the latter is better, because it is more reusable...it would support any number of items, rather than just 3, which you would have to recode for another program that needs more than three items. However, I don't think you should name a method 'try', as that is a Java keyword used in exception handling. Not necessarily an error, but it's bad practice.

  3. #3
    Join Date
    Mar 2005
    Location
    UK, London
    Posts
    150
    Does this code compile for you, i dont see a ) to close the method.

    private void try {
    setSelectedRadioButtons(
    new JRadioButton[] { radiobutton1, radiobutton2, radiobutton3 },
    new boolean[] { true, false, false }
    }
    }

  4. #4
    Join Date
    Aug 2006
    Posts
    18
    hi all,

    major: i have a typo there, you're rite, it should be a ")", not a "}"

    pirate: thanks for the suggestion, so it's ok if i use the second lines of code? will i get memory lack or
    something? because i don't really understand of the keyword "new" is. isn't it to make a new instance?

    and about that "try" keyword hehe, i wrote it myself, not copy pasting it from my code. so it's a
    mistake.

    thank you

  5. #5
    Join Date
    Dec 2005
    Location
    Arrr
    Posts
    32
    Quote Originally Posted by efm
    pirate: thanks for the suggestion, so it's ok if i use the second lines of code? will i get memory lack or
    something? because i don't really understand of the keyword "new" is. isn't it to make a new instance?
    It should be fine, you don't have to worry so much about the memory leaks in Java, because Java has automated memory management, unlike some other OOP languages. Any values you've set up in RAM will be cleaned out by the garbage collector a few milliseconds or so after no reference variables are pointing to them. The new operator is typically used to make an instance, such as:

    Foo f = new Foo();
    myMethod(f);


    but, you can also use it to create objects on the fly without a variable, the way you are doing it with your code, such as;

    myMethod(new Foo);
    Last edited by piratepops; 08-30-2006 at 11:11 PM. Reason: reducing quote clutter

Similar Threads

  1. Suggestion Needed
    By ram_mcse in forum Security
    Replies: 3
    Last Post: 12-01-2006, 11:05 AM
  2. Need your suggestion
    By swapnil_paranja in forum C++
    Replies: 0
    Last Post: 06-23-2005, 07:29 AM
  3. Replies: 0
    Last Post: 06-13-2002, 12:41 AM
  4. e-commerce suggestion...
    By ScottD in forum Architecture and Design
    Replies: 0
    Last Post: 05-24-2002, 06:03 AM
  5. Any Suggestion to this
    By Kamlesh Patel in forum Java
    Replies: 6
    Last Post: 07-09-2001, 08:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center