|
|
|
1000 Java Tips ebook
|
|
 

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java
programming articles and tips. Please take your copy here.
Take your copy of free "Java Technology Screensaver"!. |
|
Easy Learn Java: Programming Articles, Examples and Tips - Page 377
Previous
1060 Stories (530 Pages, 2 Per Page)
Next
JButton Appearance
|
You don't have to do anything special with a button, but there are many methods
for changing the appearance. You can change the alignment of the text and icon,
set the font, have different icons when the button is pressed or "rolled over",
set margins and borders, change the color, etc
Normally, you don't need to use the ActionEvent parameter that is passed
to the listener -- it's enough to know that the button was clicked. However,
you can find out things such as the time to button was clicked, or which
modifier keys were pressed, if you need them. You can also get a special
code that you associated with that button if you need it.
Dynamically changing a button
You can change the text or icon (image) that appears on a button with:
btn.setText("someText");
btn.setIcon(anIcon);
WARNING: If you change the size of the button by one of these changes,
this may have consequences in the layout of the components. This may require a
call to validate() or revalidate() which then computes
a new layout based on the change in the button size. This is not a quick
operation. Replacing an icon with another of the same size should not cause any
problems.
comments? | | Score: 0
|
Posted by jalex on Friday, June 24, 2005 (00:00:00) (3049 reads)
|
java.util.StringTokenizer
|
Java:
java.util.StringTokenizer
Purpose
The java.util.StringTokenizer class is used to break strings into
tokens (words, numbers, operators, or whatever). A more powerful solution
is to use regular expressions, which have been added to Java 1.4.
A StringTokenizer constructor takes a string to break into tokens and returns a
StringTokenizer object for that string. Each time its nextToken()
method is called, it returns the next token in that string. If you don't specify
the delimiters (separator characters), blanks are the default.
Constructors
StringTokenizer st = new StringTokenizer(s);
- Creates a StringTokenizer for the String s that uses whitespace
(blanks, tabs, newlines, returns, form feeds) as delimiters.
StringTokenizer st = new StringTokenizer(s,
d);
- Creates a StringTokenizer for the String s using delimiters from
the String d.
StringTokenizer st = new StringTokenizer(s,
d, f);
- Creates a StringTokenizer for the String s using delimiters from
the String d. If the boolean f is
true, each
delimiter character will also be returned as a token.
Common Methods
Assume that st is a StringTokenizer.
st.hasMoreTokens() -- Returns
true if there are more tokens.
st.nextToken() -- Returns the next
token as a String.
st.countTokens() -- Returns the int
number of tokens. This can be used to allocate an array before starting,
altho it can be inefficient for long strings because it has to scan the
string once just to get this number. Using a Vector and converting it to an
array at the end may be a better choice.
Example: Find the longest word in a String
This code finds the longest word (characters separated by delimiter characters)
in the String s, using blanks, commas, and tabs as delimiters.
// Assume s contains a string of words
String longestWord = "";
StringTokenizer st = new StringTokenizer(s, " , ");
while (st.hasMoreTokens()) {
String w = st.nextToken();
if (w.length() > longestWord.length()) {
longestWord = w;
}
}
comments? | | Score: 0
|
Posted by jalex on Thursday, June 23, 2005 (00:00:00) (4602 reads)
|
|
|