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);
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
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.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.