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

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Douglas Grealis
  • 599
  • 2
  • 11
  • 25

2 Answers2

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){}
            }
        });
    }
}
Vishal K
  • 12,976
  • 2
  • 27
  • 38
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

mKorbel
  • 109,525
  • 20
  • 134
  • 319