Results 1 to 7 of 7
  1. #1
    KArelVH is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default JScrollPane problem

    Ok, I have been googling this for hours now and I haven't found a solution. Help needed.

    What I basically want is this:

    -I have a JPanel, with on that JPanel 3 other JPanels using a GridLayout(1,3), with each a preferredSize of 300,600.
    - On the left one of those panels I want a text and an ImageIcon. That text can be short or long and this is where the problem begins.

    When the text is too long to fit in the foreseen 300,600 space I want scrollbars, but only vertical ones. This means the text can't be any wider than 300 unless I manually edited the window's size. The left panel is using a boxLayout. I put the content on the left panel like this:

    Java Code:
    public void addProfile(){
    
            String text = model.getArtistModel().getDescription();
            text = "<html>" + text + "</html>";
            JLabel profile = new JLabel(text);
            Box box = Box.createHorizontalBox();
            box.add(profile);
            scroll.add(box);
    
        }
    
        public void addImage(){
    
           JLabel image = new JLabel(model.getArtistModel().getImageIcon());
    
           Box box = Box.createHorizontalBox();
           box.add(image);
           scroll.add(box);
    
    );
    
    
        }
    both the base panel (the one where the 3 JPanels are added to) and the Jeft panel are JPanels. I have to fit in a JScrollPane somewhere, but I don't know where. I have tried to do so on a lot of places but it never works the way it should.

    I'd be most grateful if anyone can help me asap.

  2. #2
    KArelVH is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default

    I now tried using setMinimumSize(260,20) and setMaximumSize(260,2000000) but that doesn't seem to work either...
    does anyone know why?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    The layout manager in use likely doesn't respect these min and max. You may get a quicker correct answer if you create and post an SSCCE that shows your problem. You can read up on this process here: Short, Self Contained, Correct Example

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Manually add line breaks to the text yourself. You can us a FontMetrics to determine how wide a line of text will be.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  5. #5
    KArelVH is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default

    OK,

    here are the simplified source codes. I don't really want to use FontMetrics if not absolutely necessary. There should be a better way.

    LeftPanel.java:
    Java Code:
    public class LeftPanel extends JPanel{
    
        private ArtistVensterModelCollection model;
        private JPanel panel;
    
    
        public LeftPanel(ArtistVensterModelCollection model){
    
            this.model = model;
    
            setMinimumSize(new Dimension(260,200));
            setMaximumSize(new Dimension(260,2000000));
            setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
            setBorder(BorderFactory.createTitledBorder(model.getArtistModel().getNaam()));
            addProfile();
            add(Box.createRigidArea(new Dimension(0, 30)));        
            addImage();
    
        }
    
        public void addProfile(){
    
            String text = model.getArtistModel().getDescription();
            text = "<html>" + text + "</html>";
            JLabel profile = new JLabel(text);
            profile.setMinimumSize(new Dimension(250, 300));
            profile.setMaximumSize(new Dimension(250, 20000));        
            Box box = Box.createHorizontalBox();
            box.add(profile);
            add(box);
        }
    
        public void addImage(){
    
           JLabel image = new JLabel(model.getArtistModel().getImageIcon());
           Box box = Box.createHorizontalBox();
           box.add(image); add(box);
    
        }
    PanelBase.java:
    Java Code:
    public class VensterPanelBase extends JPanel{
        
        private ArtistVensterModelCollection model;
    
        public VensterPanelBase(ArtistVensterModelCollection model){
    
            this.model = model;
            setPreferredSize(new Dimension(900,600));
            setLayout(new GridLayout(1, 3));
            makeLeftPanel();
        }
    
        public void makeLeftPanel(){
    
            JScrollPane scroll = new JScrollPane(new LeftPanel(model));
            scroll.setPreferredSize(new Dimension(300, 600));
    
            add(scroll);
    
        }
    
    }
    which gives me a huge horizontal scrollbar in the left panel. The leftPanel itself is huge (i can see that thanks to the border) but the JLabels are added somewhere in the middle. See attachment. Can it be something with margins or something?

    thanks.
    Attached Thumbnails Attached Thumbnails JScrollPane problem-schermafdruk-tiga.jpg  
    Last edited by KArelVH; 04-27-2009 at 07:56 PM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    What if your left panel held the text in a non-editable JTextArea that called setWrapStyleWord and setLineWrap both to true? You could change its background and all to mimic a JLabel if need be.

  7. #7
    KArelVH is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default

    that might be an option, yes. I'll try it asap.
    However, I'd really like to know what is causing this problem and how to fix it without using other components:/

    thanks for your help so far :)

Similar Threads

  1. jscrollpane
    By kaemonsaionji in forum New To Java
    Replies: 3
    Last Post: 02-25-2009, 09:39 AM
  2. problem with Jscrollpane
    By ravrajesh.ap in forum AWT / Swing
    Replies: 5
    Last Post: 01-03-2009, 11:38 PM
  3. JScrollPane updation Problem
    By goodwillwins in forum AWT / Swing
    Replies: 22
    Last Post: 09-28-2008, 10:11 AM
  4. jscrollpane problem
    By monkey04 in forum AWT / Swing
    Replies: 2
    Last Post: 01-19-2008, 06:23 AM
  5. help with JScrollPane
    By tommy in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 08:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •