Results 1 to 7 of 7
  1. #1
    shurgs is offline Member
    Join Date
    Aug 2010
    Location
    England
    Posts
    12
    Rep Power
    0

    Default SOLVED: Changing different Swing control and their properties dynamically?

    So hypothetically, say I have two jTextPanes: tp1 and tp2, and an integer i.

    I want a certain piece of text to be printed in either tp1 or tp2 depending on the integer I by putting "tp" + i into a string and using that to search for the swing control. I remember in PASCAL you had to use a loop that searched through every control, but I can't for the life of me figure out how to do it in Java.

    This example is basically a very simplified version of what I want to do, but any help understanding the concept behind it would be greatly appreciated.

    Thanks for readin',
    -shurgs
    Last edited by shurgs; 08-12-2010 at 01:51 PM.

  2. #2
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    I'm no expert, but my suggestion would be to use a switch statement if there aren't that many textfields, so:

    switch(i){
    case 0: print in textfield1
    case 1: print in textfield2
    etc.
    }

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You could place the JTextFields in an array and then simply use i as the array index. Other options are to use a List or a Map.

  4. #4
    shurgs is offline Member
    Join Date
    Aug 2010
    Location
    England
    Posts
    12
    Rep Power
    0

    Cool

    Thanks for the reply. I hadn't thought of that actually and it's a good suggestion, but as you say, I'm not sure it would scale up well. There's going to quite a few and I need to loop through them systematically, pulling data from a MySQL database and putting it in the text fields.

  5. #5
    shurgs is offline Member
    Join Date
    Aug 2010
    Location
    England
    Posts
    12
    Rep Power
    0

    Default

    An array would work beautifully actually. It kinda seems obvious now that you've said it :D

    Thank you all for your replies.
    -shurgs

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  7. #7
    shurgs is offline Member
    Join Date
    Aug 2010
    Location
    England
    Posts
    12
    Rep Power
    0

    Default

    jTables and Tabel Models confuse the hell out of me, especially with the Swing GUI editor, because it makes it so bloomin' awkward to edit the code. A terrible reason to use something else and bad practice, I know, but I'm hoping this will work out better in the end anyway for what I want to achieve. Hehe.

    I just thought of something. To place them into an array I would have to loop through the controls anyway, unless I did it manually, but that would just be silly. Kinda puts me back to square one. :)

    Maybe I should just bite the bullet and try tables again.
    Last edited by shurgs; 08-12-2010 at 01:56 PM. Reason: I'm an idiot.

Similar Threads

  1. Dynamically changing the title of the JFrame??
    By Y. Progammer in forum New To Java
    Replies: 2
    Last Post: 03-15-2010, 09:20 AM
  2. Dynamically changing the TextArea.forground !
    By Y. Progammer in forum New To Java
    Replies: 6
    Last Post: 03-01-2010, 10:08 AM
  3. loading .properties file dynamically
    By nanaji in forum Advanced Java
    Replies: 3
    Last Post: 03-17-2009, 03:40 AM
  4. Dynamically changing the display
    By abhiN in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 01-22-2008, 11:19 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
  •