Search code examples
javaswingjlabeljtextarea

can JLabel be added to JTextArea?


Is there a way to add a JLabel to JtextArea? cause I try the add but it didnt work and set it to visible true, is it allowed to add a JLabel inside the JTextArea? through append?

Here is my current code that didnt work

jta = new JTextArea();
jta.setEditable(false);
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
jta.setFont(new Font("calibri", Font.PLAIN,16));
jlArray = new JLabel("radsjhkaljk sadf");
jta.add(jlArray);
jta.setVisible(true);
jspTextField = new JScrollPane(jta);

Can I append the JLabel inside the JTextArea each time a message is added?


Solution

  • JTextArea is not the appropriate swing component to insert JLabels or any other components within it. You can use JTextPane for this purpose. For example , consider the code given below: enter image description here

    import javax.swing.*;
    import javax.swing.text.*;
    import javax.swing.border.*;
    import java.awt.event.*;
    import java.awt.*;
    
    class JLabelToTextPane extends JFrame implements ActionListener
    {
        JTextField tf;
        JTextPane tp ;
        JButton click;
        StyledDocument doc;
        SimpleAttributeSet attr;
        public void createAndShowGUI()
        {
            setTitle("Add JLabel to JTextPane");
            tf = new JTextField(10);
            tp = new JTextPane();
            click = new JButton("Click");
            doc = tp.getStyledDocument();
            attr = new SimpleAttributeSet();
            JScrollPane pane = new JScrollPane(tp);
            JPanel nPanel = new JPanel();
            nPanel.add(tf);nPanel.add(click);
            tf.addActionListener(this);
            click.addActionListener(this);
            Container c = getContentPane();
            c.add(nPanel,BorderLayout.NORTH);
            c.add(pane);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(300,200);setLocationRelativeTo(null);setVisible(true);
        }
        @Override
        public void actionPerformed(ActionEvent evt)
        {
            String text =  tf.getText();
            if (text!=null && !"null".equals(text) && !"".equals(text.trim()))
            {
                JLabel label = new JLabel(text);
                label.setOpaque(true);
                label.setBackground(Color.gray);
                label.setBorder(BorderFactory.createLineBorder(Color.black,1));
                tp.setCaretPosition(tp.getDocument().getLength());
                tp.insertComponent(label);
                label.addMouseListener(new MouseAdapter()
                {
                    public void mouseClicked(MouseEvent evt)
                    {
                        String text = ((JLabel)evt.getSource()).getText();
                        JOptionPane.showMessageDialog(JLabelToTextPane.this,"Hi, My text is "+text,"Information",JOptionPane.INFORMATION_MESSAGE);
                    }
                });
                try
                {
                    doc.insertString(doc.getLength(), " ", attr );  
                }
                catch (BadLocationException ex)
                {
                    ex.printStackTrace();
                }
            }
        }
        public static void main(String[] args) 
        {
            SwingUtilities.invokeLater( new Runnable()
            {
                @Override
                public void run()
                {
                    JLabelToTextPane lta = new JLabelToTextPane();
                    lta.createAndShowGUI();
                }
            });
        }
    }