Adding components to JTextPane

  • Follow


I'm trying to add a JButton to a JTextPane. The two schools of thought
are:

1: Wrap the button in a style such as
try {
        // Get the text pane's document
        JTextPane textPane = new JTextPane();
        StyledDocument doc = (StyledDocument)textPane.getDocument();

        // The component must first be wrapped in a style
        Style style = doc.addStyle("StyleName", null);
        StyleConstants.setComponent(style, new JButton("OK"));

        // Insert the component at the end of the text
        doc.insertString(doc.getLength(), "ignored text", style);
    } catch (BadLocationException e) {
    }

OR 2: Insert directly into the textpane, as
JTextPane pane = new JTextPane();
pane.insertComponent(new JButton("Click Me"));

Number 1 is the Java Tutorial method, however NEITHER of these work for
me...

All it does is add a small area (about a space length) that is
sunken... hard to explain... It's like a full size space has been
dropped into subscript, but when highlighted is just as tall.

EG: Highlighting the section with 2 characters on the left and one on
the right of the button looks like:
######   ###
############
############
      ###

0
Reply Philbo.B.Baggins (9) 8/9/2006 4:08:58 AM

On 9-8-2006 6:08, Philbo.B.Baggins@gmail.com wrote:
> I'm trying to add a JButton to a JTextPane. The two schools of thought
> are:
> 
> 1: Wrap the button in a style such as
> try {
>         // Get the text pane's document
>         JTextPane textPane = new JTextPane();
>         StyledDocument doc = (StyledDocument)textPane.getDocument();
> 
>         // The component must first be wrapped in a style
>         Style style = doc.addStyle("StyleName", null);
>         StyleConstants.setComponent(style, new JButton("OK"));
> 
>         // Insert the component at the end of the text
>         doc.insertString(doc.getLength(), "ignored text", style);
>     } catch (BadLocationException e) {
>     }
> 
> OR 2: Insert directly into the textpane, as
> JTextPane pane = new JTextPane();
> pane.insertComponent(new JButton("Click Me"));
> 
> Number 1 is the Java Tutorial method, however NEITHER of these work for
> me...
> 
> All it does is add a small area (about a space length) that is
> sunken... hard to explain... It's like a full size space has been
> dropped into subscript, but when highlighted is just as tall.
> 
> EG: Highlighting the section with 2 characters on the left and one on
> the right of the button looks like:
> ######   ###
> ############
> ############
>       ###
> 
I'm not sure if I understand your problem, but it seems you want the 
button and its text to align vertically centered with the text of the 
paragraph.
The following solution probably is a hack, but it wraps the JButton 
component in a Box component which has an empty border at the bottom. It 
causes the button to align better with the text of the paragraph. You 
may have to adjust the size of the bottom border (currently 6 pixels) to 
your likings.

// BEGIN of DocWithButton.java
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;

public class DocWithButton {

     public static void main(String[] args) throws Exception {
         JFrame app = new JFrame();
         app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

         Container cp = app.getContentPane();
         cp.setLayout(new BorderLayout());

         JTextPane tp = new JTextPane();
         StyledDocument doc = (StyledDocument) tp.getDocument();
         Style buttonStyle = doc.addStyle("ButtonStyle", null);

         JButton okButton = new JButton("OK");
         Box buttonBox = Box.createVerticalBox();
         buttonBox.add(okButton);
         buttonBox.setBorder(
               BorderFactory.createEmptyBorder(0, 0, 6, 0));
         StyleConstants.setComponent(buttonStyle, buttonBox);

         doc.insertString(doc.getLength(), "Lorem ipsum ", null);
         doc.insertString(doc.getLength(), "Ignored", buttonStyle);
         doc.insertString(doc.getLength(), " dolor sit amet", null);

         JScrollPane sp = new JScrollPane();
         sp.setViewportView(tp);
         cp.add(sp, BorderLayout.CENTER);
         app.setSize(400, 300);
         app.setVisible(true);
     }
}
// END of DocWithButton.java
-- 
Regards,

Roland
0
Reply Roland 8/10/2006 11:32:52 AM


Roland de Ruiter wrote:
> On 9-8-2006 6:08, Philbo.B.Baggins@gmail.com wrote:
> > I'm trying to add a JButton to a JTextPane. The two schools of thought
> > are:
> >
> > 1: Wrap the button in a style such as
> > try {
> >         // Get the text pane's document
> >         JTextPane textPane = new JTextPane();
> >         StyledDocument doc = (StyledDocument)textPane.getDocument();
> >
> >         // The component must first be wrapped in a style
> >         Style style = doc.addStyle("StyleName", null);
> >         StyleConstants.setComponent(style, new JButton("OK"));
> >
> >         // Insert the component at the end of the text
> >         doc.insertString(doc.getLength(), "ignored text", style);
> >     } catch (BadLocationException e) {
> >     }
> >
> > OR 2: Insert directly into the textpane, as
> > JTextPane pane = new JTextPane();
> > pane.insertComponent(new JButton("Click Me"));
> >
> > Number 1 is the Java Tutorial method, however NEITHER of these work for
> > me...
> >
> > All it does is add a small area (about a space length) that is
> > sunken... hard to explain... It's like a full size space has been
> > dropped into subscript, but when highlighted is just as tall.
> >
> > EG: Highlighting the section with 2 characters on the left and one on
> > the right of the button looks like:
> > ######   ###
> > ############
> > ############
> >       ###
> >
> I'm not sure if I understand your problem, but it seems you want the
> button and its text to align vertically centered with the text of the
> paragraph.
> The following solution probably is a hack, but it wraps the JButton
> component in a Box component which has an empty border at the bottom. It
> causes the button to align better with the text of the paragraph. You
> may have to adjust the size of the bottom border (currently 6 pixels) to
> your likings.
>
> // BEGIN of DocWithButton.java
> import java.awt.*;
> import javax.swing.*;
> import javax.swing.text.*;
>
> public class DocWithButton {
>
>      public static void main(String[] args) throws Exception {
>          JFrame app = new JFrame();
>          app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
>
>          Container cp = app.getContentPane();
>          cp.setLayout(new BorderLayout());
>
>          JTextPane tp = new JTextPane();
>          StyledDocument doc = (StyledDocument) tp.getDocument();
>          Style buttonStyle = doc.addStyle("ButtonStyle", null);
>
>          JButton okButton = new JButton("OK");
>          Box buttonBox = Box.createVerticalBox();
>          buttonBox.add(okButton);
>          buttonBox.setBorder(
>                BorderFactory.createEmptyBorder(0, 0, 6, 0));
>          StyleConstants.setComponent(buttonStyle, buttonBox);
>
>          doc.insertString(doc.getLength(), "Lorem ipsum ", null);
>          doc.insertString(doc.getLength(), "Ignored", buttonStyle);
>          doc.insertString(doc.getLength(), " dolor sit amet", null);
>
>          JScrollPane sp = new JScrollPane();
>          sp.setViewportView(tp);
>          cp.add(sp, BorderLayout.CENTER);
>          app.setSize(400, 300);
>          app.setVisible(true);
>      }
> }
> // END of DocWithButton.java
> --
> Regards,
>
> Roland

Thanks for the help, that will come in useful later, however my problem
is that the button does not display...

0
Reply Philbo 8/11/2006 1:28:02 AM

2 Replies
284 Views

(page loaded in 0.117 seconds)

Similiar Articles:













7/15/2012 3:55:26 PM


Reply: