Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

scroll bars in swing

Posted on 2003-12-03
2
Medium Priority
?
784 Views
Last Modified: 2013-11-23
I am making a swing program in Java. I am instantiating a DemoFrame (inherited from JFrame) in my main class. In this DemoFrame class, I am instantiating a JScrollPane class. The JScrollPane adds a DemoPanel class (inherited off JPanel) which is where the drawing takes place. Then the DemoFrame adds the JScrollPane. I have the view policy set to always view for the scrollbars. The drawing that goes on in DemoPanel is way larger than the window which holds it. However, the problem is that the scroll bars appear but they aren't scrollable. i.e. the program doesnt think that the scrollbars are necessary but displays them because of the always policy. Here's my code:


import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.io.*;

class DemoFrame extends JFrame
{
  private DemoPanel panel;
  private int height, width;
  private Vector path;

  public DemoFrame(Hashtable h, int height, int width, Vector path)
  {
    super("window");
    final int DEFAULT_FRAME_WIDTH = 300;
    final int DEFAULT_FRAME_HEIGHT = 300;
    this.height = height;
    this.width = width;
    this.path = path;

    // set the size of the frame window
    setSize(DEFAULT_FRAME_WIDTH, DEFAULT_FRAME_HEIGHT);

    // create and install a "listener object"
    // (event handler) to listen for window events
    WindowCloser listener = new WindowCloser();
    addWindowListener(listener);

    // create a panel object and install it in
    // the "content pane"
    panel = new DemoPanel(h, height, width, path);

    JScrollPane js = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    js.setVisible(true);
    js.setOpaque(true);
    js.setWheelScrollingEnabled(true);

    Container contentPane = getContentPane();
    contentPane.add(js, "Center");

    this.setVisible(true);
  }
}

********************************************************

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.io.*;

class DemoPanel extends JPanel
{
  private Hashtable h;
  private int cellwidth = 10;
  private int cellheight = 10;
  private int height, width;
  private int ULx = 50, ULy = 50;
  private Vector path;

  public DemoPanel(Hashtable h, int height, int width, Vector path)
  {
    this.h = h;
    this.setVisible(true);
    this.setSize(900, 900);
    this.height = height;
    this.width = width;
    this.path = path;
  }

  public void paint(Graphics g)
  {
       //paint method
      //draws really really big picture
  }
}

What should I do?

lj8866



0
Comment
Question by:lj8866
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Accepted Solution

by:
Tols earned 500 total points
ID: 9865497
Do not override 'paint' method, but 'paintComponent':

protected void paintComponent(Graphics g)
{
    super.paintComponent(g);
    //draw code
}
0
 
LVL 2

Expert Comment

by:timbauer
ID: 9866044
Tols is right about overriding paintComponent instead.

However, I think your problem might be in the preferred size of your component. Try calling,
setPreferredSize( new Dimension(900,900) );

I think a JScrollPane decides how much space to give your component based on getPreferredSize

getPreferredSize for any JComponent will fetch your saved dimension (900,900). Generally, if a paint delegate decides how much room is needed for getPreferredSize(). The delegate for JPanel computes this based on its layout and children. Since your Demo component has no children its preferred size is computed as (0,0).

Hope this helps.
- Tim
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question