Switch to full style
Graphics and animation Java code examples
Post a reply

BorderFactory create EtchedBorder, Line Border,GridBagLayout

Thu Feb 07, 2013 10:15 pm

java code
/*
* Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL purposes and without
* fee is hereby granted provided that this copyright notice
* appears in all copies. Please refer to the file "copyright.html"
* for further important copyright and licensing information.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/
import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.BorderFactory;
import com.sun.java.swing.border.Border;
import com.sun.java.swing.border.TitledBorder;
import com.sun.java.swing.ImageIcon;
import com.sun.java.swing.JTabbedPane;
import com.sun.java.swing.JLabel;
import com.sun.java.swing.JPanel;
import com.sun.java.swing.JFrame;

public class BorderDemo extends JTabbedPane {
GridBagConstraints c = new GridBagConstraints();

public BorderDemo() {
Border blackline,
raisedbevel, loweredbevel,
empty;

blackline = BorderFactory.createLineBorder(Color.black);
raisedbevel = BorderFactory.createRaisedBevelBorder();
loweredbevel = BorderFactory.createLoweredBevelBorder();
empty = BorderFactory.createEmptyBorder();

GridBagLayout gridbag = null;
setUpGridBagConstraints();

//First pane: simple borders
JPanel simpleBorders = new JPanel();
gridbag = new GridBagLayout();
simpleBorders.setLayout(gridbag);

addCompForBorder(BorderFactory.createEtchedBorder(),
"etched border",
simpleBorders, gridbag);
addCompForBorder(blackline, "line border",
simpleBorders, gridbag);
addCompForBorder(raisedbevel, "raised bevel border",
simpleBorders, gridbag);
addCompForBorder(loweredbevel, "lowered bevel border",
simpleBorders, gridbag);
addCompForBorder(empty, "empty border",
simpleBorders, gridbag);

//Second pane: titled borders
JPanel titledBorders = new JPanel();
gridbag = new GridBagLayout();
titledBorders.setLayout(gridbag);
TitledBorder titled;

titled = BorderFactory.createTitledBorder("title");
addCompForBorder(titled,
"titled etched border"
+ " (default just., default pos.)",
titledBorders, gridbag);

titled = BorderFactory.createTitledBorder(
blackline, "title");
addCompForTitledBorder(titled,
"titled line border"
+ " (centered, default pos.)",
TitledBorder.CENTER,
TitledBorder.DEFAULT_POSITION,
titledBorders, gridbag);

titled = BorderFactory.createTitledBorder(
raisedbevel, "title");
addCompForTitledBorder(titled,
"titled raised bevel border"
+ " (right just., default pos.)",
TitledBorder.RIGHT,
TitledBorder.DEFAULT_POSITION,
titledBorders, gridbag);

titled = BorderFactory.createTitledBorder(
loweredbevel, "title");
addCompForTitledBorder(titled,
"titled lowered bevel border"
+ " (default just., above top)",
TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.ABOVE_TOP,
titledBorders, gridbag);

titled = BorderFactory.createTitledBorder(
empty, "title");
addCompForTitledBorder(titled, "titled empty border"
+ " (default just., bottom)",
TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.BOTTOM,
titledBorders, gridbag);

//Third pane: compound borders
JPanel compoundBorders = new JPanel();
gridbag = new GridBagLayout();
compoundBorders.setLayout(gridbag);
Border redline = BorderFactory.createLineBorder(Color.red);

Border compound;
compound = BorderFactory.createCompoundBorder(
raisedbevel, loweredbevel);
addCompForBorder(compound, "compound border (two bevels)",
compoundBorders, gridbag);

compound = BorderFactory.createCompoundBorder(
redline, compound);
addCompForBorder(compound, "compound border (add a red outline)",
compoundBorders, gridbag);

titled = BorderFactory.createTitledBorder(
compound, "title",
TitledBorder.CENTER,
TitledBorder.BELOW_BOTTOM);
addCompForBorder(titled,
"titled compound border"
+ " (centered, below bottom)",
compoundBorders, gridbag);

addTab("Simple Borders", null, simpleBorders, null);
addTab("Titled Borders", null, titledBorders, null);
addTab("Compound Borders", null, compoundBorders, null);
setSelectedIndex(0);
}


void addCompForTitledBorder(TitledBorder border,
String description,
int justification,
int position,
Container container,
GridBagLayout gridbag) {
border.setTitleJustification(justification);
border.setTitlePosition(position);
addCompForBorder(border, description,
container, gridbag);
}

void addCompForBorder(Border border,
String description,
Container container,
GridBagLayout gridbag) {
JPanel comp = new JPanel(false);
JLabel label = new JLabel(description, JLabel.CENTER);
comp.setLayout(new GridLayout(1, 1));
comp.add(label);
comp.setBorder(border);

gridbag.setConstraints(comp, c);
container.add(comp);
}

/*
* Pretend this isn't here...
* Ordinarily, we might use BoxLayout instead of GridBagLayout
* for laying out simple columns.
*
* However, with BoxLayout we'd have to either insert many spacers
* (a bit ugly) or use empty borders to create extra space around
* each component. And since this is a border example, we
* don't want that extra empty-border code in here confusing
* people. So to simplify the code that's important for this
* example, we use GridBagLayout but hide much of the layout
* code here.
*/
void setUpGridBagConstraints() {
// Put some space between the border demos.
c.insets = new Insets(10, 20, 10, 20);

//Add some space between the label and the border.
c.ipadx = 5;
c.ipady = 20;

//Make each border demo as big as available space allows.
c.fill = GridBagConstraints.BOTH;

//One item per row.
c.gridwidth = GridBagConstraints.REMAINDER;

//Set up column/row weights so resizing works.
c.weightx = 1.0; //make the column as wide as possible.
c.weighty = 1.0; //equal weights for each row.
}

public static void main(String[] args) {
JFrame frame = new JFrame("BorderDemo");

frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

frame.getContentPane().add("Center", new BorderDemo());
frame.pack();
frame.show();
}
}




Post a reply
  Related Posts  to : BorderFactory create EtchedBorder, Line Border,GridBagLayout
 Reading a File Line by Line in php     -  
 Using list with GridBagLayout and GridBagConstraints     -  
 elements of a GridBagLayout organized     -  
 Border around JPG img     -  
 Add solid Border to div     -  
 Add dotted Border to div     -  
 Set border bottom color     -  
 separate border collapse example     -  
 Add dashed border to text div     -  
 set the size of border in pixels     -  

Topic Tags

Java Swing