The Artima Developer Community
Sponsored Link

Java Answers Forum
toolbars & scrolling

2 replies on 1 page. Most recent reply: Apr 5, 2002 4:03 AM by Thomas SMETS

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
grace s

Posts: 2
Nickname: gracie
Registered: Mar, 2002

toolbars & scrolling Posted: Mar 13, 2002 6:14 AM
Reply to this message Reply
Advertisement
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class Toolbar extends JFrame{
ShapesPanel shapespanel;

public Toolbar() {
super ("test");
JFrame f = new JFrame("ShapesDemo");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setSize(400, 300);

// create the toolbar
JToolBar toolBar = new JToolBar();
addButtons(toolBar);

shapespanel = new ShapesPanel();

JScrollPane scrollPane = new JScrollPane(shapespanel);

//Lay out content pane
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
contentPane.setPreferredSize(new Dimension(200, 200));
contentPane.add(toolBar, BorderLayout.NORTH);
contentPane.add(scrollPane, BorderLayout.CENTER);
f.setContentPane(contentPane);
f.setVisible(true);

}
protected void addButtons(JToolBar toolBar) {
JButton button = null;

//first button
button = new JButton (new ImageIcon("but1.gif"));
toolBar.add(button);

}

public static void main(String[] args) {
Toolbar sd = new Toolbar();
}


class ShapesPanel extends Canvas {
final Color bg = Color.white;
final Color fg = Color.red;

public ShapesPanel() {
setBackground(bg);
setForeground(fg);
}

public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
draw(g2D);
}

public void draw(Graphics2D g2D) {
Line2D line1 = new Line2D.Float(50, 150, 250, 150);
g2D.draw(line1);

}

}

}

Hi,
I am trying to create an interface with scrollbars and a toolbar. But the problem is that my scrollbars are not getting displayed properly. I want to be able to scroll when all of what is been drawn in ?ShapesPanel? is not visible. Presently the scrollbars are only appearing when I resize (enlarge) the interface. Another problem is that when I use the vertical scrollbar, the toolbar and the horizontal scrollbar also move up and down.

What do I need to edit in the above code to overcome these problems?

Any help would be really appreciated.
Thanks in advance.
Thanks in advance


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: toolbars & scrolling Posted: Mar 13, 2002 2:10 PM
Reply to this message Reply
Change this line from
JScrollPane scrollPane = new JScrollPane(shapespanel);

to

JScrollPane scrollPane = new JScrollPane(shapespanel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
if you want vertical and horizontal scroll bars in your scrollpane

there are three options for vertical and horizontal scrollbars:

JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED
JScrollPane.VERTICAL_SCROLLBAR_NEVER
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS

JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS
so change them to what you need.

I am seeing the shapespanel when running on my machine.
I added a line in the:

public void draw(Graphics2D g2D) {

method to be sure:

g2D.drawString("Test Test Test", 10,10);
and I see the string and the line drawn just fine.

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: toolbars & scrolling Posted: Apr 5, 2002 4:03 AM
Reply to this message Reply
Should help :
http://www.cs.cf.ac.uk/Dave/HCI/HCI_Handout_CALLER/node85.html

Rgds,

thomas,

Flat View: This topic has 2 replies on 1 page
Topic: java + win2000 Previous Topic   Next Topic Topic: converting the extensions

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use