How to autofit JEditorPane in a jpanel?

Hi everybody,
I have added a JEditorPane to a JPanel wrapped by a splitpane, as displayed in [this snapshot|http://img40.imageshack.us/img40/7540/jeditorpaneissue.png].
The splitpanel works fine, but for some reason the JEditorPane has a fixed size and won't fit the parent when I resize it by dragging the split bar (the red arrows in the image show the annoying gap).
I'd like to improve this GUI with 2 things:
1) autofit: when I change the size of the window or I drag the split bar, the JEditorPane should fit the new size automatically. I suppose I can do this with a listener, but I was wondering if there is better way of doing it (e.g. some settings in the panel).
2) vertical scrollbar: I obviously need a scrollbar for the JEditorPane, but I don't know where I am supposed to add it.
Thanks for any hints!
Mulone

PS: here's the code:
JEditorPane:
public class Recommender extends JEditorPaneInit function of the JEditorPane:
void init(){
setEditorKit(new StyledEditorKit());
setContentType( "text/html" );
setEditable(false);
setPreferredSize(new Dimension(300,600));Place where I load the JEditorPane
InfoBrowserPanel infoBr = new InfoBrowserPanel();
infoBr.add( UcdNavManager.getInstance().getRecommender() );
mapHandler.add( infoBr );InfoBrowserPanel (just a container):
public class InfoBrowserPanel extends JPanel implements BeanContextChild, BeanContextMembershipListenerthen I load it into a splitPane
if(someObj instanceof InfoBrowserPanel){
Debug.message("basic", "OpenMapFrame: found an InfoBrowserPanel");
InfoBrowserPanel bp = (InfoBrowserPanel)someObj;
splitPane.setRightComponent(bp);which is defined as:
private JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);That's pretty much it.
Mulone

Similar Messages

  • How to get BufferedImage of a JPanel

    Can anyone tell me : how to get BufferedImage of a JPanel

    I've done something like this:
    import java.awt.Graphics;
    import java.awt.LayoutManager;
    import java.awt.Dimension;
    import java.awt.image.BufferedImage;
    import java.io.InputStream;
    import java.io.FileInputStream;
    import javax.swing.JPanel;
    import com.sun.image.codec.jpeg.JPEGCodec;
    import com.sun.image.codec.jpeg.JPEGImageDecoder;
    * @author Martin Breton
    public class JBackgroundImagePanel extends JPanel {
         private BufferedImage mImage = null;
         private InputStream mImageStream = null;
         public  String mImageFilename = null;
         public JBackgroundImagePanel( String ImageFilename ) {
              super();
              mImageFilename = ImageFilename;
              initialize();
         public JBackgroundImagePanel( BufferedImage ImageFilename ) {
              super();
              mImage = ImageFilename;
              initialize();
         public JBackgroundImagePanel( InputStream ImageStream ) {
              super();
              mImageStream= ImageStream;
              initialize();
         private  void initialize() {
              if(mImage == null)
                   try {
                        ClassLoader Loader = this.getClass().getClassLoader();
                        if( mImageStream == null )
                             mImageStream = Loader.getResourceAsStream(mImageFilename);
                             if( mImageStream == null )
                                 mImageStream = new FileInputStream( mImageFilename );     
                        JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder( mImageStream );
                        mImage = decoder.decodeAsBufferedImage();
                        mImageStream.close();
                   catch(Exception e){
                        System.out.print("Image could not be loaded in JBackgroundImagePanel.");
              this.setSize(mImage.getWidth(),mImage.getHeight());
         public void paintComponent(Graphics g) {
              super.paintComponent(g);
              g.drawImage(mImage, 0, 0, null);
    }Hope this helps.
    proxi

  • How to embedd html page in JPanel

    Hello!
    I am generating a jasper report and exporting the output to HTML file and then i will have to embedd this page in JPanel..How do I do??If this is not possible let me know the way of displaying jasper report in JPanel;

    Use a JEditorPane - for example:
    JEditorPane RigArea () {JEditorPane Area = new JEditorPane();
                    Area.setContentType("text/html");Area.setEditable(false);
                    return(Area);}
    JPanel MyPanel=new JPanel();MyPanel.setLayout(new BoxLayout(MyPanel, BoxLayout.Y_AXIS));
    JScrollPane Pane = new JScrollPane(MyPanel);Pane.setPreferredSize(new Dimension(7*AppLength+60, 600));
    JEditorPane MyAnswers=RigArea();
    JScrollPane MyScrollPane = RigScrollPane(MyAnswers,"Details presentation", 900, 600);
    MyScrollPane.setBackground(new Color(new Float(0.9), new Float(0.9), new Float(0.9)));
    MyAnswers.setBackground(new Color(new Float(0.9), new Float(0.9), new Float(0.9)));
    MyAnswers.setText(Answer);
    MyPanel.add(MyAnswers);
    Pane.setPreferredSize(new Dimension(900,600));
    JOptionPane.showMessageDialog(p, Pane);

  • How to make JEditorPane support applet?

    hi hi,
    i would like to know how to make the JEditorPane support applet coming from internet source. i will be using JEditorPane to view web page but if it contains applet, how can i make JEditor support it? what should i need to do so. pls give me some guide lines. thanks

    That's an interesting question. The short answer is, you can't. For one, the APPLET tag is ignored by the Swing HTML parser. Then, I'm not sure, what happens if you embedd heavy-weight components like applets into an editor pane.
    However, you can use  JEditorPane e = new JEditorPane();
      e.setText("Hallo, Welt");
      e.setCaret(5);
      e.insertComponent(new JButton("Click me"));to add swing components to your text. So you might be able to create a working solution based on this foundation.
    I can imagine two approaches. You could either create your own applet-like JEditorPanelets which would be basically JPanels with some glue code to allow the the panelet to access its editorPane.
    Or you could explore the approach to add a heavy weight applet and write a compatiblily stub. Here's an outline  class AppletAwareEditorPane extends JEditorPane {
        List applets;
        MyAppletStub stub;
        public void insertApplet(Applet a) {
          applets.add(a);
          a.setStub(stub);
          a.init();
        public void startApplets() {
          Iterator i = applets.iterator();
          while(i.hasNext()) ((Applet)i.next()).start();
        }However, each applet would require to run in its own tread and I'm pretty sure this would break swing as it is not thread save.
    bye

  • How do i open a new JPanel on mouse click

    i'm trying to make the program open a new JPanel when i click on a button in the main JPanel.
    how do i do this?

    regarding that example given ( [CardLayoutDemo.java|http://java.sun.com/docs/books/tutorial/uiswing/examples/layout/CardLayoutDemoProject/src/layout/CardLayoutDemo.java] )
    when i add buttons/text fields, and/or new JPanels, the changes are not reflected when i compile and run it. other than the title of the JFrame.
    oh, never mind. it is because of a one liner stupid mistake.

  • How do I draw on a Jpanel

    I've spent around 15 hours trying to work out how to draw something as simple as a line onto a JPanel!
    I kind of understand about using paint, set color etc. if I was writing code in notepad I'm sure that it wouldn't be a problem but as I'm using the JDK SE form editor I'm very confused as to what should be done.
    Basically i have a frame with 2 panels in gridLayout position and filled top to bottom left to right equally spaced.
    I've added a button to the left panel. When this button is clicked I want to draw a line on the right panel. It's that simple! If I can do that then I can move on.
    All the examples i've read don't show what to do when using the JDK SE form editor. Please can someone tell me exactly what to do...thanks

    see if you can follow this
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    class Testing extends JFrame
      public Testing()
        setSize(400,300);
        setLocation(200,100);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        getContentPane().add(new DrawPanel());
        pack();
      public static void main(String[] args){new Testing().setVisible(true);}
    class DrawPanel extends JPanel
      java.util.List points = new java.util.ArrayList();
      public DrawPanel()
        setPreferredSize(new Dimension(400,300));
        setBackground(Color.WHITE);
        addMouseListener(new MouseAdapter(){
          public void mousePressed(MouseEvent me){
            points = new java.util.ArrayList();
            points.add(me.getPoint());}});
        addMouseMotionListener(new MouseMotionListener(){
          public void mouseMoved(MouseEvent me){}
          public void mouseDragged(MouseEvent me){
            points.add(me.getPoint());
            DrawPanel.this.repaint();}});
      public void paintComponent(Graphics g)
        super.paintComponent(g);
        if(points.size() > 0)
          Point sPt = (Point)points.get(0);
          for(int x = 1, y = points.size(); x < y; x++)
            Point ePt = (Point)points.get(x);
            g.drawLine(sPt.x,sPt.y,ePt.x,ePt.y);
            sPt = ePt;
    }if you want a bit of advice - dump the gui builder

  • How to set Tab ordering in JPanel

    hi all
    i add a JPanel on a JFrame and Various swing Controls on that JPanel. now i want to change tab ordering of components present in my JPanel.
    please guide me how can i do this. i have tried setFocusTraversalPolicy but it does not work with JPanel.

    You should read the article from Sun:
    "How to Use the Focus Subsystem"
    http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html
    Moshe

  • How to show google maps in JPanel

    How do i show the google maps in Java Application.
    Please help me.

    Yup, that's it. Notice that it's "The Google Maps *JavaScript* API" so you're going to have to provide a JavaScript runtime environment in your JPanel. That shouldn't be impossible given that Java does contain such a thing these days, but the AJAX aspects (where the API wants to connect to the Google servers) might be tricky. Perhaps somebody has done this sort of thing already and posted it on Sourceforge or something.

  • How does autoFS work?

    How does it work? I guess on a more technical aspect. Why do some of my samba shares not show up? Would they show up better if they were afp or ftp or something? And why do they then show up after I go to server and put in the ip address? I've added a share to my login items but I guess that only means when I boot up and not when I login from sleep. I even have an alias to a folder on the share on my desktop hoping that would create the link needed but it doesn't really work. How can I make the Finder/AutoFS scan the network for shares?

    AirPlay is a means to wirelessly stream audio from your iTunes host device to an AirPlay-Ready one. Both the AirPort Express and the Apple TV are examples of AirPlay-Ready devices.
    Please see the following Apple Support article for more information: Using AirPlay

  • How to set Width of a JPanel?

    I am overwirting paintComponent(Graphics g) method to
    draw some g2D in a JPanel.
    I want the drawing processing shown on screen (not flashed out after drawn because it is VERY SLOW).
    So that I CAN NOT use setsize() or setBound() because to change JPanle.width because this is NOT what I wanted(the graphics is shown AFTER is drawn).
    Everytime I call paintComponent(), the width of JPanel is changed.
    My question is : How can change JPanel.width without calling method like setsize() or setBound() .
    Thanks a lot!!

    My code too large, I just show some idea here:
    class caller (){
    myPanel p;
    Graphics2D g2 = (Graphics2D)p.getGraphics();
    //I nee to increase the p.width before call my_paint
    //I CAN NOT use setsize() or setBound()
    //because I want show the drawing process
    //on screen( and that is fast)
    p.my_paint(g2); // how to set p.width ??
    public class myPanel extends JPanel
    public void my_paint(Graphics2D g2)
    //Do my drawing here ,
    // I need to draw thousands of Line2D.
    public void paintComponent(Graphics g){
    Graphics2D g2 = (Graphics2D)g;
    my_paint(g2);

  • How to use JEditorPane in Jtable

    Hi
    I'm trying to put HyperLinks in JTable but i don't not how to add Hand Cursor when mouse is over a hyperLink and how to active it when click it.
    Thanks
    DefaultTableModel tableModel = new DefaultTableModel(new String[][]{{"<a href=www.google.com>Google</a>, <a href=www.yahoo.com>Yahoo</a>"}}, new String[]{"URL"});
    JTable table = new JTable(tableModel);
    table.setRowHeight(24);
    table.setDefaultRenderer(Object.class, new EditorPaneTableCellRenderer());
    class EditorPaneTableCellRenderer extends JEditorPane implements TableCellRenderer
    EditorPaneTableCellRenderer()
    setContentType("text/html");
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    String link = String.valueOf(value);
    setText(link);
    setBackground(isSelected? table.getSelectionBackground() : table.getBackground());
    return this;
    }

    Multi-post: http://forum.java.sun.com/thread.jspa?threadID=5273661&tstart=0

  • How to Get object that a JPanel belongs to.

    I created an object that has a JPanel. When that JPanel is clicked, how can I get the object to which its a GUI data member of?

    Hi,
    If you try JPanel.getParent() it will return the Container that it is in.
    If what you want is the creator you can derive from JPanel and add a constructor that has the creator as its only parm.
      JPanel panel = new JPanel(this);
    public class MyPanel extends JPanel {
      Object myCreator;
      public JPanel(Object creator) {
         super();
         myCreator = creator;
    }Now you can use myCreator to access that class that created it.
    Paul

  • How to set height of a JPanel ?

    Hi.
    My JPanel is a tad too high and I'd rather not use
    mypanel.setPreferredSize(int xxx, int yyy)
    because the breadth is alright.
    How do I play around with the height only?
    Something like
    myjpanel.setHeight(int yyy);
    would be nice but the method deoesn't exist.
    :-)

    Set the minimum size and maximum size as well, to force it to work no matter what the layout manager is. You can set the minimum width to something like 1 and the maximum to 2000 to ensure it works properly if the window is resized, but make sure the minimum height is the height you want. The dimensions are in pixels by the way.

  • How to use backing store in JPanel?

    Below is a sketch of a class that extends JPanel. If I remove the variable "draw", the class works fine, except that the graphics drawing code is executed every time the parent window is fronted, resized, etc.
    I introduced the variable "draw" hoping that the call super.paintComponent(g) would use the backing store to refresh the display. Instead, the call simply erases the panel.
    How can I use the backing store?
    Thanks, for your help.
    public class DisplayPanel extends JPanel {
         boolean draw = true;
         public void paintComponent(Graphics g) {
             super.paintComponent(g);
             if (!draw)
                   return;
              // graphics drawing code here.
             draw = false;
         }

    Backing stores are only used on JViewpots. You will need to draw each time.

  • ANuB figures out how == overlap transparent gif in a JPanel!

    I have to say I have a hate/love realtionship with this forum. Sorry if I ofend but I know it's hard sometimes to get an answer for help as it is purely voluntary. But I've asked help for this a bazillion times and help has been min to null, but I want to say I'm hoping to post something to help someone how to figure out how to Overlap a transparent image or transparent gif over another in a custom JPanel and display it when an action is called. (action called code to follow soon in another forum)
    import java.awt.*;                                                                                          //     import extension packages
    import javax.swing.*;
    * <strong>TransparentImageExample</strong> -- drawing a transparent
    * image on top of a background image.
    public class PizzaImage extends JPanel
         private Icon     icnPizza,
                             icnLoaded,
                             icnSausage,
                             icnPepp,
                             icnOnion,
                             icnGrPep,
                             icnMush,
                             icnOlive,
                             icnGarlic,
                             icnPineApp,
                             icnSpinach;
         private Color bgColor;
         public void paintComponent( Graphics g )
              super.paintComponent( g );
              bgColor = new Color( 255, 235, 160 );
              this.setBackground( bgColor );
              icnPizza = new ImageIcon( "pzaThin.gif" );
              icnLoaded = new ImageIcon( "icnLoaded.gif" );
              icnSausage = new ImageIcon( "topSausage.gif" );
              icnPepp = new ImageIcon( "topPepp.gif" );
              icnOnion = new ImageIcon( "topOnion.gif" );
              icnGrPep = new ImageIcon( "topGrPep.gif" );
              icnMush = new ImageIcon( "topMush.gif" );
              icnOlive = new ImageIcon( "topOlives.gif" );
              icnGarlic = new ImageIcon( "topGarlic.gif" );
              //icnPineApp = new ImageIcon( "topPineApp.gif" );
              //icnSpinach = new ImageIcon( "icnLoaded.gif" );
              icnPizza.paintIcon( this, g, 0, 0 );
              icnSausage.paintIcon( this, g, 0, 0 );
              icnPepp.paintIcon( this, g, 0, 0 );
              icnOnion.paintIcon( this, g, 0, 0 );
              icnGrPep.paintIcon( this, g, 0, 0 );
              icnMush.paintIcon( this, g, 0, 0 );
              icnOlive.paintIcon( this, g, 0, 0 );
              icnGarlic.paintIcon( this, g, 0, 0 );
         public void draw()
              repaint();
    so just import the above code in a class fime which extends JPanel and display the PizzaImage objVariable  = new PizzaImage() in the component of your choice.
    [\code]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

    You guys are right about setting the background color outside of the paint method. I was more pre-occupied with trying to overlap the images. So now I have it set. NOW I need to draw each item when it is selected with a JCheckBox and I can't think of a good way to do that. Is there a way to undraw an image? I suppose I've gone too long w/o sleep. Here's what I have.
    // this code is in another class file
    private class chkBoxHandler implements ItemListener
              public void itemStateChanged( ItemEvent e )
                   for( int i = 0; i < chkBoxName.length; i++ )
                        if( e.getSource() == toppingChk[ i ] )     
                             if( e.getStateChange() == ItemEvent.SELECTED )
                                  pzaImage.setImage( i );
                                  pzaImage.draw();
                             if( e.getStateChange() != ItemEvent.SELECTED )
                                  JOptionPane.showMessageDialog( null, "Button Unselected",     //     displays a message
                                       "About MyPizzeria", JOptionPane.PLAIN_MESSAGE );          //     code to "undraw" the image will go here
    import java.awt.*;     
    import javax.swing.*;
    public class PizzaImage extends JPanel
         int     thisImgNum = -1;
         Icon pzaThin, icnLoaded;
         public String imgName[] = {     "topSausage.gif",
                                            "topPepp.gif",
                                            "topOnion.gif",
                                            "topGrPep.gif",
                                            "topMush.gif",
                                            "topOlives.gif",
                                            "topGarlic.gif",
                                            "topPine.gif",
                                            "topSpinach.gif"
         public Icon topping[] = {     new ImageIcon ( imgName[ 0 ] ),
                                            new ImageIcon ( imgName[ 1 ] ),
                                            new ImageIcon ( imgName[ 2 ] ),
                                            new ImageIcon ( imgName[ 3 ] ),
                                            new ImageIcon ( imgName[ 4 ] ),
                                            new ImageIcon ( imgName[ 5 ] ),
                                            new ImageIcon ( imgName[ 6 ] ),
                                            new ImageIcon ( imgName[ 7 ] ),
                                            new ImageIcon ( imgName[ 8 ] )
         private Color bgColor;
         public PizzaImage()
              setBackground(new Color(255, 235, 160));                         
              pzaThin = new ImageIcon( "pzaThin.gif" );                         
              icnLoaded = new ImageIcon( "icnLoaded.gif" );                    
         public void setImage( int img )                                             
              thisImgNum = img;                                                       
         public void paintComponent( Graphics g )                              
              super.paintComponent( g );
              pzaThin.paintIcon( this, g, 0, 0 );                                   
              if( thisImgNum == 0 )
                   topping[ 0 ].paintIcon( this, g, 0, 0 );
              if( thisImgNum == 1 )
                   topping[ 1 ].paintIcon( this, g, 0, 0 );
              if( thisImgNum == 2 )
                   topping[ 2 ].paintIcon( this, g, 0, 0 );
              if( thisImgNum == 3 )
                   topping[ 3 ].paintIcon( this, g, 0, 0 );
              if( thisImgNum == 4 )
                   topping[ 4 ].paintIcon( this, g, 0, 0 );
              if( thisImgNum == 5 )
                   topping[ 5 ].paintIcon( this, g, 0, 0 );
              if( thisImgNum == 6 )
                   topping[ 6 ].paintIcon( this, g, 0, 0 );
              if( thisImgNum == 7 )
                   topping[ 7 ].paintIcon( this, g, 0, 0 );
              if( thisImgNum == 8 )
                   topping[ 8 ].paintIcon( this, g, 0, 0 );
         public void draw()
              repaint();
    }

Maybe you are looking for

  • Re: Finder and many more won't open?

    Hey, So about a month ago, I had a problem with my MB Pro (mid-2009) where it wouldn't boot. Took it up to the apple store and they wiped it and re-installed Lion. A few weeks later and I tried to uninstall the Messages beta and was prompted to resta

  • Ipad disabled. How can I unlock without restoring on Itunes? Need the photos

    My daughter disabled my Ipad. Now it says to connect to Itunes and does not even allow me to put in a code. I have many photos and videos saved on my Ipad I wish not to erase. Any way to unlock the ipad without having to restore it through itunes? Or

  • Mapping: Problem with Namespaces while testing

    Hello, can anyone tell me what's the thing with the namespace in xml-messages for testing? My problem is the following: I have exported a test-xml-file from a mapping in another namespace (where everything is correct) and now i want to import this xm

  • Access Long Integer

    I need to query a Long Integer from my app. I have tried using int and long but with no luck. I get a type mismatch every time it tries to execute. This does not work                     StringTokenizer st = new StringTokenizer(results.getSelectedVal

  • How to eliminate SQL command in Spool output?

    Hi, I'm trying to Spool an output to a txtfile without the SQL command being at the top of the page. I've tried a number of ways, but I'm having no luck.Any Ideas? Sorry for the long msg, but maybe if some saw what I'm trying to do it would be helpfu