Problems with a JTextField inside of a JTextPane
I have a JTextPane that needs to have individual words or phrases (in the DefaultStyledDocument) that can generate a MouseEvent. I've tried wrapping a JTextField in a Style (when adding it to the StyledDocument), this works nice and does create the type of MouseEvents that I'm looking for. But there a few problems...
1. The line height increases when inserting a JTextField. Screenshot: http://mirc-dll.com/images/java.gif
2. When highlighting in the document, the JTextField doesn't highlight.
3. When a JTextField is on a line of its own, the JTextField mouse events extend for the entire line, I only want to see the mouse events when the mouse is over the actual text.
Here is an example of both problems...
Note: for some reason, when I put a newline at the end of the string in doc.insertString it looks fine, but only in this example... In my actual code is still doesn't look right, It could be because I'm placing newlines at the beginning of strings (in the actual code), to avoid an empty line on the bottom of the JTextPane
Note: You can highlight just the hotlink, but when highlighting all text, it is not highlighted. Also for some reason, the example doesn't copy to the clipboard properly, unlike my actual code... ?
Note: Using JDK 6 Update 1
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
publicclass JTextPanelextends JPanel
{
finalstaticprivate StringFONTNAME ="SansSerif";
finalstaticprivateintFONTSIZE = 14;
private JScrollPanescrollPane;
private JTextPanetextPane;
private Stylenormal;
private Stylehotlink;
publicstaticvoid main(String [] args)
{
try{
SwingUtilities.invokeAndWait(new Runnable()
{
publicvoid run()
{
JFrameframe =new JFrame("Example");
Container pane = frame.getContentPane();
JTextPanel panel =new JTextPanel();
panel.setPreferredSize(new Dimension(180, 180));
panel.setMinimumSize(new Dimension(50, 50));
pane.add( panel );
frame.setSize( 190, 210 );
frame.setLocation( 0, 0 );
frame.setLayout(new FlowLayout() );
frame.setAlwaysOnTop(true);
frame.setVisible(true);
}
});
}catch (Exception e){}
}
public JTextPanel()
{
scrollPane =new JScrollPane(new JTextPane() );
scrollPane.setVerticalScrollBarPolicy(scrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(160, 160));
scrollPane.setMinimumSize(new Dimension(50, 50));
textPane = (JTextPane)scrollPane.getViewport().getView();
textPane.setEditable(false);
add(scrollPane);
print();
}
privatevoid print()
{
Style startStyle =
StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
normal = textPane.addStyle(null, startStyle);
StyleConstants.setFontFamily(normal, FONTNAME);
StyleConstants.setFontSize(normal, FONTSIZE);
JTextField textField =new JTextField("hotlink");
DefaultStyledDocumentdoc =
(DefaultStyledDocument)textPane.getStyledDocument();
textField.setFont(new Font(FONTNAME, 0, FONTSIZE) );
textField.setBorder(null);
textField.setOpaque(false);
textField.setEditable(false);
textField.addMouseListener(new MouseListener()
{
publicvoid mouseEntered(MouseEvent e)
{
System.out.println("Over Hotlink");
}
publicvoid mouseExited(MouseEvent e)
{
System.out.println("Left Hotlink");
}
publicvoid mousePressed(MouseEvent e)
{
System.out.println("Pressed Hotlink");
}
publicvoid mouseClicked(MouseEvent e){}
publicvoid mouseReleased(MouseEvent e){}
});
hotlink = doc.addStyle(null, normal);
StyleConstants.setComponent(hotlink, textField);
StyleConstants.setBold(hotlink,false);
StyleConstants.setUnderline(hotlink,true);
try{
doc.insertString(doc.getLength(),"Normal ", normal);
doc.insertString(doc.getLength()," ", hotlink);
doc.insertString(doc.getLength(),"Normal\n", normal);
}catch (Exception e){ e.printStackTrace();}
}
}
Any ideas, or other ways of doing this?
Naquada

