Michal
Michal

Reputation: 65

Scrolls not appears

I want add JScrolpane to JPanel, but that not appears. In JLabel works fine and its very easy. I am using JPanel beacuse I'll add some image proccessing stuff to my program. There is my code:

public void draw(){  
 panel=new JPanel(){
        protected void paintComponent(Graphics g){
            Graphics g2 = g.create();
            g2.drawImage(image, 0, 0, this);
            g2.dispose();             
        }         
 };   
 panel.setBorder(BorderFactory.createEtchedBorder());
 panel.setPreferredSize(new Dimension(400, 330));
 s=new JScrollPane(panel);
 s.setPreferredSize(new Dimension(400,285));
 this.getContentPane().add(s,BorderLayout.CENTER);
 add(panel);
 revalidate();  
 repaint();     
 }

Upvotes: 1

Views: 102

Answers (3)

Michal
Michal

Reputation: 65

I got it! The main reason for me was no-layout manager and no-declarated size of this. There is my code:

public void draw(){
 panel=new JPanel(new BorderLayout()){
 @Override
        protected void paintComponent(Graphics g){
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.drawImage(img, 0, 0, null);
        }         
 };   
 panel.setBounds(0, 0, img.getWidth(), img.getHeight());
 panel.setPreferredSize(new Dimension(img.getWidth(),obraz.getHeight()));
 JScrollPane scrolls=new JScrollPane(panel,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

 add(scrolls);
 validate();  
 repaint();     
 }

But if someone will want copy this code in the future, there is one thing to be known- a size of Jpanel isn't refreshing (it's always one size of first opened image), but I have scrolsl and I am satisfied for now ;). Thanks a lot for everyone.

Upvotes: 0

David Kroukamp
David Kroukamp

Reputation: 36423

You dont honor the paint chain, call super.paintComponent(g) as first call in overridden paintComponent method. i.e

@Override
protected void paintComponent(Graphics g){ 
      super.paintComponent(g); 
      //draw here
}

or visual artifacts may occur.

  • Also note the @Override annotation which should be used with overridden methods in order to gain the advantage of compiler checking that we overrode the method correctly.

  • There is no need for getContentPane().add(..) simply call add on JFrame instance as add(..) along with remove(..) and setLayout(..) have been forward to the JFrames contentPane

  • Also not a good idea to go extending JFrame for nor reason, simply create an instance and use that i.e:

    JFrame frame=new JFrame("Title here");
    ...
    frame.add(..);
    ...
    frame.pack();
    frame.visible(true);
    
  • Also draw onto the Graphics object passed into paintComponent g dont go creating your own in paintComponent. Unless of course you're doing if for the reason @HFOE mentioned in below comment :).

  • No need for this parameter in drawImage(..) unless your JPanel implements on ImageObserver or the image may not be fully loaded when painting occurs. Simply use null.

  • And just for the cherry on top use some Graphics2D and RenderHints as seen here. This will allow for a better quality image to be drawn and text.

Upvotes: 3

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

 s=new JScrollPane(panel);
 s.setPreferredSize(new Dimension(400,285));
 this.getContentPane().add(s,BorderLayout.CENTER);
 add(panel); // ****** ????????????
 revalidate();  
 repaint();     
}

You're adding the JPanel to the GUI not the JScrollPane, so you really shouldn't expect to see any scrollpanes if they've not been added anywhere.

Solution: Add your JScrollPane, s, that holds the JPanel to the GUI, not JPanel itself.

Upvotes: 3

Related Questions