How to address a component in a background tab in a JTabbedPane

Hello and greetings for everybody, this is my first post here. I have a question:

I want to write a simple little chat program, which makes use of a JTabbedpane, and if the user gets a private message from another user, it would open a new tab with the given user's nick in the tab title, and would create a JTextArea on the opened tab and write the message to that specific JTextarea. My question: Is it possible to write on the JTextarea, while the given tab is in the background? How can I address that specific JTextarea on the specified tab?

Thank you very much in advance for the answers, and I wis you all a nice day!

[644 byte] By [Eddie303a] at [2007-11-27 8:25:58]
# 1

It's possible to use the JTabbedPane api to get the component that's under a tab's label, but a better design would be to map "conversations" directly to the component it's written on. I put conversations in quotes because I'm not talking about the implementation (a textpane in a tab, etc.) I'm talking about the model of a conversation (made up of the text that's been written, the name of the people, etc.)

When you start a conversation, map the person's name to the component that you write on. Then you can just get the value for that name, and you'll have your component. Make sense?

hunter9000a at 2007-7-12 20:15:10 > top of Java-index,Java Essentials,Java Programming...
# 2
Thank you, but please, if you can, write a little example or write at least some methods which I must implement :)
Eddie303a at 2007-7-12 20:15:10 > top of Java-index,Java Essentials,Java Programming...
# 3
Hai Eddie, snd me codez 4 chaat prgm.
filestreama at 2007-7-12 20:15:10 > top of Java-index,Java Essentials,Java Programming...
# 4

> Thank you, but please, if you can, write a little

> example or write at least some methods which I must

> implement :)

That would require me to know much more of your design than I do/can. If you're talking about the tabbed pane method, look at getComponentAt(int).

hunter9000a at 2007-7-12 20:15:10 > top of Java-index,Java Essentials,Java Programming...
# 5

public static void privMsg(String nick) {

if (mainwindow.tabpane.indexOfTab(nick)==-1) {

javax.swing.JScrollPane panel=new javax.swing.JScrollPane();

javax.swing.JTextArea msgBox=new javax.swing.JTextArea();

msgBox.setEditable(false);

msgBox.setAutoscrolls(true);

msgBox.setLineWrap(true);

panel.setViewportView(msgBox);

mw.tabpane.addTab(nick,panel);

mw.tabpane.setSelectedIndex(mw.tabpane.indexOfTab(nick));

msgBox.append("Private conversation: "+nick+"\n");

}

Now this happens when I doubleclick the name of a person on a JList. I want to be able to refer to this msgBox while a new message comes from the network.

Eddie303a at 2007-7-12 20:15:10 > top of Java-index,Java Essentials,Java Programming...
# 6

// create a Map<String, JTextArea>

public static void privMsg(String nick) {

if (mainwindow.tabpane.indexOfTab(nick)==-1) { // look for the nick in the map, not on the tabbedpane (if map.get(nick) != null

javax.swing.JScrollPane panel=new javax.swing.JScrollPane();

javax.swing.JTextArea msgBox=new javax.swing.JTextArea();

msgBox.setEditable(false);

msgBox.setAutoscrolls(true);

msgBox.setLineWrap(true);

panel.setViewportView(msgBox);

mw.tabpane.addTab(nick,panel);

mw.tabpane.setSelectedIndex(mw.tabpane.indexOfTab(nick));

msgBox.append("Private conversation: "+nick+"\n");

// add the nick and the msgBox to the map

}

// if the nick already has been added to the map, (meaning there's already a tab open for that nick), then get that textpane and write to it.

That's the basic idea behind MVC, which you'd do yourself a huge favor by reading into. You separate the display of the data from the data itself. That way when you update the data (Model), you don't have to deal with gui code (View). The actions the user takes are also separated into the Controller.

hunter9000a at 2007-7-12 20:15:10 > top of Java-index,Java Essentials,Java Programming...
# 7
I understand what do you write, but I am a very bad programmer to implement that :( I mean I don't know how to create the map, and how to name the message box in a custom way.
Eddie303a at 2007-7-12 20:15:10 > top of Java-index,Java Essentials,Java Programming...
# 8

> I understand what do you write, but I am a very bad

> programmer to implement that :( I mean I don't know

> how to create the map, and how to name the message

> box in a custom way.

This should help you understand maps better:

http://java.sun.com/docs/books/tutorial/collections/interfaces/map.html

Especially the "The Map Interface" and "Map Interface Basic Operations" sections.

hunter9000a at 2007-7-12 20:15:10 > top of Java-index,Java Essentials,Java Programming...
# 9
This would not need me to give other name for the msgBox when creating a new tab?
Eddie303a at 2007-7-12 20:15:10 > top of Java-index,Java Essentials,Java Programming...
# 10
Thank you very very much, this one saved me from much-much pain !!! :)
Eddie303a at 2007-7-12 20:15:10 > top of Java-index,Java Essentials,Java Programming...
# 11
Eddie, i m in same boat. snd me chaat.
filestreama at 2007-7-12 20:15:10 > top of Java-index,Java Essentials,Java Programming...
# 12
I will povide you one copy, if it will be finished, the thing is, that it is and has the function and variable names in hungarian, since that is my mother tongue.
Eddie303a at 2007-7-12 20:15:10 > top of Java-index,Java Essentials,Java Programming...
# 13

> I will povide you one copy, if it will be finished,

> the thing is, that it is and has the function and

> variable names in hungarian, since that is my mother

> tongue.

In that case, perhaps you could send young 'streamie zserbo or kifli instead of Indian chaat.

DrLaszloJamfa at 2007-7-12 20:15:10 > top of Java-index,Java Essentials,Java Programming...
# 14
A Magyar m閘ange?
filestreama at 2007-7-12 20:15:10 > top of Java-index,Java Essentials,Java Programming...
# 15
Mi az a mlange? Erdlyi vagyok, nem rtem :D(What is a Melange? I am from Transylvania, and I don't understand)
Eddie303a at 2007-7-21 22:40:32 > top of Java-index,Java Essentials,Java Programming...