Results 1 to 8 of 8
  1. #1
    trishtren is offline Member
    Join Date
    Feb 2011
    Posts
    53
    Rep Power
    0

    Smile Adding a jpanel to a customized Jpanel Class

    Hey, i have been trying to create a custom gui using awt and java2d. Its all been going well except that i cannot seem to add a regular jscrollpane or even a jpanel to my already customized jpanel.

    Below is the method im trying to use which works but performs erratic results:
    JScrollPane scrollpane;
    JPanel p = new JPanel();
    scrollpane = new JScrollPane(p);
    this.add(scrollpane, BorderLayout.CENTER);

    Im using this code within my customized Jpanel class file :

    @Override public void paintComponent(Graphics g)
    { //parent paints the background
    super.paintComponents(g);
    }

    This changes the appearance, but the code does work but it causes odd behaviour such as the panel appearing in the top left corner of my window instead of inside the jpanel it is added. Any advice would be great :)

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default

    Are you sure that "this" is using a BorderLayout?

    And why are you calling super.paintComponents() from paintComponent()?

    Also, are you sure you're using AWT and not Swing?

    If you want further help with code, you'll have to provide an SSCCE. Otherwise we're just guessing.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.

    db

  4. #4
    trishtren is offline Member
    Join Date
    Feb 2011
    Posts
    53
    Rep Power
    0

    Default

    ok thanks, im using an example class i got on the internet to override the awt gui. It uses both awt and swing. Ill provide the class code below:


    import java.awt.*;
    import javax.swing.*;
    public class CustomPanel extends JPanel
    {
    private static final long serialVersionUID = 1L;
    JScrollPane scrollpane;
    JPanel p = new JPanel();
    scrollpane = new JScrollPane(p);

    public CustomPanel()
    {
    setPreferredSize(new Dimension(130,100));
    this.add(scrollpane, BorderLayout.CENTER);
    }

    @Override public void paintComponent(Graphics g)
    { //parent paints the background
    super.paintComponents(g);
    Color background = new Color(220, 220, 220);
    Color toolbackground = new Color(220, 220, 220);

    int w = getWidth();
    int h = getHeight();

    //draw tool bar panel
    g.setColor (background);
    g.fillRoundRect(0, 0, w-1, h-5, 2, 2);

    g.setColor (Color.LIGHT_GRAY);
    g.drawRoundRect(0, 0, w-1, h-5, 2, 2);

    //draw the toolbar top
    g.setColor (toolbackground);
    g.fillRoundRect(0, 0, w-1, 26, 2, 2);
    g.setColor (Color.LIGHT_GRAY);
    g.drawRoundRect(0, 0, w-1, 26, 2, 2);
    }}

    What im looking for is a way to add a new jpanel into this custom jpanel class and have it behave normally would be good too lol. Thanks for taking a look :)
    Last edited by trishtren; 04-05-2011 at 07:25 PM.

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default

    Well, trying to hammer away at random code you found online, without actually understanding it, is a pretty dangerous way to program.

    Like I already asked you: are you sure your custom JPanel is using a BorderLayout? And why are you calling super.paintComponents() from paintComponent()? And are you sure you're using AWT components?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    trishtren is offline Member
    Join Date
    Feb 2011
    Posts
    53
    Rep Power
    0

    Default

    actually im not sure about the border layout, iv not implicitly stated any border layout in my main window frame.

    Im adding it to the main window frame like so :

    JPanel content = new JPanel();
    content.setLayout(new BorderLayout(5,5));
    content.add(CustomPanel, BorderLayout.EAST);
    setContentPane(content);

    and all this is happening in my main function. As for the super.paint components the example states in a comment that " super.paintComponent(g); // ask parent to paint background " to be honest im not 100% sure why its needed as im new to java 2d and still trying to grasp a lot of differences between java and c++ as well. But other than the border layout i have applied above in my main window i have not applied any specific border layouts.

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default

    Okay, you haven't specified a layout (which means the JPanel is using a FlowLayout), but you're trying to use a BorderLayout in this line: this.add(scrollpane, BorderLayout.CENTER);

    paintComponents() != paintComponent()
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    trishtren is offline Member
    Join Date
    Feb 2011
    Posts
    53
    Rep Power
    0

    Default

    holy chocolate star fish batman thanks to your help its working, the funny thing is it mainly seemed to be the silly paintcomponent"s" typo that caused the whole problem but either way iv learned a few things trying to solve this. Thanks for all the help, probably one of the fastest replying forums iv been on.
    Last edited by trishtren; 04-05-2011 at 08:15 PM.

Similar Threads

  1. Adding Jpanel ontop of another Jpanel
    By Manfizy in forum AWT / Swing
    Replies: 4
    Last Post: 03-05-2011, 11:34 PM
  2. adding integer to the JPanel
    By navid in forum New To Java
    Replies: 2
    Last Post: 12-18-2010, 05:26 PM
  3. adding a buffered image over a jpanel
    By fatmat9 in forum New To Java
    Replies: 1
    Last Post: 11-23-2010, 04:23 AM
  4. Having difficulty adding graphics to a JPanel
    By DrKilljoy in forum New To Java
    Replies: 20
    Last Post: 07-20-2010, 09:40 PM
  5. adding a jpanel in the middle of the script
    By 2o2 in forum AWT / Swing
    Replies: 11
    Last Post: 10-12-2008, 06:50 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
  •