Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #19190 > unrolled thread
| Started by | bob smith <bob@coolfone.comze.com> |
|---|---|
| First post | 2012-10-08 12:52 -0700 |
| Last post | 2012-10-09 16:01 -0700 |
| Articles | 7 — 7 participants |
Back to article view | Back to comp.lang.java.programmer
Object message? bob smith <bob@coolfone.comze.com> - 2012-10-08 12:52 -0700
Re: Object message? Arne Vajhøj <arne@vajhoej.dk> - 2012-10-08 15:58 -0400
Re: Object message? Knute Johnson <nospam@knutejohnson.com> - 2012-10-08 13:00 -0700
Re: Object message? Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-10-08 16:13 -0400
Re: Object message? Roedy Green <see_website@mindprod.com.invalid> - 2012-10-09 02:11 -0700
Re: Object message? Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2012-10-09 23:46 +0200
Re: Object message? Lew <lewbloch@gmail.com> - 2012-10-09 16:01 -0700
| From | bob smith <bob@coolfone.comze.com> |
|---|---|
| Date | 2012-10-08 12:52 -0700 |
| Subject | Object message? |
| Message-ID | <9621cdc5-4aa4-4057-894f-93ef69915871@googlegroups.com> |
Can someone tell me why "message" is an Object here in javax.swing.JOptionPane?
Why isn't it a String?
showMessageDialog
public static void showMessageDialog(Component parentComponent,
Object message)
throws HeadlessException
Brings up an information-message dialog titled "Message".
[toc] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2012-10-08 15:58 -0400 |
| Message-ID | <50733068$0$293$14726298@news.sunsite.dk> |
| In reply to | #19190 |
On 10/8/2012 3:52 PM, bob smith wrote: > Can someone tell me why "message" is an Object here in javax.swing.JOptionPane? > > Why isn't it a String? > > showMessageDialog > public static void showMessageDialog(Component parentComponent, > Object message) > throws HeadlessException > Brings up an information-message dialog titled "Message". If you really want to know, then lookup the author in the source and email him. For a guess: they wanted to make it convenient for callers, so they call toString instead of caller having to. Arne
[toc] | [prev] | [next] | [standalone]
| From | Knute Johnson <nospam@knutejohnson.com> |
|---|---|
| Date | 2012-10-08 13:00 -0700 |
| Message-ID | <k4vbcg$vs6$1@dont-email.me> |
| In reply to | #19190 |
On 10/8/2012 12:52 PM, bob smith wrote:
> Can someone tell me why "message" is an Object here in javax.swing.JOptionPane?
>
> Why isn't it a String?
>
> showMessageDialog
> public static void showMessageDialog(Component parentComponent,
> Object message)
> throws HeadlessException
> Brings up an information-message dialog titled "Message".
>
import java.awt.*;
import javax.swing.*;
public class test8 {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JLabel l = new JLabel("Because I'm an Object");
JOptionPane.showMessageDialog(null,l);
}
});
}
}
--
Knute Johnson
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2012-10-08 16:13 -0400 |
| Message-ID | <k4vc5f$5ht$1@dont-email.me> |
| In reply to | #19190 |
On 10/8/2012 3:52 PM, bob smith wrote:
> Can someone tell me why "message" is an Object here in javax.swing.JOptionPane?
Um, er, have you considered reading the class' Javadoc?
"A descriptive message to be placed in the dialog box.
In the most common usage, message is just a String or
String constant. However, the type of this parameter
is actually Object. Its interpretation depends on its
type: [...]"
> Why isn't it a String?
So it can be something other than a String, of course.
--
Eric Sosman
esosman@comcast-dot-net.invalid
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2012-10-09 02:11 -0700 |
| Message-ID | <4gq778dca0tk3db180c2aou7qd7jkkqta1@4ax.com> |
| In reply to | #19190 |
On Mon, 8 Oct 2012 12:52:56 -0700 (PDT), bob smith <bob@coolfone.comze.com> wrote, quoted or indirectly quoted someone who said : >Can someone tell me why "message" is an Object here in javax.swing.JOptionP= >ane? > >Why isn't it a String? In general Object can let you pass any sort of information to yourself, not just a string. It can be formatted data. The disadvantage is you must cast it to a String. -- Roedy Green Canadian Mind Products http://mindprod.com The iPhone 5 is a low end Rolex.
[toc] | [prev] | [next] | [standalone]
| From | Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> |
|---|---|
| Date | 2012-10-09 23:46 +0200 |
| Message-ID | <k5262u$vio$1@dont-email.me> |
| In reply to | #19203 |
On 09/10/2012 11:11, Roedy Green allegedly wrote: > On Mon, 8 Oct 2012 12:52:56 -0700 (PDT), bob smith > <bob@coolfone.comze.com> wrote, quoted or indirectly quoted someone > who said : > >> Can someone tell me why "message" is an Object here in javax.swing.JOptionP= >> ane? >> >> Why isn't it a String? > > In general Object can let you pass any sort of information to > yourself, not just a string. It can be formatted data. The > disadvantage is you must cast it to a String. Not quite, Roedy. Among other things, it can be any JComponent. And that's extremely useful. -- DF.
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2012-10-09 16:01 -0700 |
| Message-ID | <06d9d085-2a5a-4b89-b380-1b6b7f5f2444@googlegroups.com> |
| In reply to | #19209 |
Daniele Futtorovic wrote: > Roedy Green allegedly wrote: >> bob smith wrote, quoted or indirectly quoted someone who said : >>> Can someone tell me why "message" is an Object here in javax.swing.JOptionPane? >>> Why isn't it a String? > >> In general Object can let you pass any sort of information to >> yourself, not just a string. It can be formatted data. The >> disadvantage is you must cast it to a String. No, you mustn't, actually. That would defeat the purpose of the 'message' argument. > Not quite, Roedy. Among other things, it can be any JComponent. And > that's extremely useful. The other things: an 'Icon', an array, a 'Component' (not just 'JComponent', per the docs), and of course, any ol' 'Object' via its 'toString()'. Also, in general you cannot cast a reference to 'String'. Reference casts must adhere to http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.5.1 You can use 'String' conversion or a transformation method. http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.4 http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#valueOf(java.lang.Object) http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString() et al. As for the OP's question, IT'S IN THE JAVADOCS! http://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html "message "A descriptive message to be placed in the dialog box. In the most common usage, message is just a String or String constant. However, the type of this parameter is actually Object. Its interpretation depends on its type: ..." Followed by an explanation of what it does for each type. So, no, you don't have to convert/transform the reference to a 'String', contrary to what was said upthread. bob, you might not be aware of the full power of the Javadocs. The ones for 'JOptionPane' show a lot of that power. It's got class-level documentation, which you should go ahead and read after all, field (static constant) documentation, constructor documentation, method documentation, nested class documentation, and of course, package documentation through the 'javax.swing' package documentation. It's also got hyperlinked documentation, some of which in Swing's case seems to have gotten sparse and some of which is good. Swing is a tricky package. First and foremost it's a UI-builder platform. I've used a bunch, such as OpenLook, and quite often they are messy, but creatively and purposefully so. It's also an API. This means it speaks in two domains of discourse. Types in an API have members, but that's a different "containment" than a UI component exercises over another. The former is class membership and the latter is geometric enclosure, sort of. So the method could have had a bunch of overloads, one for each different 'message' type (not to be confused with a 'messageType'), but that could lead to an explosion of methods, so they chose to stick with 'Object' and presumably reflectively work its display magic. As a UI component it has the luxury of time to examine its arguments, but you do lose the safety net of compile-time checks. -- Lew
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web