Results 1 to 5 of 5
  1. #1
    phil128 is offline Member
    Join Date
    Sep 2008
    Posts
    40
    Rep Power
    0

    Default Adding jscrollpane when extending jpanel

    Hi.

    I'm working within a class where I'm extending from jpanel. I'm trying to add a jscrollpane to from.

    Is there a way I can call the current panel from when I'm extending from jpanel, so I can add a jscrollpane to it?

    thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by phil128 View Post
    I'm working within a class where I'm extending from jpanel. I'm trying to add a jscrollpane to from.

    Is there a way I can call the current panel from when I'm extending from jpanel, so I can add a jscrollpane to it?
    The current JPanel is the current object of the class or "this". If you're trying to place the current JPanel in a JScrollPane, then you have to do just that, place it in a JScrollPane. If you're still having problems, you will want to provide more information, at least enough detail so that your question is answerable.

  3. #3
    phil128 is offline Member
    Join Date
    Sep 2008
    Posts
    40
    Rep Power
    0

    Default

    Java Code:
    public class MainWindow {
    	MainWindow(){
    		JFrame rF = new JFrame("Sequencer"); //Root frame
    		JPanel rP = new JPanel(); //Root panel
    		rP.setLayout(new BorderLayout());
    		rF.getContentPane().add(rP);
    		rP.add(new GridPanel(),BorderLayout.SOUTH);
    		rF.setSize(800, 400);
    		rF.setVisible(true);
    		rF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    	public static void main(String[] args){
    		new MainWindow();
    	}
    }
    Java Code:
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    
    
    @SuppressWarnings("serial")
    public class GridPanel extends JPanel{
    	GridPanel(){
    		JScrollPane hSPane = new JScrollPane(this);
    		for(int i=0; i<200; i++){
    			super.add(new JLabel("CENTRE"));
    		}
    		hSPane.add(this);
    	}
    }
    I'm thinking that I need to pass the current panel to JScrollPane(), instead of "this".

    Really buggin me how.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Why must you try to add the JScrollPane inside the GridPanel class? Why not just simply add your GridPanel to a JScrollPane?

    Java Code:
    public class MainWindow {
    	MainWindow(){
    		JFrame rF = new JFrame("Sequencer"); //Root frame
    		JPanel rP = new JPanel(); //Root panel
    		rP.setLayout(new BorderLayout());
    		rF.getContentPane().add(rP);
    		rP.add(new JScrollPane(new GridPanel()),BorderLayout.SOUTH); // *** changed ***
    		rF.setSize(800, 400);
    		rF.setVisible(true);
    		rF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    	public static void main(String[] args){
    		new MainWindow();
    	}
    }

    Another issue is why are you subclassing JPanel, or extending by inheritance rather than extending by composition? Does GridPanel override any JPanel methods such as paintComponent?

  5. #5
    phil128 is offline Member
    Join Date
    Sep 2008
    Posts
    40
    Rep Power
    0

    Default

    Ahhhh I see.. Excellent works now. I'm going to using a different set of custom colours within this frame you see.

    Hopefully thats the correct way of going it. I understand how bad it is to extend in java.
    Last edited by phil128; 01-19-2011 at 12:48 AM.

Similar Threads

  1. Replies: 1
    Last Post: 12-17-2010, 10:24 AM
  2. Using Jscrollpane to move images on jpanel
    By Lazairus in forum New To Java
    Replies: 0
    Last Post: 12-06-2010, 03:46 PM
  3. JPanel/JScrollPane issue
    By Moncleared in forum New To Java
    Replies: 1
    Last Post: 09-23-2010, 05:02 AM
  4. Possible to do JPanel drawing without extending?
    By SM2010 in forum AWT / Swing
    Replies: 7
    Last Post: 07-03-2010, 12:30 AM
  5. Replies: 1
    Last Post: 05-09-2010, 07:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •