On 22 Mrz., 14:38, Andrew Thompson <andrewtho...@gmail.com> wrote:
On Mar 22, 11:30 pm, Karsten Wutzke <kwut...@web.de> wrote:
Hello,
I'm referring to
Sub: Text component not focusable (tabbed pane switching involved) try
2: SSCCE
Hey Karsten. I try to look in on any thread that
mentions SSCCE in the title. An SSCCE is intended
to either
a) solve the problem for you, or..
b) make it easy for others to see what the
problem is.
Unfortunately, there are two problems with the
code you posted that inhibit that goal..
1) That code is *far* too wide and line wraps.
Line wrap causes all sorts of problems to code
examples. I recommend that no line be wider than
72 chars (less, if practical) but as I looked through
the 21 compilation errors that copy/pasted code
produced, it was obvious the *indent* of some lines
was 67 chars!
2) A 'proper' news client will understand that a post
might end with '}' but damnable GG screws it up.
If you add a 'sig.', even poor (retarded) GG
gets it right. Please include a sig. after
the end of your message. (Awaits the torrent of
abuse for daring suggest anybody cater to GG
or their users).
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;
}
}
}
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
(returned from each chat panel).
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!