This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

A question about JScrollPane. Thanks in advance!  RSS feed

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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!!
 
What could go wrong in a swell place like "The Evil Eye"? Or with this tiny ad?
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!