This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using JTextfields in a JTabbedPane

 
Rudy Rodriguez
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I created a GUI application, that uses JTabbedPane.
Inside the Tabs, I am using JTextfields to display data from an Arraylist.

I am having problems sending data from my arraylist to the textfields in my gui.

I try to access the tab selected similar to accessing a arraylist.

However this does not seem as straight forward as I think..

Could someone provide some insight ???

Thanks

 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you have so far?
 
Rudy Rodriguez
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob

I have the following code below.

I am pulling data from the arraylist eloc, and trying to place the data in textfields in the JTabbedPane's txtTotalWater box..


 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about you use arrays of text fields, with the indexes being equal to the tab indexes? Or List<JTextField>? You can then use the tab index to directly retrieve the text fields.

Also, another oddity:
That's a lot of code repretition. In fact, the colors are the only real difference. So why not move the other two lines out of the switch statement?
You could also move the entire color code out of the if-statement if you use two arrays:
In fact, you could turn those two arrays into private static final fields.
 
Rudy Rodriguez
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob,

Thanks for the advice, I followed your advice and streamlined the method.

Works great..

How do I now access a Textfield in a panel of a JTabbedPane so I can add or remove String information ?

I have tried typing in "jtp.setSelectedIndex(tabindex);", trying to see if I can access it similar to a Arraylist.
But the IDE does not let me.

Thanks

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic