Insert image in JTextPane after,before or in text string
843805Mar 30 2007 — edited Mar 30 2007Soryy for my english!
I want to insert image(a smyle like in yahoo mess) in jtextpane. The image can be inserted in a string or after or before a string i use this code :
I want that image to be at the same level with the text,actually is in the up right corner of the text
Please help me!
/****************************************************************/
/* InterfataCuPoze */
/* */
/****************************************************************/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
/**
* Summary description for InterfataCuPoze
*
*/
public class InterfataCuPoze extends JFrame
{
private String newline="\n";
private String buttonString="Sugiuc";
// Variables declaration
private JTextArea inputArea;
private JScrollPane scroll;
private JTextPane outputArea;
private JScrollPane jScrollPane4;
private JPanel contentPane;
private JTextPane textPane;
//-----
private JButton buzz;
private JPanel butoane;
//-----
private JButton send;
private JPanel panel_send;
private JTextPane createTextPane() {
String[] initString =
{ "", //regular
"marian.radu : " , "buna dimineata fetelor"," "
};
String[] initStyles =
{ "regular","italic", "regular","icon"
};
textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();
addStylesToDocument(doc);
try {
for (int i=0; i < initString.length; i++) {
doc.insertString(doc.getLength(), initString,
doc.getStyle(initStyles[i]));
}
} catch (BadLocationException ble) {
System.err.println("Couldn't insert initial text into text pane.");
}
return textPane;
}
protected void addStylesToDocument(StyledDocument doc) {
//Initialize some styles.
Style def = StyleContext.getDefaultStyleContext().
getStyle(StyleContext.DEFAULT_STYLE);
Style regular = doc.addStyle("regular", def);
StyleConstants.setFontFamily(def, "Arial");
Style s = doc.addStyle("italic", regular);
StyleConstants.setBold(s, true);
StyleConstants.setForeground(s,Color.RED);
s = doc.addStyle("bold", regular);
StyleConstants.setBold(s, true);
s = doc.addStyle("small", regular);
StyleConstants.setFontSize(s, 10);
s = doc.addStyle("large", regular);
StyleConstants.setFontSize(s, 16);
s = doc.addStyle("icon", regular);
StyleConstants.setAlignment(s, StyleConstants.ALIGN_JUSTIFIED);
ImageIcon pigIcon =new ImageIcon("Smileys\\3.gif");
StyleConstants.setIcon(s, pigIcon);
}
public InterfataCuPoze()
{
super();
initializeComponent();
//
// TODO: Add any constructor code after initializeComponent call
//
this.repaint();
this.setVisible(true);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always regenerated
* by the Windows Form Designer. Otherwise, retrieving design might not work properly.
* Tip: If you must revise this method, please backup this GUI file for JFrameBuilder
* to retrieve your design properly in future, before revising this method.
*/
private void initializeComponent()
{
inputArea = new JTextArea();
scroll = new JScrollPane();
outputArea = createTextPane();
jScrollPane4 = new JScrollPane();
contentPane = (JPanel)this.getContentPane();
//-----
buzz = new JButton();
butoane = new JPanel();
//-----
send = new JButton();
panel_send = new JPanel();
//-----
//
// inputArea
//
//
// scroll
//
scroll.setViewportView(inputArea);
//
// outputArea
//
outputArea.setEditable(false);
//
// jScrollPane4
//
jScrollPane4.setViewportView(outputArea);
//
// contentPane
//
contentPane.setLayout(null);
addComponent(contentPane, scroll, 5,244,353,82);
addComponent(contentPane, jScrollPane4, 5,3,457,207);
addComponent(contentPane, butoane, 5,213,456,29);
addComponent(contentPane, panel_send, 363,243,98,83);
//
// buzz
//
buzz.setIcon(new ImageIcon("C:\\Documents and Settings\\Marian\\Desktop\\alert.gif"));
buzz.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
buzz_actionPerformed(e);
}
});
//
// butoane
//
butoane.setLayout(null);
butoane.setBackground(new Color(102, 102, 102));
addComponent(butoane, buzz, 9,2,42,25);
//
// send
//
send.setText("Send");
send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
send_actionPerformed(e);
}
});
//
// panel_send
//
panel_send.setLayout(null);
addComponent(panel_send, send, 7,26,81,37);
//
// InterfataCuPoze
//
this.setTitle("InterfataCuPoze - extends JFrame");
this.setLocation(new Point(5, 0));
this.setSize(new Dimension(478, 360));
}
/** Add Component Without a Layout Manager (Absolute Positioning) */
private void addComponent(Container container,Component c,int x,int y,int width,int height)
{
c.setBounds(x,y,width,height);
container.add(c);
}
//
// TODO: Add any appropriate code in the following Event Handling Methods
//
private void buzz_actionPerformed(ActionEvent e)
{
System.out.println("\nbuzz_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here
}
private void send_actionPerformed(ActionEvent e)
{
System.out.println("\nsend_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here
}
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
try
{
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
}
catch (Exception ex)
{
System.out.println("Failed loading L&F: ");
System.out.println(ex);
}
new InterfataCuPoze();
}
//= End of Testing =
}