> > Hello,
>
[quoted text clipped - 31 lines]
> Please consider making code lines much shorter,
> and adding a sig., in future posts.
OK that was a little dirty... try #3:
Hope this works now...
Karsten
--------------------------------------
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
public class JChatsTabbedPane extends JTabbedPane
implements ChangeListener, ContainerListener
{
public static void main(String[] strArgs)
{
JFrame frm = new JFrame("Tab selection text pane focus test");
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setSize(600, 480);
final int MAX = 5;
final JChatsTabbedPane ctp = new JChatsTabbedPane();
frm.getContentPane().add(ctp);
//build menu
JMenuBar mb = new JMenuBar();
JMenu mn = new JMenu("Menu");
//add tab
Action actAdd =
new AbstractAction("Add tab")
{
public void actionPerformed(ActionEvent e)
{
int tc = ctp.getTabCount();
if ( tc < MAX )
{
JChatPanel cp = new JChatPanel(tc);
ctp.addTab("i = " + tc, null, cp, "tip");
}
}
};
mn.add(new JMenuItem(actAdd));
mn.addSeparator();
//remove tabs
for ( int i = 0 ; i < MAX ; i++ )
{
Action actRem =
new AbstractAction("Remove tab #" + (i + 1))
{
public void actionPerformed(ActionEvent e)
{
int tc = ctp.getTabCount();
String str = e.getActionCommand();
//hack: char '0' is int 48
int index = str.charAt(str.length() - 1) - 48;
if ( tc > 0 )
{
ctp.removeTabAt(index);
}
}
};
//actRem.setProp("Remove tab " + i);
JMenuItem mi = new JMenuItem(actRem);
//mi.setName("Remove tab " + i);
mn.add(mi);
}
mb.add(mn);
frm.setJMenuBar(mb);
frm.setVisible(true);
}
public JChatsTabbedPane()
{
//add change listener to selection model
getModel().addChangeListener(this);
addContainerListener(this);
}
public void stateChanged(ChangeEvent ce)
{
System.out.println("selection stateChanged");
final SingleSelectionModel ssm = getModel();
int tc = getTabCount();
//one tab is displayed when chat list empty,
//so there's always a selected index
if ( tc > 0 )
{
int index = ssm.getSelectedIndex();
System.out.println("Selected index is " + index);
if ( index >= tc )
{
return;
}
//valid index...
JChatPanel cp = (JChatPanel)getComponentAt(index);
System.out.println("Requesting focus on "
+ getTitleAt(index));
JTextComponent tcUserMessage =
cp.getUserMessageTextComponent();
//request focus on user message text component
tcUserMessage.requestFocus();
/*Document doc = tcUserMessage.getDocument();
try
{
//prove we the right text component
doc.insertString(doc.getEndPosition().getOffset(),
"" + index, null);
}
catch ( Exception e )
{
e.printStackTrace();
}*/
}
}
public void componentAdded(ContainerEvent ce)
{
//System.out.println("componentAdded");
}
public void componentRemoved(ContainerEvent ce)
{
//System.out.println("componentRemoved");
//stateChanged(null);
}
//inner class building the panels, not interesting!
public static class JChatPanel extends JPanel
{ //too lazy to indent!
private final JTextComponent tcUserMessage;
public JChatPanel(int i)
{
super(new BorderLayout());
String str = "Welcome to chat " + i + "!\n"
+ "\n"
+ "specialk > hello\n"
+ "agentk > hi specialk\n";
//chat messages text pane
JTextComponent tcChatMessages = new JTextPane();
tcChatMessages.setText(str);
tcChatMessages.setEditable(false);
//user message text pane
tcUserMessage = new JTextField( "panel index = " + i
+ ": user writes here");
//chat messages scroller
JScrollPane scrChatMessages = new JScrollPane();
scrChatMessages.setViewportView(tcChatMessages);
//user list
JScrollPane scrUserList = new JScrollPane();
scrUserList.setViewportView(new JList());
//chat split pane
JSplitPane splChat = new JSplitPane();
splChat.setLeftComponent(scrChatMessages);
splChat.setResizeWeight(0.8);
splChat.setRightComponent(scrUserList);
splChat.setEnabled(true);
//main horizontal divider split pane
JSplitPane splMain = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
true);
splMain.setResizeWeight(0.9);
splMain.setLeftComponent(splChat);
splMain.setRightComponent(null); //divider not movable
final JComponent cmpMain;
//user messages view
cmpMain = new JPanel(new BorderLayout());
cmpMain.add(splMain);
cmpMain.add(tcUserMessage, BorderLayout.SOUTH);
//add
add(cmpMain);
}
public JTextComponent getUserMessageTextComponent()
{
return tcUserMessage;
}
}
}
Karsten Wutzke - 22 Mar 2008 18:33 GMT
> > > Hello,
>
[quoted text clipped - 249 lines]
> }
> }
To make the program a little better you might add
private static int added = 0;
just before main() and replace the "add tab" action
//add tab
Action actAdd =
new AbstractAction("Add tab")
{
public void actionPerformed(ActionEvent e)
{
int tc = ctp.getTabCount();
if ( tc < MAX )
{
JChatPanel cp = new JChatPanel(added);
ctp.addTab("i = " + added++, null, cp, "tip");
}
}
};
Like this a global counter is used for new tabs which is better. But
the program should demonstrate well enough what is going on. Focus is
on why requestFocus doesn't work on the text field at the bottom
(returned from each chat panel).
Karsten
Karsten Wutzke - 22 Mar 2008 18:34 GMT
Oops sry forgot to remove the full quote...