Hi,
Any Help, I need guide/way on how to get Image from JLabel and set it to JTextArea in netbeans when a button is clicked.
I have 3 Object: (1) Jlabel(with an imported picture in it) (2) JTextArea(to store the picture when Button is clicked) (3)A button

I tried using a method:

private Icon Icon;
private Icon Image;
public Icon getImage(){
    return Icon;

}


//CODE FOR BUTTON(GET IMAGE)

Image=l.getIcon();
S.setText(""+Image); // S is the variable name for the JTextArea

it on captures the file link of the image and displays it in the textArea field when the Button is clicked

Dani AI

Generated

is right: a JTextArea is for plain text. If you need text and an image together (so they flow and print correctly), switch to a JTextPane or JEditorPane with a StyledDocument. You can grab the Icon from your JLabel and insert it inline; it will behave like a character in the text, not an overlay.

Here is a minimal example wired to your button. It copies the icon from jLabel1 and inserts it at the caret/end of a JTextPane named textPane:

button.addActionListener(e -> {
    Icon icon = jLabel1.getIcon();
    if (icon == null) return;

    // Optional: scale if needed
    if (icon instanceof ImageIcon) {
        Image img = ((ImageIcon) icon).getImage()
                       .getScaledInstance(120, -1, Image.SCALE_SMOOTH);
        icon = new ImageIcon(img);
    }

    StyledDocument doc = textPane.getStyledDocument();
    Style style = textPane.addStyle("img", null);
    StyleConstants.setIcon(style, icon);
    try {
        doc.insertString(doc.getLength(), " ", null);   // text before
        doc.insertString(doc.getLength(), " ", style);  // inserts the icon
        doc.insertString(doc.getLength(), "\n", null);  // text after/newline
    } catch (BadLocationException ex) {
        ex.printStackTrace();
    }
});

For printing, call textPane.print(); Swing will render both text and icon on the page. Avoid jTextArea1.add(jLabel1, 0) as suggested; it overlays a component on top of the text (exactly what you observed) and will not flow or print as part of the document. If you absolutely must keep JTextArea, the only options are overlays or custom painting, which are brittle for layout and printing. Using JTextPane keeps things simple and reliable.

Recommended Answers

All 6 Replies

A JTextArea is really bad choice of component to display an image - it's specifically designed to display lines of text. Maybe you should re-think your choice of components?
If you explain what you are trying to achieve maybe someone can suggest an easier way to achieve it.

I think the best way to insert would be to insert it as a background image.

JtextArea textArea = new JtextArea(){
  public void paintComponent(Graphics g){
      // the image variable should be replaced with your image
     g.drawImage(image, 0, 0, (int)getSize().getWidth(), (int)getSize().getHeight(), this);
     super.paintComponent(g);
  }
};

Thanx for your contribution, actually am working on a project, and the user is expected to input variables, and these variable directed to the TextArea icluding an Image which i imported to the Jframe via JLable, for Printing: my challenge now is getting the Image from the JLabel, and setting it to appear on the JTextArea for onward printing.

You might want to have a look at this: Click Here

try this
jTextArea1.add(jLabel1, 0);

Hi,
Thanks Lena1990, u code really helped, though i realised i could make the image output together with text output, it is always fixed at the centre of the jTextArea on top of the text ouput thereby blocking the text outputted, it was nice knowing that a simple code like dis cld solve my pussle, but am still searching for a better way to output image and text in an formatted format. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.