Results 1 to 9 of 9
  1. #1
    ejs7597 is offline Member
    Join Date
    Feb 2009
    Posts
    13
    Rep Power
    0

    Default GUI help - Text Box Defaults to Array Value

    I have created a simple inventory program for school that stores inventory items in arrays. Now I have created a GUI for the program in a new class. How do I set the default text in the text box equal to the data in an array?
    For example I would like to set the textbox to display the valuee stored in itemName[0].

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    if you use JComboBox, it has a constructor accept array or vector JComboBox(Object[] items) , JComboBox(Vector<?> items)

  3. #3
    ejs7597 is offline Member
    Join Date
    Feb 2009
    Posts
    13
    Rep Power
    0

    Default

    I need to display the data in the arrays one item at a time so I was going to have a text box to display the data for each array. itemNum[], itemDesc[],itemQty[]. I am having a hard time setting the text in the text box equal to the values in the array. Does anyone have any suggestions on how to do this and maybe syntax examples?

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

    Default

    What is a "text box" and what GUI library are you using?

  5. #5
    ejs7597 is offline Member
    Join Date
    Feb 2009
    Posts
    13
    Rep Power
    0

    Default JTextField

    I am trying to use JTextField

    JTextField txt1 = new JTextField("Product Name"); //HOW DO i GET THIS VALUE SET TO itemNum[counter]

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

    Default

    JTextField -- thanks for clarifying this, as your question is completely unanswerable without this information.

    You can set the text of any JTextField simply by calling the setText(String text) method. Does this answer your question?

  7. #7
    ejs7597 is offline Member
    Join Date
    Feb 2009
    Posts
    13
    Rep Power
    0

    Default

    I have been trying to use the following code:

    JTextField txt1 = new JTextField(itemNum[counter]);

    itemNum[counter] is a working variable in the main class. This new code is in my GUI class. When I use this line I get an error saying that the variable itemNum is not found. Do I need to do something to get my GUI class to be able to see the variable in my main class?

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

    Default

    Do I need to do something to get my GUI class to be able to see the variable in my main class?
    Obviously, yes.
    Your problem doesn't appear to have much to do with JTextFields and everything to do with one class being able to get information from another.

    Perhaps your GUI class needs a public method, say public void displayInTextField(String text), where it allows the main class to pass a String that is then displayed by the GUI.

  9. #9
    ejs7597 is offline Member
    Join Date
    Feb 2009
    Posts
    13
    Rep Power
    0

    Default

    Ok, I will try to figure out how to get that to work. I haven't been able to grasp how to allow classes to uses variables from other classes. I do some more reading and maybe I can get a better understanding.

Similar Threads

  1. [SOLVED] Reading a text file into an Array
    By DonCash in forum New To Java
    Replies: 13
    Last Post: 01-25-2011, 12:51 AM
  2. Content assist defaults
    By tenmiles in forum Eclipse
    Replies: 0
    Last Post: 03-24-2009, 06:21 AM
  3. Replies: 2
    Last Post: 01-23-2009, 04:09 PM
  4. Export an array to a text file
    By carderne in forum New To Java
    Replies: 32
    Last Post: 10-02-2008, 03:52 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
  •