Reputation: 325
I have this program which accepts input from the user through JOptionPane. What my program is doing is recording everything I input from the JOptionPane in the JTextArea. Now, I want to remove some of my input in the JTextArea but I don't know how.
I'm thinking that there must be something like a JList that I can use instead of a JTextArea. However, I'm not too familiar with JList and it uses arrays. I don't really know arrays D:
Thanks to anyone who can help!
Upvotes: 1
Views: 1224
Reputation: 324207
However, I'm not too familiar with JList and it uses arrays
No, it uses a ListModel
.
Look at the DefaultListModel
. You can dynamically add/remove elements from the model.
Upvotes: 2
Reputation: 9676
Arrays are one of the most fundamental constructs in programming, and I'd highly suggest experimenting with those as it does seem like this would be the best solution to your problem. The official Java tutorial is not too too bad, but it is a little dry.
Basically, arrays are there for your convenience. It allows you to declare the space for a set of variables in one line, so rather than:
int var1, var2, var3, var4, var5, /* snip */ , var100;
One can declare the space for all one hundred variables with a single statement:
int[100] var;
And reference them using the syntax var[1]
, var[2]
, etc, instead. It also helps when you need to do something to each of the members of the array, say, initializing.
Rather than initializing each in its own statement, like so:
var1 = 0; var2 = 0; var3 = 0; /* snip */ var100 = 0;
Instead, one can loop over the whole array and use a single statement to update each of the variables in the array, like so:
for (int i = 0; i < 100; i++) {
var[i] = 0;
}
Not only does it save on the amount of code, but this will be much easier to maintain if, say, one needs to change the number of variables.
Upvotes: 2