Results 1 to 5 of 5
Thread: Problem with JScrollPane
- 03-29-2011, 11:18 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
Problem with JScrollPane
so im making this program that displays circles connected with lines, but i have a problem with the JScrollPane, the scrollbars arent working. Im creatin a Jpanel pBtn where i put 6 buttons that will decide which circles and lines are supposed to be drawn, the other JPanel, pDraw, is the panel where im drawing the circles and lines, this panel is inside a JScrollPane, so in case the image is too large it would allow to scroll left-right and up-down to view the full image, but when i tested it (i tested it drawing an oval with a size of width=500 and height=750) the scrollbars didnt work, here i put my code:
so if anyone can help me see my error i would be so glad, the complete program will be drawing bigger structures, im planning to save different structures of circles and lines drawn and then connect them, but that i'll do it later, first i wanna know what's the error im making with the scrollbars.Java Code:import javax.swing.*; import javax.swing.event.MouseInputAdapter; import java.awt.*; import java.awt.event.*; import java.util.*; public class AFNGUI extends JFrame implements ActionListener{ JButton btns[]; JPanel pBtn, pDraw; JScrollPane sDraw; public AFNGUI(){ getContentPane().setLayout(null); setVisible(true); setSize(650,550); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); btns=new JButton[6]; pBtn=new JPanel(); pBtn.setLayout(null); pBtn.setSize(600,50); pBtn.setAlignmentY(Component.TOP_ALIGNMENT); for(int i=0;i<6;i++){ btns[i]=new JButton(new ImageIcon(i + ".PNG")); btns[i].setName("" + i ); btns[i].setBounds(100*(i),0,50,50); btns[i].setVisible(true); pBtn.add(btns[i]); } btns[0].addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {boton1(e);} }); pBtn.setBorder(BorderFactory.createLineBorder(Color.black)); getContentPane().add(pBtn); pDraw=new JPanel(); pDraw.setLayout(null); pDraw.setBackground(Color.white); pDraw.setSize(1000,1000); sDraw=new JScrollPane(pDraw,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); sDraw.setBounds(0,50,600,450); getContentPane().add(sDraw); repaint(); } public void boton1(ActionEvent e){ int w=500,h=750; //pDraw.setSize(w,h); Graphics g=pDraw.getGraphics(); g.setColor(Color.blue); g.drawOval(100,150,w,h); } public void actionPerformed(ActionEvent e){} public static void main(String args[]){ AFNGUI miafn=new AFNGUI(); } }
thanksLast edited by loony; 03-29-2011 at 11:21 PM. Reason: made a mistake in the code
-
Don't get your Graphics object by calling getGraphics on a JPanel or other component. Instead do it as the Swing tutorials suggest: in the paintComponent override method using the Graphics object that is passed in as a parameter to this method.
Please start here
-
Also, you want to avoid using null layout and setting the absolute size and position of your components, but rather using appropriate layout managers and preferredSizes of your components. This is the main reason that your scrollbars aren't working.
- 03-30-2011, 01:30 AM #4
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
how do i override the method? can you give me an example?
-
Similar Threads
-
Problem with JScrollPane and VK_TAB
By hitesh in forum AWT / SwingReplies: 3Last Post: 09-10-2010, 12:27 PM -
JScrollPane problem
By KArelVH in forum AWT / SwingReplies: 6Last Post: 04-27-2009, 09:40 PM -
problem with Jscrollpane
By ravrajesh.ap in forum AWT / SwingReplies: 5Last Post: 01-03-2009, 10:38 PM -
JScrollPane updation Problem
By goodwillwins in forum AWT / SwingReplies: 22Last Post: 09-28-2008, 09:11 AM -
jscrollpane problem
By monkey04 in forum AWT / SwingReplies: 2Last Post: 01-19-2008, 05:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks