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 4 of 4

Thread: make object and add components

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Location
    kathmandu, nepal
    Posts
    29
    My Mood
    Cheerful
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default make object and add components

    Adding lots of JFrame components would be tidious so i wish if there is some way to create a way to add all the JFrame components at once.
    lbl_info = new JLabel("Values with an astrick(*) are necessary");
    		lbl_propertyNo = new JLabel("Property Numner: ");
    		lbl_propertyName = new JLabel("* Property Name: ");
    		lbl_purchasePrice = new JLabel("Purchase Price: ");
    		lbl_rent = new JLabel("Rental charges (per month)");
    		lbl_occRate =  new JLabel("Expected Occupancy Rate: ");
    		lbl_mgmtFees = new JLabel("Property management fees (per month): ");
    		lbl_insurance = new JLabel("Annual Cost of Insurance: ");
    		lbl_maintenanceCost = new JLabel("Annual Maintenance Cost");
     
    		txt_propertyName = new JTextField("* Property Name: ");
    		txt_purchasePrice = new JTextField("Purchase Price: ");
    		txt_rent = new JTextField("Rental charges (per month)");
    		txt_occRate =  new JTextField("Expected Occupancy Rate: ");
    		txt_mgmtFees = new JTextField("Property management fees (per month): ");
    		txt_insurance = new JTextField("Annual Cost of Insurance: ");
    		txt_maintenanceCost = new JTextField("Annual Maintenance Cost");

    here is my code


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: make object and add components

    Sometimes writing proper code can be tedious to those who don't enjoy it and find satisfaction in doing it properly.

    However, arrays of components can be added using loops when the naming of the components allows, it makes sense to do so, and the code is still easily read and understood.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Location
    kathmandu, nepal
    Posts
    29
    My Mood
    Cheerful
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: make object and add components

    Quote Originally Posted by GregBrannon View Post
    Sometimes writing proper code can be tedious to those who don't enjoy it and find satisfaction in doing it properly.

    However, arrays of components can be added using loops when the naming of the components allows, it makes sense to do so, and the code is still easily read and understood.
    Thanks for the answer but I know how to add components using arrays, that's a simple thing. But you can see the code I have posted here has different names. I want to add these very components to JFrame

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: make object and add components

    You could also put the names in an array and match them up as you go through the loops, but that's starting to sound like storing the program data in multiple parallel arrays, a practice to be avoided. Instead, create a class of components with the details stored in fields, and then store those in an array which can be added to the container using loops. I don't know that any of those approaches will reduce the typing required, but it's starting to evolve into a more elegant and OOP-aware design.

Similar Threads

  1. Add an object to a linked list and a database
    By a21j92 in forum JDBC & Databases
    Replies: 0
    Last Post: March 2nd, 2013, 10:09 AM
  2. Components I add to JApplet won't appear!
    By austin.rose94 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 6th, 2012, 11:47 AM
  3. [SOLVED] Add more components from JFrame Form
    By zecute in forum AWT / Java Swing
    Replies: 3
    Last Post: May 11th, 2011, 07:30 AM
  4. SAX Parser will not add object to arrayList
    By michaelz in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 1st, 2010, 02:59 PM
  5. How can i make the components resizable?
    By ces_31 in forum AWT / Java Swing
    Replies: 4
    Last Post: October 1st, 2009, 10:46 AM