• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

A question about JScrollPane. Thanks in advance!

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I am new to the GUI/swing, so maybe you think the question is too simple, but please give some words if you know the answer. Thanks!!
What I am going to do is draw a big size custom Graphics2D in a JPanel, and then put the JPanel into a JFame. But the size of the Graphics2D/JPanel is too big, So must be scrollable. I tried to put the JPanel into a JScrollPane, then add the JScrollPane to the JFrame, the Graghics2D is there, but it just can't be scrollable. Shall I use other class?
I also tried to use awt.Canvas, but there is still problems. ON windows, no matter how small I resize the JFrame's size. The scroll bar just can't be showed, but it can be scrolled vetically by rolling the mouse(I am using JDK1.4.1, and I guess the Canvas blocks it, but I can't find how to make the Canvas not Opaque)
Then I tried on linux, This time, the scrollbar can be showed, but the Canvas can still block the scrollbar. The scrollbar can only be showed when the view moves to the right-bottom conner. Otherwise, the Canvas will block the scrollbar.
I read a book said that all the JComponent in the JScrollPane can be scrollable, but how can I do that?
Any information or suggestions will be highly appreciated!!
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
set the setPreferredSize of the JPanel and add it to the scrollpane and then add the scroll pane to the JFrame,
u will get the scrollbar here is a code example which u may look at

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TestRepaint extends JFrame
{
public TestRepaint()
{
super("layered pane");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JScrollPane scrollPane = new JScrollPane(new MyPanel());
Container contentPane = this.getContentPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
setSize(new Dimension(800,600));
this.setVisible(true);
}


class MyPanel extends JPanel
{
public MyPanel()
{
setPreferredSize(new Dimension(2000,600) );
this.setBackground(Color.white);
this.setOpaque(true);

}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
System.out.println ("Painting Back " );
g.setColor(Color.blue);
int x = 0;
for(int i = 0; i < 60; i++)
{
x +=60;
g.drawLine(x, 0, x, 600);

}

}
}

public static void main(String args[])
{
new TestRepaint();

}

}
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure on it, but maybe you will need to put the scrollbar policy to always.
 
Joe J. Wang
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,
It is great!! ashish kulkarni just got me, yeah, I need to read about the whole chapter for JComponent. In my program, I used JPanel.setSize() instead of setPreferredSize(). Actually, the setSize is from java.awt.Component. Damn AWT!
Thank you VERY MUCH for replying my post!!
 
It will give me the powers of the gods. Not bad for a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic