Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

Jpanel Dashed Border?

Thread view: 
Barkster - 27 Jun 2006 17:42 GMT
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
Thomas Fritsch - 27 Jun 2006 18:24 GMT
> 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

Signature

Thomas

Vova Reznik - 27 Jun 2006 18:52 GMT
> 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 - 27 Jun 2006 19:01 GMT
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.

> > 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
Vova Reznik - 27 Jun 2006 19:13 GMT
> 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
[quoted text clipped - 5 lines]
>>>
>> 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;
    }
}
Andrey Kuznetsov - 27 Jun 2006 20:11 GMT
> public void paintBorder(Component c, Graphics g, int x, int y,
> int width, int height) {
[quoted text clipped - 12 lines]
> }
> }

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
Signature

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 - 27 Jun 2006 21:49 GMT
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();
 }
}
Vova Reznik - 27 Jun 2006 21:58 GMT
>> public void paintBorder(Component c, Graphics g, int x, int y,
>> int width, int height) {
[quoted text clipped - 23 lines]
>     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
Barkster - 27 Jun 2006 22:21 GMT
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();
   }  
}
Vova Reznik - 27 Jun 2006 22:45 GMT
> Sorry for my ignorance but I having a problem implementing this
> solution.  I get an error:
[quoted text clipped - 8 lines]
>                                          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) {
[quoted text clipped - 4 lines]
>     }  
> }
Vova Reznik - 27 Jun 2006 22:45 GMT
> 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) {
[quoted text clipped - 4 lines]
>     }  
> }
Andrey Kuznetsov - 27 Jun 2006 23:14 GMT
> 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

Signature

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 - 28 Jun 2006 01:13 GMT
Thanks Andrey, I'll see if I can get it implemented

> > Sorry for my ignorance but I having a problem implementing this
> > solution.  I get an error:
[quoted text clipped - 43 lines]
> http://reader.imagero.com Java image reader
> http://jgui.imagero.com Java GUI components and utilities
Barkster - 03 Jul 2006 17:33 GMT
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
> Thanks Andrey, I'll see if I can get it implemented
>
[quoted text clipped - 45 lines]
> > http://reader.imagero.com Java image reader
> > http://jgui.imagero.com Java GUI components and utilities
Andrey Kuznetsov - 03 Jul 2006 18:14 GMT
> 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
[quoted text clipped - 26 lines]
> 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

Signature

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 - 03 Jul 2006 19:13 GMT
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.

> > 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
[quoted text clipped - 64 lines]
> http://reader.imagero.com Java image reader
> http://jgui.imagero.com Java GUI components and utilities
Andrey Kuznetsov - 27 Jun 2006 22:54 GMT
>>     public void paintBorder(Component c, Graphics g, int x, int y, int
>> width, int height) {
[quoted text clipped - 3 lines]
>>         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

Signature

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.