Adding a JButton to a JTextPane
Asked Answered
S

2

5

I'm having problems trying to add a JButton to a JTextPane with a String. So what I am trying to do is to add each String in a for loop and then add ad JButton after that added String. The code below is what I am trying to accomplish.

ArrayLst<String> data = new ArrayList();
data.add("Data here");
data.add("Data here 2");
data.add("Data here 3");
data.add("Data here 4");

Container cp = getContentPane();

JTextPane pane = new JTextPane();
SimpleAttributeSet set = new SimpleAttributeSet();
StyleConstants.setBold(set, true);
pane.setBackground(Color.BLUE);
pane.setEditable(false);

Document doc = pane.getStyledDocument();

for(int i=0; i<data.size(); i++)
{
    doc.insertString(doc.getLength(), data.get(i)+ "\n", set);
    pane.insertComponent(new JButton("View Info"));
}

Can anyone tell me how I can add a JButton to each of the Strings on the same line?

Thanks very much appreciated

Selfeffacement answered 4/4, 2013 at 18:35 Comment(0)
T
6

You can try like this:
enter image description here

import javax.swing.*;
import javax.swing.text.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

class TextPaneDemo extends JFrame
{
    public void createAndShowGUI()throws Exception
    {
        JTextPane tp = new JTextPane();
        ArrayList<String> data = new ArrayList();
        data.add("Data here");
        data.add("Data here 2");
        data.add("Data here 3");
        data.add("Data here 4");
        getContentPane().add(tp);
        setSize(300,400);
        StyledDocument doc = tp.getStyledDocument();
        SimpleAttributeSet attr = new SimpleAttributeSet();
        for (String dat : data )
        {
            doc.insertString(doc.getLength(), dat, attr );
            tp.setCaretPosition(tp.getDocument().getLength());
            tp.insertComponent(new JButton("Click"));
            doc.insertString(doc.getLength(), "\n", attr );
        }

        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    public static void main(String[] args) 
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                TextPaneDemo tpd = new TextPaneDemo();
                try
                {
                    tpd.createAndShowGUI(); 
                }
                catch (Exception ex){}
            }
        });
    }
}
Thieve answered 4/4, 2013 at 19:4 Comment(0)
C
2

Can anyone tell me how I can add a JButton to each of the Strings on the same line?

  • remove LineSeparator ("\n") from doc.insertString(doc.getLength(), data.get(i)+ "\n", set);

pseudo code could be

for (int i = 0; i < data.size(); i++) {
    try {
        doc.insertString(doc.getLength(), data.get(i), set);
        textPane.insertComponent(new JButton("View Info"));
        doc.insertString(doc.getLength(), "\n", set);
    } catch (BadLocationException ex) {
    }    
}
  • with output

nnn

Calutron answered 4/4, 2013 at 19:8 Comment(1)
Thanks for the reply. The output was a bit messy, but I got it solved. Much appreciatedSelfeffacement

© 2022 - 2024 — McMap. All rights reserved.