Jpanel Dashed Border?

Discussion in 'Java' started by Barkster, Jun 27, 2006.

  1. Barkster

    Barkster Guest

    Is there a way to make a border dashed around a panel? I'm sure it
    could be painted somehow but I have no clue on that. Any examples on
    how to do this? Thanks
     
    Barkster, Jun 27, 2006
    #1
    1. Advertising

  2. Barkster wrote:
    > Is there a way to make a border dashed around a panel? I'm sure it
    > could be painted somehow but I have no clue on that. Any examples on
    > how to do this? Thanks

    You could implement your own Border implementation, probably as a subclass
    of AbstractBorder. At least you need to override the getBorderInsets(...)
    and paintBorder(...) methods. Then set that border onto your JPanel by
    setBorder(...).
    See
    http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/border/Border.html
    http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/border/AbstractBorder.html

    --
    Thomas
     
    Thomas Fritsch, Jun 27, 2006
    #2
    1. Advertising

  3. Barkster

    Vova Reznik Guest

    Vova Reznik, Jun 27, 2006
    #3
  4. Barkster

    Barkster Guest

    Thanks, those sound logical but I'm new to this and that is a bit over
    my head, neither actually showed dashed border example. May be in over
    my head on this one.

    Vova Reznik wrote:
    > Barkster wrote:
    > > Is there a way to make a border dashed around a panel? I'm sure it
    > > could be painted somehow but I have no clue on that. Any examples on
    > > how to do this? Thanks
    > >

    >
    > http://java.sun.com/docs/books/tutorial/uiswing/misc/border.html
     
    Barkster, Jun 27, 2006
    #4
  5. Barkster

    Vova Reznik Guest

    Barkster wrote:
    > Thanks, those sound logical but I'm new to this and that is a bit over
    > my head, neither actually showed dashed border example. May be in over
    > my head on this one.
    >
    > Vova Reznik wrote:
    >> Barkster wrote:
    >>> Is there a way to make a border dashed around a panel? I'm sure it
    >>> could be painted somehow but I have no clue on that. Any examples on
    >>> how to do this? Thanks
    >>>

    >> http://java.sun.com/docs/books/tutorial/uiswing/misc/border.html

    >


    class DashBorder implements Border {
    private final Insets insets = new Insets(1, 1, 1, 1);
    private final int length = 5;
    private final int space = 3;
    public boolean isBorderOpaque() {
    return false;
    }
    public void paintBorder(Component c, Graphics g, int x, int y,
    int width, int height) {
    g.setColor(Color.RED);
    // --- draw horizontal ---
    for (int i = 0; i < width; i += length) {
    g.drawLine(i, y, i + length, y);
    g.drawLine(i, height - 1, i + length, height - 1);
    i += space;
    }
    // --- draw vertical ---
    for (int i = 0; i < height; i += length) {
    g.drawLine(0, i, 0, i + length);
    g.drawLine(width - 1, i, width - 1, i + length);
    i += space;
    }
    }
    public Insets getBorderInsets(Component c) {
    return insets;
    }
    }
     
    Vova Reznik, Jun 27, 2006
    #5
  6. > public void paintBorder(Component c, Graphics g, int x, int y,
    > int width, int height) {
    > g.setColor(Color.RED);
    > // --- draw horizontal ---
    > for (int i = 0; i < width; i += length) {
    > g.drawLine(i, y, i + length, y);
    > g.drawLine(i, height - 1, i + length, height - 1);
    > i += space;
    > }
    > // --- draw vertical ---
    > for (int i = 0; i < height; i += length) {
    > g.drawLine(0, i, 0, i + length);
    > g.drawLine(width - 1, i, width - 1, i + length);
    > i += space;
    > }
    > }


    why ignore java2D and existing classes?

    class DashBorder extends LineBorder {

    //make getters and setters for stroke as exercise ;-)
    BasicStroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT,
    BasicStroke.JOIN_BEVEL, 1, new float[]{5, 5}, 0);

    public void paintBorder(Component c, Graphics g, int x, int y, int
    width, int height) {
    Graphics2D g2d = (g2d)g.create();
    g2d.setStroke(stroke);
    super.paintBorder(c, g, x, y, width, height);
    g2d.dispose();
    }
    }

    Andrey
    --
    http://uio.imagero.com Unified I/O for Java
    http://reader.imagero.com Java image reader
    http://jgui.imagero.com Java GUI components and utilities
     
    Andrey Kuznetsov, Jun 27, 2006
    #6
  7. Barkster

    Barkster Guest

    Hmmn, I'll look at those, I got this to work but I want to be able to
    change from dashed back to solid and dashed again using a checkbox. I
    can get it to go dashed but I can't figure out how to apply this class
    again the the panel pn? Here is simplified version of what I'm trying
    to do?



    //declare new dashed panel
    DashedBorderPanel pn = new DashedBorderPanel();

    //watch checkbox
    public void jCheckBox1_actionPerformed(ActionEvent e) {
    if(this.jCheckBox1.isSelected()) {
    pn.setBorder(BorderFactory.createLineBorder(Color.black));
    System.out.println("Checked");
    }else {
    pn.??????
    System.out.println("UnChecked");
    }
    }



    import javax.swing.JPanel;
    import java.awt.LayoutManager;
    import java.awt.Color;
    import java.awt.*;
    import javax.swing.border.*;

    public class DashedBorderPanel extends JPanel {

    class DashedBorder extends LineBorder {

    public DashedBorder(Color p0) {
    super(p0);
    }

    public DashedBorder(Color p0, int p1) {
    super(p0, p1);
    }

    public DashedBorder(Color p0, int p1, boolean p2) {
    super(p0, p1, p2);
    }

    public void paintBorder(Component comp,Graphics g, int x1,int x2,
    int y1,int y2){
    Stroke old=((Graphics2D)g).getStroke();
    BasicStroke bs=new BasicStroke(5.0f, BasicStroke.CAP_BUTT,
    BasicStroke.JOIN_MITER, 10.0f, new
    float[]{8.0f}, 2.0f);
    ((Graphics2D)g).setStroke(bs);
    super.paintBorder(comp,g, x1,x2, y1,y2);
    ((Graphics2D)g).setStroke(old);
    }



    }
    public DashedBorderPanel() {
    DashedBorder db=new DashedBorder(Color.black);
    this.setBorder(db);
    }

    public DashedBorderPanel(LayoutManager p0, boolean p1) {
    super(p0, p1);
    }

    public DashedBorderPanel(LayoutManager p0) {
    super(p0);
    }

    public DashedBorderPanel(boolean p0) {
    super(p0);
    }
    public static void main(String[] args) {
    DashedBorderPanel dashedBorderPanel1 = new DashedBorderPanel();
    }
    }
     
    Barkster, Jun 27, 2006
    #7
  8. Barkster

    Vova Reznik Guest

    Andrey Kuznetsov wrote:
    >> public void paintBorder(Component c, Graphics g, int x, int y,
    >> int width, int height) {
    >> g.setColor(Color.RED);
    >> // --- draw horizontal ---
    >> for (int i = 0; i < width; i += length) {
    >> g.drawLine(i, y, i + length, y);
    >> g.drawLine(i, height - 1, i + length, height - 1);
    >> i += space;
    >> }
    >> // --- draw vertical ---
    >> for (int i = 0; i < height; i += length) {
    >> g.drawLine(0, i, 0, i + length);
    >> g.drawLine(width - 1, i, width - 1, i + length);
    >> i += space;
    >> }
    >> }

    >
    > why ignore java2D and existing classes?
    >
    > class DashBorder extends LineBorder {
    >
    > //make getters and setters for stroke as exercise ;-)
    > BasicStroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT,
    > BasicStroke.JOIN_BEVEL, 1, new float[]{5, 5}, 0);
    >
    > public void paintBorder(Component c, Graphics g, int x, int y, int
    > width, int height) {


    Good idea if:

    > Graphics2D g2d = (g2d)g.create();

    Graphics2D g2d = (Graphics2D)g.create();

    > g2d.setStroke(stroke);
    > super.paintBorder(c, g, x, y, width, height);

    super.paintBorder(c, g2d, x, y, width, height);
    > g2d.dispose();
    > }
    > }
    >
    > Andrey
     
    Vova Reznik, Jun 27, 2006
    #8
  9. Barkster

    Barkster Guest

    Sorry for my ignorance but I having a problem implementing this
    solution. I get an error:
    "LineBorder(java.awt.Color,int,int) in com.sun.javaws.ui.general
    LineBorder cannot be applied to ()" I'm pretty new to programming and
    to java and have quite developed an understanding of implementing the
    classes.

    class DashBorder extends LineBorder {
    //make getters and setters for stroke as exercise ;-)
    BasicStroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT,
    BasicStroke.JOIN_BEVEL, 1, new
    float[]{5, 5}, 0);

    public void paintBorder(Component c, Graphics g, int x, int y, int
    width, int height) {
    Graphics2D g2d = (Graphics2D)g.create();
    g2d.setStroke(stroke);
    super.paintBorder(c, g2d, x, y, width, height);
    g2d.dispose();
    }
    }
     
    Barkster, Jun 27, 2006
    #9
  10. Barkster

    Vova Reznik Guest

    Barkster wrote:
    > Sorry for my ignorance but I having a problem implementing this
    > solution. I get an error:
    > "LineBorder(java.awt.Color,int,int) in com.sun.javaws.ui.general
    > LineBorder cannot be applied to ()" I'm pretty new to programming and
    > to java and have quite developed an understanding of implementing the
    > classes.


    That is why I offered implementation of Border, not LineBorder
    >
    > class DashBorder extends LineBorder {
    > //make getters and setters for stroke as exercise ;-)
    > BasicStroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT,
    > BasicStroke.JOIN_BEVEL, 1, new
    > float[]{5, 5}, 0);


    You may, for example:
    DashBorder (){
    super(Color.RED);

    }

    LineBorder has no empty constructor.
    But it has:
    LineBorder(Color)
    LineBorder(Color, int thickness)
    LineBorder(Color, int thickness, boolean rounded)

    You need to call super constructor when you
    extending a class.


    >
    > public void paintBorder(Component c, Graphics g, int x, int y, int
    > width, int height) {
    > Graphics2D g2d = (Graphics2D)g.create();
    > g2d.setStroke(stroke);
    > super.paintBorder(c, g2d, x, y, width, height);
    > g2d.dispose();
    > }
    > }
    >
     
    Vova Reznik, Jun 27, 2006
    #10
  11. Barkster

    Vova Reznik Guest

    Barkster wrote:
    > Sorry for my ignorance but I having a problem implementing this
    > solution. I get an error:
    > "LineBorder(java.awt.Color,int,int) in com.sun.javaws.ui.general
    > LineBorder cannot be applied to ()" I'm pretty new to programming and
    > to java and have quite developed an understanding of implementing the
    > classes.
    >
    > class DashBorder extends LineBorder {
    > //make getters and setters for stroke as exercise ;-)
    > BasicStroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT,
    > BasicStroke.JOIN_BEVEL, 1, new
    > float[]{5, 5}, 0);
    >


    public DashBorder(){
    super(Color.RED);
    }


    You may read docs yourself.

    > public void paintBorder(Component c, Graphics g, int x, int y, int
    > width, int height) {
    > Graphics2D g2d = (Graphics2D)g.create();
    > g2d.setStroke(stroke);
    > super.paintBorder(c, g2d, x, y, width, height);
    > g2d.dispose();
    > }
    > }
    >
     
    Vova Reznik, Jun 27, 2006
    #11
  12. >> public void paintBorder(Component c, Graphics g, int x, int y, int
    >> width, int height) {

    >
    > Good idea if:
    >
    >> Graphics2D g2d = (g2d)g.create();

    > Graphics2D g2d = (Graphics2D)g.create();


    yes, of course,
    I just tipped it direct in outlook
    and 37.8 fever is not the best condition for progamming ;-)

    Andrey

    --
    http://uio.imagero.com Unified I/O for Java
    http://reader.imagero.com Java image reader
    http://jgui.imagero.com Java GUI components and utilities
     
    Andrey Kuznetsov, Jun 27, 2006
    #12
  13. > Sorry for my ignorance but I having a problem implementing this
    > solution. I get an error:


    this should work and gives you possibility to switch between solid and
    dashed border.
    I hope this is not your homework.

    public class DashBorder extends LineBorder {

    public DashBorder(Color c) {
    this(c, 1);
    }

    public DashBorder(Color c, int thickness) {
    this(c, thickness, new float[] {5, 5});
    }

    public DashBorder(Color c, int thickness, float [] dash) {
    super(c, thickness);
    this.stroke = new BasicStroke(thickness, BasicStroke.CAP_BUTT,
    BasicStroke.JOIN_BEVEL, 1, dash, 0);
    }

    private boolean solid;
    private BasicStroke stroke;

    public void paintBorder(Component c, Graphics g, int x, int y, int
    width, int height) {
    if(!solid) {
    Graphics2D g2d = (Graphics2D)g.create();
    g2d.setStroke(stroke);
    super.paintBorder(c, g2d, x, y, width, height);
    g2d.dispose();
    }
    else {
    super.paintBorder(c, g, x, y, width, height);
    }
    }
    }

    Andrey

    --
    Andrey Kuznetsov
    http://uio.imagero.com Unified I/O for Java
    http://reader.imagero.com Java image reader
    http://jgui.imagero.com Java GUI components and utilities
     
    Andrey Kuznetsov, Jun 27, 2006
    #13
  14. Barkster

    Barkster Guest

    Thanks Andrey, I'll see if I can get it implemented

    Andrey Kuznetsov wrote:
    > > Sorry for my ignorance but I having a problem implementing this
    > > solution. I get an error:

    >
    > this should work and gives you possibility to switch between solid and
    > dashed border.
    > I hope this is not your homework.
    >
    > public class DashBorder extends LineBorder {
    >
    > public DashBorder(Color c) {
    > this(c, 1);
    > }
    >
    > public DashBorder(Color c, int thickness) {
    > this(c, thickness, new float[] {5, 5});
    > }
    >
    > public DashBorder(Color c, int thickness, float [] dash) {
    > super(c, thickness);
    > this.stroke = new BasicStroke(thickness, BasicStroke.CAP_BUTT,
    > BasicStroke.JOIN_BEVEL, 1, dash, 0);
    > }
    >
    > private boolean solid;
    > private BasicStroke stroke;
    >
    > public void paintBorder(Component c, Graphics g, int x, int y, int
    > width, int height) {
    > if(!solid) {
    > Graphics2D g2d = (Graphics2D)g.create();
    > g2d.setStroke(stroke);
    > super.paintBorder(c, g2d, x, y, width, height);
    > g2d.dispose();
    > }
    > else {
    > super.paintBorder(c, g, x, y, width, height);
    > }
    > }
    > }
    >
    > Andrey
    >
    > --
    > Andrey Kuznetsov
    > http://uio.imagero.com Unified I/O for Java
    > http://reader.imagero.com Java image reader
    > http://jgui.imagero.com Java GUI components and utilities
     
    Barkster, Jun 28, 2006
    #14
  15. Barkster

    Barkster Guest

    Hello Andrey, I got the class implemented finally but I'm having a hard
    time changing the border. From what I see when paint is called it
    checks to see if it is solid else it paints dashed? But when I run it
    and change the border to something other than solid it just changes to
    that. How do I swap back to dashed? Here is what I'm using

    if(this.jCheckBox1.isSelected()) {
    pn.setBorder(BorderFactory.createLineBorder(Color.black));
    System.out.println("Checked");
    }else {
    pn.setBorder(BorderFactory.createEtchedBorder());
    System.out.println("UnChecked");
    }

    when I debug the paint in dahedborder class

    if (!solid) {
    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setStroke(stroke);
    super.paintBorder(c, g2d, x, y, width, height);
    g2d.dispose();
    System.out.println("Not Solid");
    } else {
    super.paintBorder(c, g, x, y, width, height);
    System.out.println("Solid");
    }

    it only prints "Not Solid" on load after than when I change the
    checkbox it only prints Check/Unchecked? Any help would be
    appreciated, I'm struggling with learning java.

    Thank
    Barkster wrote:
    > Thanks Andrey, I'll see if I can get it implemented
    >
    > Andrey Kuznetsov wrote:
    > > > Sorry for my ignorance but I having a problem implementing this
    > > > solution. I get an error:

    > >
    > > this should work and gives you possibility to switch between solid and
    > > dashed border.
    > > I hope this is not your homework.
    > >
    > > public class DashBorder extends LineBorder {
    > >
    > > public DashBorder(Color c) {
    > > this(c, 1);
    > > }
    > >
    > > public DashBorder(Color c, int thickness) {
    > > this(c, thickness, new float[] {5, 5});
    > > }
    > >
    > > public DashBorder(Color c, int thickness, float [] dash) {
    > > super(c, thickness);
    > > this.stroke = new BasicStroke(thickness, BasicStroke.CAP_BUTT,
    > > BasicStroke.JOIN_BEVEL, 1, dash, 0);
    > > }
    > >
    > > private boolean solid;
    > > private BasicStroke stroke;
    > >
    > > public void paintBorder(Component c, Graphics g, int x, int y, int
    > > width, int height) {
    > > if(!solid) {
    > > Graphics2D g2d = (Graphics2D)g.create();
    > > g2d.setStroke(stroke);
    > > super.paintBorder(c, g2d, x, y, width, height);
    > > g2d.dispose();
    > > }
    > > else {
    > > super.paintBorder(c, g, x, y, width, height);
    > > }
    > > }
    > > }
    > >
    > > Andrey
    > >
    > > --
    > > Andrey Kuznetsov
    > > http://uio.imagero.com Unified I/O for Java
    > > http://reader.imagero.com Java image reader
    > > http://jgui.imagero.com Java GUI components and utilities
     
    Barkster, Jul 3, 2006
    #15
  16. > Hello Andrey, I got the class implemented finally but I'm having a hard
    > time changing the border. From what I see when paint is called it
    > checks to see if it is solid else it paints dashed? But when I run it
    > and change the border to something other than solid it just changes to
    > that. How do I swap back to dashed? Here is what I'm using
    >
    > if(this.jCheckBox1.isSelected()) {
    > pn.setBorder(BorderFactory.createLineBorder(Color.black));
    > System.out.println("Checked");
    > }else {
    > pn.setBorder(BorderFactory.createEtchedBorder());
    > System.out.println("UnChecked");
    > }
    >
    > when I debug the paint in dahedborder class
    >
    > if (!solid) {
    > Graphics2D g2d = (Graphics2D) g.create();
    > g2d.setStroke(stroke);
    > super.paintBorder(c, g2d, x, y, width, height);
    > g2d.dispose();
    > System.out.println("Not Solid");
    > } else {
    > super.paintBorder(c, g, x, y, width, height);
    > System.out.println("Solid");
    > }
    >
    > it only prints "Not Solid" on load after than when I change the
    > checkbox it only prints Check/Unchecked? Any help would be
    > appreciated, I'm struggling with learning java.


    you should really learn java first, but ok.
    I told you already that you have to implement getter and setter for solid by
    yourself.
    this is pretty simple thing:

    class DashedBorder {


    .....

    //getter:
    public boolean isSolid() {
    return solid;
    }

    //setter:
    public void setSolid(boolean b) {
    this.solid = b;
    }
    }

    now you can use setter in your checkbox handler:

    JCheckBox dashBox = new JCheckBox("Dashed Border");
    dashBox.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    dashBorder.setSolid(!dashBox.isSelected());
    }
    }

    HTH

    Andrey

    --
    http://uio.imagero.com Unified I/O for Java
    http://reader.imagero.com Java image reader
    http://jgui.imagero.com Java GUI components and utilities
     
    Andrey Kuznetsov, Jul 3, 2006
    #16
  17. Barkster

    Barkster Guest

    Thank Andrey, I think I getting through it ok for a beginner just not
    used to most of the terminology and methodology. I appreciate the
    help.

    Andrey Kuznetsov wrote:
    > > Hello Andrey, I got the class implemented finally but I'm having a hard
    > > time changing the border. From what I see when paint is called it
    > > checks to see if it is solid else it paints dashed? But when I run it
    > > and change the border to something other than solid it just changes to
    > > that. How do I swap back to dashed? Here is what I'm using
    > >
    > > if(this.jCheckBox1.isSelected()) {
    > > pn.setBorder(BorderFactory.createLineBorder(Color.black));
    > > System.out.println("Checked");
    > > }else {
    > > pn.setBorder(BorderFactory.createEtchedBorder());
    > > System.out.println("UnChecked");
    > > }
    > >
    > > when I debug the paint in dahedborder class
    > >
    > > if (!solid) {
    > > Graphics2D g2d = (Graphics2D) g.create();
    > > g2d.setStroke(stroke);
    > > super.paintBorder(c, g2d, x, y, width, height);
    > > g2d.dispose();
    > > System.out.println("Not Solid");
    > > } else {
    > > super.paintBorder(c, g, x, y, width, height);
    > > System.out.println("Solid");
    > > }
    > >
    > > it only prints "Not Solid" on load after than when I change the
    > > checkbox it only prints Check/Unchecked? Any help would be
    > > appreciated, I'm struggling with learning java.

    >
    > you should really learn java first, but ok.
    > I told you already that you have to implement getter and setter for solid by
    > yourself.
    > this is pretty simple thing:
    >
    > class DashedBorder {
    >
    >
    > ....
    >
    > //getter:
    > public boolean isSolid() {
    > return solid;
    > }
    >
    > //setter:
    > public void setSolid(boolean b) {
    > this.solid = b;
    > }
    > }
    >
    > now you can use setter in your checkbox handler:
    >
    > JCheckBox dashBox = new JCheckBox("Dashed Border");
    > dashBox.addActionListener(new ActionListener() {
    > public void actionPerformed(ActionEvent e) {
    > dashBorder.setSolid(!dashBox.isSelected());
    > }
    > }
    >
    > HTH
    >
    > Andrey
    >
    > --
    > http://uio.imagero.com Unified I/O for Java
    > http://reader.imagero.com Java image reader
    > http://jgui.imagero.com Java GUI components and utilities
     
    Barkster, Jul 3, 2006
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rick
    Replies:
    3
    Views:
    1,466
    Sandip Chitale
    Sep 30, 2003
  2. Andreas Beresko

    Add a JPanel to a JPanel...

    Andreas Beresko, Jul 14, 2004, in forum: Java
    Replies:
    6
    Views:
    27,037
  3. TheKeith
    Replies:
    15
    Views:
    6,828
    Denise Enck
    Jun 30, 2003
  4. Raphael Jolivet

    [JPanel] Drop Down JPanel

    Raphael Jolivet, Jul 18, 2008, in forum: Java
    Replies:
    1
    Views:
    1,550
    John B. Matthews
    Jul 18, 2008
  5. t1m1976
    Replies:
    1
    Views:
    2,448
    t1m1976
    Nov 7, 2010
Loading...

Share This Page