Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Swing / AWT / SWT
setting border to Panel, how-to?
Akshay Kumar
Ranch Hand
Posts: 37
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
hi,
i need to set border to Panel(AWT component) but i know that AWT Panel doesn't support this unlike JPanel.
anyone with the solution can extend their arms.
thanks.
Darin Niard
Ranch Hand
Posts: 118
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Is there a particular reason that you can't use a JPanel?
Craig Wood
Ranch Hand
Posts: 1535
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Here's a possibility. Let your imagination play and see what you come up with...
import java.awt.*; import java.awt.event.*; import java.awt.font.*; import java.awt.geom.*; public class AWTBorder { static Font font; public static void main(String[] args) { font = new Font("lucida sans demibold", Font.PLAIN, 24); Panel red = new Panel(), green = new Panel(), blue = new Panel(); red.setLayout(new BorderLayout()); green.setLayout(new BorderLayout()); blue.setLayout(new BorderLayout()); red.setBackground(Color.red); green.setBackground(Color.green); blue.setBackground(Color.blue); red.add(getLabel("Line Border", Color.orange)); green.add(getLabel("Color Border", Color.blue)); blue.add(getLabel("Title Border", Color.yellow)); Panel linePanel = new BorderPanel(red), colorPanel = new BorderPanel(green, Color.magenta), titlePanel = new BorderPanel(blue, "Blue Panel"); Panel panel = new Panel(); panel.setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(5,5,5,5); gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.fill = gbc.BOTH; gbc.gridwidth = gbc.REMAINDER; panel.add(linePanel, gbc); panel.add(colorPanel, gbc); panel.add(titlePanel, gbc); ScrollPane scrollPane = new ScrollPane(); scrollPane.add(panel); Frame f = new Frame("AWT Border"); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); f.add(scrollPane); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } private static Label getLabel(String text, Color color) { Label label = new Label(text); label.setForeground(color); label.setAlignment(Label.CENTER); label.setFont(font); return label; } } class BorderPanel extends Panel { Component c; Color color; String text; Font font; final int PAD = 5; GridBagLayout gridbag; GridBagConstraints gbc; boolean firstTime; final static int LINE = 0, COLOR = 1, TITLE = 2; int style; private BorderPanel(Component c, int style) { this.c = c; this.style = style; firstTime = true; gridbag = new GridBagLayout(); setLayout(gridbag); setBackground(Color.lightGray); gbc = new GridBagConstraints(); gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.fill = gbc.BOTH; add(c, gbc); } public BorderPanel(Component c) { this(c, LINE); int x = 2*PAD; gbc.insets = new Insets(x, x, x, x); gridbag.setConstraints(c, gbc); } public BorderPanel(Component c, Color color) { this(c, COLOR); this.color = color; int x = PAD + 4; gbc.insets = new Insets(x + 1, x + 1, x, x); gridbag.setConstraints(c, gbc); } public BorderPanel(Component c, String s) { this(c, TITLE); text = s; font = new Font("lucida bright regular", Font.PLAIN, 20); } public void paint(Graphics g) { super.paint(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int w = getWidth(); int h = getHeight(); g2.setPaint(Color.gray); switch(style) { case LINE: g2.draw(new Line2D.Double(PAD, PAD, w - PAD - 1, PAD)); g2.draw(new Line2D.Double(PAD, PAD, PAD, h - PAD - 1)); g2.draw(new Line2D.Double(PAD, h - PAD - 1, w - PAD - 1, h - PAD - 1)); g2.draw(new Line2D.Double(w - PAD - 1, PAD, w - PAD - 1, h - PAD - 1)); break; case COLOR: int line = 4; g2.setPaint(color); g2.fill(new Rectangle2D.Double(PAD, PAD, w - 2*PAD, line)); g2.fill(new Rectangle2D.Double(PAD, PAD + line, line, h - 2*(PAD + line))); g2.fill(new Rectangle2D.Double(PAD, h - 2*PAD + 1, w - 2*PAD, line)); g2.fill(new Rectangle2D.Double(w - PAD - line, PAD + line, line, h - 2*(PAD + line))); break; case TITLE: g2.setFont(font); FontRenderContext frc = g2.getFontRenderContext(); float textWidth = (float)font.getStringBounds(text, frc).getWidth(); LineMetrics lm = font.getLineMetrics(text, frc); float x = (w - textWidth)/2; float y = PAD + lm.getAscent(); if(firstTime) adjustPanel(y); g2.drawString(text, x, y); int cy = PAD + (int)lm.getAscent()/2; g2.draw(new Line2D.Double(PAD, y, (w - textWidth)/2 - PAD, y)); g2.draw(new Line2D.Double(w/2 + textWidth/2 + PAD, y, w - PAD - 1, y)); g2.draw(new Line2D.Double(PAD, y, PAD, h - PAD - 1)); g2.draw(new Line2D.Double(PAD , h - PAD - 1, w - PAD - 1, h - PAD - 1)); g2.draw(new Line2D.Double(w - PAD - 1, h - PAD - 1, w - PAD - 1, y)); } } private void adjustPanel(float y) { int x = 2*PAD; int top = (int)(y + PAD); gbc.insets = new Insets(top, x, x, x); gridbag.setConstraints(c, gbc); gridbag.layoutContainer(this); validate(); firstTime = false; } }
Akshay Kumar
Ranch Hand
Posts: 37
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
hi darin,
yeah, bcoz v r using
applets
to display it in browsers and restricted to use swings for that, here. thanks.
hi craig,
thanks a lot for such a good code and also the way v can use
java
by just in anyway. thanks, again.
bye bye
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Swing vs AWT
how to set this a borderq
Suppressing borders on a TextArea
Borders
AWT Panels with Borders
More...