Draw lines on jpanel

11,001

You can simply override paintComponent(Graphics g){} for ff and draw your within that method.

i.e.

JPanel ff = new JPanel(){ 
    public void paintComponent(Graphics g){
        // Draw what you want to appear on your JPanel here.
        // g.drawLine(blah blah blah), etc.
    }
};

In which case you have no need for this...

ff.add(new JComponent() {
    ...
});

You don't need this generic component unless you want to implement the custom component as suggest in the link you provided. In the case that you do want to create such a custom component, then you don't need ff, since a JFrame is already a container that can hold your component.

Share:
11,001
user1949713
Author by

user1949713

Updated on June 04, 2022

Comments

  • user1949713
    user1949713 almost 2 years

    I want to make it like Draw a ruler (line with tick marks at 90 degree angle) just not on jframe but on jpanel.

    So I tried:

    JFrame f = new JFrame();
    JPanel ff = new JPanel();
    
    ff.add(new JComponent() {
    ...
    });
    
    f.add(ff);
    ...
    

    but I failed. :( How to?