Using a BitSet

The BitSet allows you to work with bits (0 and 1). You can not only get and set bits individually but also perform bitwise operations like and and or. The size of the BitSet may vary. For example, if you initialy set the first bit to 1, 64 bits will be used. Then if you set the bit 100 to 1, the BitSet will grow to 128. The 64 alignment may be different in future implementations.

Operations:

and:   result of (a and b) is 1 if only a and b have the value of 1
or:    result of (a or b) is 1 if at least a or b has the value of 1
xor:   result of (a xor b) is 1 if a or b has the value of 1, but not both

Main.java:

import java.util.*;
 
public class Main {
   public static void main(String args[]) {
      BitSet bs1 = new BitSet();
 
      bs1.set(7);               // sets the 7th bit   00000001000... (64)
      System.out.println(bs1);  // outputs {7}
      bs1.set(5);	        // sets the 5th bit   00000101000... (64)
      System.out.println(bs1);  // outputs {5, 7}
      bs1.clear(7);             //                    00000100000... (64)
      System.out.println(bs1);  // outputs {5}
      
      BitSet bs2 = new BitSet();
      bs2.set(1);               //                    01000000000... (64)
      bs2.set(5);               //                    01000100000... (64)
 
      bs1.and(bs2);             //                    00000100000... (64)
      System.out.println(bs1);  // outputs {5}
 
      bs1.xor(bs2);             //                    01000000000... (64)
      System.out.println(bs1);  // outputs {1}
 
      bs1.or(bs2);              //                    01000100000... (64)
      System.out.println(bs1);  // outputs {1, 5}
   }
}

outputs:

{7}
{5, 7}
{5}
{5}
{1}
{1, 5}

Determining the number of days in a month of a particular year

This small example looks only at the days of february of the last 20 years.

Main.java:

import java.util.*;
 
public class Main
{
   public static void main(String []args) {
      for (int i=1980; i<2010; i++) {
         Calendar calendar = new GregorianCalendar(i, Calendar.FEBRUARY, 1);
 
         int numberOfDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
 
         System.out.println("February " + i + ": " + numberOfDays + " days");
      }
   }
} 

outputs:

February 1980: 29 days
February 1981: 28 days
February 1982: 28 days
February 1983: 28 days
February 1984: 29 days
February 1985: 28 days
February 1986: 28 days
February 1987: 28 days
February 1988: 29 days
February 1989: 28 days
February 1990: 28 days
February 1991: 28 days
February 1992: 29 days
February 1993: 28 days
February 1994: 28 days
February 1995: 28 days
February 1996: 29 days
February 1997: 28 days
February 1998: 28 days
February 1999: 28 days
February 2000: 29 days
February 2001: 28 days
February 2002: 28 days
February 2003: 28 days
February 2004: 29 days
February 2005: 28 days
February 2006: 28 days
February 2007: 28 days
February 2008: 29 days
February 2009: 28 days

Getting a list of the available locales

import java.util.Locale;
 
public class Main
{
   public static void main(String[] args) {
      Locale[] locales = Locale.getAvailableLocales();
 
      printInColumn("DisplayName", 20);
      printInColumn("DisplayCountry", 15);
      printInColumn("DispLang", 11);
      printInColumn("ISO3L", 6);
      printInColumn("Lang", 5);
      printInColumn("ISO3C", 6);
      printInColumn("Variant", 6);
      printInColumn("DispVariant", 10);
      System.out.println();
      System.out.println();
 
      for (int i=0; i<locales.length; i++) {
         printInColumn(locales[i].getDisplayName(), 20);
         printInColumn(locales[i].getDisplayCountry(), 15);
         printInColumn(locales[i].getDisplayLanguage(), 11);
         printInColumn(locales[i].getISO3Language(), 6);
         printInColumn(locales[i].getLanguage(), 5);
         printInColumn(locales[i].getISO3Country(), 6);
         printInColumn(locales[i].getVariant(), 6);
         printInColumn(locales[i].getDisplayVariant(), 10);
         System.out.println();
      }
   }
 
   public static void printInColumn(String s, int col) {
      if (s.length() > col-2) s = s.substring(0, col-2);
      System.out.print(s);
      for (int i=s.length(); i<col; ++i)
         System.out.print(" ");
   }
}

outputs:

DisplayName         DisplayCountr  DispLang   ISO3  Lan  ISO3  Vari  DispVari  

English                            English    eng   en                         
English (United St  United States  English    eng   en   USA                   
Arabic                             Arabic     ara   ar                         
Arabic (United Ara  United Arab E  Arabic     ara   ar   ARE                   
Arabic (Bahrain)    Bahrain        Arabic     ara   ar   BHR                   
Arabic (Algeria)    Algeria        Arabic     ara   ar   DZA                   
Arabic (Egypt)      Egypt          Arabic     ara   ar   EGY                   
Arabic (Iraq)       Iraq           Arabic     ara   ar   IRQ                   
Arabic (Jordan)     Jordan         Arabic     ara   ar   JOR                   
Arabic (Kuwait)     Kuwait         Arabic     ara   ar   KWT                   
Arabic (Lebanon)    Lebanon        Arabic     ara   ar   LBN                   
Arabic (Libya)      Libya          Arabic     ara   ar   LBY                   
Arabic (Morocco)    Morocco        Arabic     ara   ar   MAR                   
Arabic (Oman)       Oman           Arabic     ara   ar   OMN                   
Arabic (Qatar)      Qatar          Arabic     ara   ar   QAT                   
Arabic (Saudi Arab  Saudi Arabia   Arabic     ara   ar   SAU                   
Arabic (Sudan)      Sudan          Arabic     ara   ar   SDN                   
Arabic (Syria)      Syria          Arabic     ara   ar   SYR                   
Arabic (Tunisia)    Tunisia        Arabic     ara   ar   TUN                   
Arabic (Yemen)      Yemen          Arabic     ara   ar   YEM                   
Byelorussian                       Byeloruss  bel   be                         
Byelorussian (Bela  Belarus        Byeloruss  bel   be   BLR                   
Bulgarian                          Bulgarian  bul   bg                         
Bulgarian (Bulgari  Bulgaria       Bulgarian  bul   bg   BGR                   
Catalan                            Catalan    cat   ca                         
Catalan (Spain)     Spain          Catalan    cat   ca   ESP                   
Czech                              Czech      ces   cs                         
Czech (Czech Repub  Czech Republi  Czech      ces   cs   CZE                   
Danish                             Danish     dan   da                         
Danish (Denmark)    Denmark        Danish     dan   da   DNK                   
German                             German     deu   de                         
German (Austria)    Austria        German     deu   de   AUT                   
German (Austria,Eu  Austria        German     deu   de   AUT   EURO  Euro      
German (Switzerlan  Switzerland    German     deu   de   CHE                   
German (Germany)    Germany        German     deu   de   DEU                   
German (Germany,Eu  Germany        German     deu   de   DEU   EURO  Euro      
German (Luxembourg  Luxembourg     German     deu   de   LUX                   
German (Luxembourg  Luxembourg     German     deu   de   LUX   EURO  Euro      
Greek                              Greek      ell   el                         
Greek (Greece)      Greece         Greek      ell   el   GRC                   
English (Australia  Australia      English    eng   en   AUS                   
English (Canada)    Canada         English    eng   en   CAN                   
English (United Ki  United Kingdo  English    eng   en   GBR                   
English (Ireland)   Ireland        English    eng   en   IRL                   
English (Ireland,E  Ireland        English    eng   en   IRL   EURO  Euro      
English (New Zeala  New Zealand    English    eng   en   NZL                   
English (South Afr  South Africa   English    eng   en   ZAF                   
Spanish                            Spanish    spa   es                         
Spanish (Bolivia)   Bolivia        Spanish    spa   es   BOL                   
Spanish (Argentina  Argentina      Spanish    spa   es   ARG                   
Spanish (Chile)     Chile          Spanish    spa   es   CHL                   
Spanish (Colombia)  Colombia       Spanish    spa   es   COL                   
Spanish (Costa Ric  Costa Rica     Spanish    spa   es   CRI                   
Spanish (Dominican  Dominican Rep  Spanish    spa   es   DOM                   
Spanish (Ecuador)   Ecuador        Spanish    spa   es   ECU                   
Spanish (Spain)     Spain          Spanish    spa   es   ESP                   
Spanish (Spain,Eur  Spain          Spanish    spa   es   ESP   EURO  Euro      
Spanish (Guatemala  Guatemala      Spanish    spa   es   GTM                   
Spanish (Honduras)  Honduras       Spanish    spa   es   HND                   
Spanish (Mexico)    Mexico         Spanish    spa   es   MEX                   
Spanish (Nicaragua  Nicaragua      Spanish    spa   es   NIC                   
Spanish (Panama)    Panama         Spanish    spa   es   PAN                   
Spanish (Peru)      Peru           Spanish    spa   es   PER                   
Spanish (Puerto Ri  Puerto Rico    Spanish    spa   es   PRI                   
Spanish (Paraguay)  Paraguay       Spanish    spa   es   PRY                   
Spanish (El Salvad  El Salvador    Spanish    spa   es   SLV                   
Spanish (Uruguay)   Uruguay        Spanish    spa   es   URY                   
Spanish (Venezuela  Venezuela      Spanish    spa   es   VEN                   
Estonian                           Estonian   est   et                         
Estonian (Estonia)  Estonia        Estonian   est   et   EST                   
Finnish                            Finnish    fin   fi                         
Finnish (Finland)   Finland        Finnish    fin   fi   FIN                   
Finnish (Finland,E  Finland        Finnish    fin   fi   FIN   EURO  Euro      
French                             French     fra   fr                         
French (Belgium)    Belgium        French     fra   fr   BEL                   
French (Belgium,Eu  Belgium        French     fra   fr   BEL   EURO  Euro      
French (Canada)     Canada         French     fra   fr   CAN                   
French (Switzerlan  Switzerland    French     fra   fr   CHE                   
French (France)     France         French     fra   fr   FRA                   
French (France,Eur  France         French     fra   fr   FRA   EURO  Euro      
French (Luxembourg  Luxembourg     French     fra   fr   LUX                   
French (Luxembourg  Luxembourg     French     fra   fr   LUX   EURO  Euro      
Croatian                           Croatian   hrv   hr                         
Croatian (Croatia)  Croatia        Croatian   hrv   hr   HRV                   
Hungarian                          Hungarian  hun   hu                         
Hungarian (Hungary  Hungary        Hungarian  hun   hu   HUN                   
Icelandic                          Icelandic  isl   is                         
Icelandic (Iceland  Iceland        Icelandic  isl   is   ISL                   
Italian                            Italian    ita   it                         
Italian (Switzerla  Switzerland    Italian    ita   it   CHE                   
Italian (Italy)     Italy          Italian    ita   it   ITA                   
Italian (Italy,Eur  Italy          Italian    ita   it   ITA   EURO  Euro      
Hebrew                             Hebrew     heb   iw                         
Hebrew (Israel)     Israel         Hebrew     heb   iw   ISR                   
Japanese                           Japanese   jpn   ja                         
Japanese (Japan)    Japan          Japanese   jpn   ja   JPN                   
Korean                             Korean     kor   ko                         
Korean (South Kore  South Korea    Korean     kor   ko   KOR                   
Lithuanian                         Lithuania  lit   lt                         
Lithuanian (Lithua  Lithuania      Lithuania  lit   lt   LTU                   
Latvian (Lettish)                  Latvian (  lav   lv                         
Latvian (Lettish)   Latvia         Latvian (  lav   lv   LVA                   
Macedonian                         Macedonia  mkd   mk                         
Macedonian (Macedo  Macedonia      Macedonia  mkd   mk   MKD                   
Dutch                              Dutch      nld   nl                         
Dutch (Belgium)     Belgium        Dutch      nld   nl   BEL                   
Dutch (Belgium,Eur  Belgium        Dutch      nld   nl   BEL   EURO  Euro      
Dutch (Netherlands  Netherlands    Dutch      nld   nl   NLD                   
Dutch (Netherlands  Netherlands    Dutch      nld   nl   NLD   EURO  Euro      
Norwegian                          Norwegian  nor   no                         
Norwegian (Norway)  Norway         Norwegian  nor   no   NOR                   
Norwegian (Norway,  Norway         Norwegian  nor   no   NOR   NY    Nynorsk   
Polish                             Polish     pol   pl                         
Polish (Poland)     Poland         Polish     pol   pl   POL                   
Portuguese                         Portugues  por   pt                         
Portuguese (Brazil  Brazil         Portugues  por   pt   BRA                   
Portuguese (Portug  Portugal       Portugues  por   pt   PRT                   
Portuguese (Portug  Portugal       Portugues  por   pt   PRT   EURO  Euro      
Romanian                           Romanian   ron   ro                         
Romanian (Romania)  Romania        Romanian   ron   ro   ROM                   
Russian                            Russian    rus   ru                         
Russian (Russia)    Russia         Russian    rus   ru   RUS                   
Serbo-Croatian                     Serbo-Cro  srp   sh                         
Serbo-Croatian (Yu  Yugoslavia     Serbo-Cro  srp   sh   YUG                   
Slovak                             Slovak     slk   sk                         
Slovak (Slovakia)   Slovakia       Slovak     slk   sk   SVK                   
Slovenian                          Slovenian  slv   sl                         
Slovenian (Sloveni  Slovenia       Slovenian  slv   sl   SVN                   
Albanian                           Albanian   sqi   sq                         
Albanian (Albania)  Albania        Albanian   sqi   sq   ALB                   
Serbian                            Serbian    srp   sr                         
Swedish                            Swedish    swe   sv                         
Serbian (Yugoslavi  Yugoslavia     Serbian    srp   sr   YUG                   
Swedish (Sweden)    Sweden         Swedish    swe   sv   SWE                   
Thai                               Thai       tha   th                         
Thai (Thailand)     Thailand       Thai       tha   th   THA                   
Turkish                            Turkish    tur   tr                         
Turkish (Turkey)    Turkey         Turkish    tur   tr   TUR                   
Ukrainian                          Ukrainian  ukr   uk                         
Ukrainian (Ukraine  Ukraine        Ukrainian  ukr   uk   UKR                   
Chinese                            Chinese    zho   zh                         
Chinese (China)     China          Chinese    zho   zh   CHN                   
Chinese (Hong Kong  Hong Kong      Chinese    zho   zh   HKG                   
Chinese (Taiwan)    Taiwan         Chinese    zho   zh   TWN                   

Using the Properties class

The Properties class is able to contain key/value pairs, just like a Hashtable (in fact, Properties extends from Hashtable). An example of where the Properties class is used is in the System class, that uses this class to store its system properties (like java.version, …). Properties allows its key/value pairs to be loaded and stored and through the methods load and store.

This following example stores the system properties in a file. The store method allows you to save an extra header comment for identification purposes.

Main.java:

import java.util.*;
import java.io.*;
 
public class Main {   
   public static void main(String[] args) throws Exception {
      if (args.length != 1) {
         System.out.println("Usage: java Main <filename>");
         System.exit(1);
      }
 
      BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(args[0]));
      Properties p = System.getProperties();
      p.store(bos, "Our stored System properties");     
   }
}

stores in a specified file:

#Our stored System properties
#Wed Jun 27 17:21:15 CEST 2001
java.specification.name=Java Platform API Specification
awt.toolkit=sun.awt.windows.WToolkit
java.version=1.2.2
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
user.timezone=Europe/Paris
java.specification.version=1.2
java.vm.vendor=Sun Microsystems Inc.
java.vm.specification.version=1.0
user.home=C:\WINDOWS
os.arch=x86
java.awt.fonts=
java.vendor.url=http://java.sun.com/
file.encoding.pkg=sun.io
user.region=US
java.home=C:\JDK1.2.2\JRE
java.class.path=c:\jdk1.2.2\jre\lib\rt.jar;C:\PRO
GRA~1\JMF2.1\LIB\SOUND.JAR;C:\PROGRA~1\JMF2.1\LIB\
JMF.JAR;c:\jakarta-log4j-1.0.4\log4j-core.jar;
line.separator=rn
java.ext.dirs=C:\JDK1.2.2\JRE\lib\ext
java.io.tmpdir=c:\windows\TEMP\
os.name=Windows 95
java.vendor=Sun Microsystems Inc.
java.awt.printerjob=sun.awt.windows.WPrinterJob
java.library.path=C:\JDK1.2.2\BIN;.;C:\WINDOWS\SYSTEM;C:\WINDOWS;C:\JIKES\BIN
;C:\ANT\BIN\;C:\JDK1.2.2\BIN;C:\WINDOWS;C:\WIN
DOWS;C:\WINDOWS\COMMAND;;C:\WN16\BIN;C:\PROGRA~1
NETWOR~1\PGP;C:\VIM\VIM55
java.vm.specification.vendor=Sun Microsystems Inc.
sun.io.unicode.encoding=UnicodeLittle
file.encoding=Cp1252
java.specification.vendor=Sun Microsystems Inc.
user.name=esus
user.language=en
java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport.cgi
java.vm.name=Classic VM
java.vm.specification.name=Java Virtual Machine Specification
java.class.version=46.0
sun.boot.library.path=C:\JDK1.2.2\JRE\bin
os.version=4.10
java.vm.info=build JDK-1.2.2_005, native threads, symcjit
java.vm.version=1.2.2
java.compiler=symcjit
path.separator=;
user.dir=C:\
file.separator=\
sun.boot.class.path=C:\JDK1.2.2\JRE\lib\rt.jar;C:\
JDK1.2.2\JRE\lib\i18n.jar;C:\JDK1.2.2\JRE\classes

You can create your own properties object as follows:

import java.util.*;
import java.io.*;
 
public class Main {   
   public static void main(String[] args) throws Exception {
      // creating a properties object
      Properties p = new Properties();
      p.put("lastlogin", "crazycow");
      p.put("lastpassword", "j23js09zz");
      
      // storing our properties in the file test.prop 
      BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("test.prop"));
      p.store(bos, "Our own properties");     
 
      // loading our properties in another Properties object and 
      // printing its values out.
      Properties p2 = new Properties();
      p2.load(new BufferedInputStream(new FileInputStream("test.prop")));
      p2.list(System.out);
   }
}

Notice that Properties extends from Hashtable, so you can put elements in there that are not Strings. In this case, the load and save methods won’t work properly. A better way of implementating Properties is to have it contain a Hashtable instead of extending from it.

Tokenizing text with a StringTokenizer

First determine what characters delimit your text tokens, for example if you’re parsing a comma-separated values file (csv), your delimiter character would probably be the comma. Then create an instance of StringTokenizer with the String you want to split up into tokens and pass it your set of delimiter characters. You can also use the default set of delimiter characters which is ” tnrf”, the space character, the tab character, the newline character, the carriage-return character and the form-feed character.

Then continually call the method hasMoreTokens and nextToken until there are no more tokens. The StringTokenizer will keep track of the current position and nextToken will return the set of characters that appear before the next delimiter character. You can change the delimiter character(s) anytime by calling nextToken(String delim).

Here’s an example that reads in a csv and prints out its tokens.

csv.txt:

 "Mike",1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,9,8,7,6,5,4  
 "George",3,6,4,2,3,5,2,3,4  
 "Anne",7,4,3,2,8,5,4,1,,3,4,5  

Main.java:

import java.util.*;
import java.io.*;
 
public class Main
{
   public static void main(String []args) throws IOException {
      if (args.length != 1) {
         System.out.println("Usage: java Main <textfile>");
         System.exit(1);
      }
 
      String text = readFile(args[0]);
 
      String lineSep = System.getProperty("line.separator");
      StringTokenizer st = new StringTokenizer(text, " ," + lineSep);
      while (st.hasMoreTokens()) {
         String token = st.nextToken();
         if (token.charAt(0) == '"') {
            System.out.println();
         }
         System.out.print(token + "/");
      }
   }
 
   public static String readFile(String filename) throws IOException {
      BufferedReader br = new BufferedReader(new FileReader(filename));
      StringBuffer total = new StringBuffer();
      String lineSep = System.getProperty("line.separator");
      String line;
      while ((line = br.readLine()) != null) {
         total.append(line + lineSep);
      }
 
      br.close();
      return total.toString();
   }      
}

outputs:


"Mike"/1/2/3/4/5/6/7/8/9/1/2/3/4/5/6/9/8/7/6/5/4/
"George"/3/6/4/2/3/5/2/3/4/
"Anne"/7/4/3/2/8/5/4/1/3/4/5/

Putting a component in a TitledBorder?

Courtesy of Nobuo Tamemasa (http://www2.gol.com/users/tame/swing/examples/JTableExamples5.html)

CompTitledPaneExample1.java:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
 
/**
 * @version 1.0 08/12/99
 */  
public class CompTitledPaneExample1 extends JFrame {
 
  String title = "<html>Title (" 
               + "<font color="#ffffff"><i>JLabel</i></font>"
               + ")";
 
  public CompTitledPaneExample1() {
    super("CompTitledPaneExample1");
    JLabel titleLabel = new JLabel(title);
    CompTitledPane p1 = new CompTitledPane(titleLabel);
    JPanel p = p1.getContentPane();
    p.setLayout(new BorderLayout());
    p.add(new SwitchPanel(p1), BorderLayout.CENTER);
    getContentPane().add(p1, BorderLayout.CENTER);
  }
  
  class SwitchPanel extends JPanel implements ActionListener {
    String[] posStr = {"", "ABOVE_TOP"   , "TOP"   , "BELOW_TOP"
                         , "ABOVE_BOTTOM", "BOTTOM", "BELOW_BOTTOM"};
    String[] jusStr = {"", "LEFT"        , "CENTER", "RIGHT"};
    TitledBorder border;
    CompTitledPane panel;
    
    SwitchPanel(CompTitledPane panel) {
      this.panel = panel;
      this.border = (TitledBorder)panel.getBorder();
      add(createPanel("Position"     ,posStr, 2));
      add(createPanel("Justification",jusStr, 1));
    }
    
    JPanel createPanel(String str, String[] strs, int selectPos) {
      JPanel p = new JPanel();
      p.setLayout(new GridLayout(strs.length, 1));
      p.add(new JLabel(str));
      ButtonGroup g = new ButtonGroup();
      for (int i=1;i<strs.length;i++) {
        JRadioButton b = new JRadioButton(strs[i]);
        if (i == selectPos) {
          b.setSelected(true);
        }
        p.add( b );
        g.add( b );
        b.addActionListener(this);
      }
      return p;
    }
    
    public void actionPerformed(ActionEvent e) {
      JRadioButton b = (JRadioButton)e.getSource();
      String label = b.getText();
      for (int i=1; i<posStr.length; i++) {
        if (label.equals(posStr[i])) {
          border.setTitlePosition(i);
          panel.revalidate();
          panel.repaint();
          return;
        }    
      }
      for (int i=1; i<jusStr.length; i++) {
        if (label.equals(jusStr[i])) {
          border.setTitleJustification(i);
          panel.revalidate();
          panel.repaint();
          return;
        }    
      }
    }   
  }
 
  public static void main (String args[]) {
    CompTitledPaneExample1 frame = new CompTitledPaneExample1();
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
	System.exit(0);
      }
    });
    frame.setSize(280, 230);
    frame.setVisible(true);
  }
}

CompTitledPane.java:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
    
/**
 * @version 1.0 08/12/99
 */
public class CompTitledPane extends JPanel {
  protected CompTitledBorder border;
  protected JComponent component;
  protected JPanel panel;
  protected boolean transmittingAllowed;
  protected StateTransmitter transmitter;
  
  public CompTitledPane() {
    this(new JLabel("Title"));
    // debug
    // JLabel label = (JLabel)getTitleComponent();
    // label.setOpaque(true);
    // label.setBackground(Color.yellow);
  }
  
  public CompTitledPane(JComponent component) {
    this.component = component;
    border = new CompTitledBorder(component);
    setBorder(border);
    panel = new JPanel();
    setLayout(null);
    add(component);
    add(panel); 
    transmittingAllowed = false;
    transmitter = null;
  }
  
  public JComponent getTitleComponent() {
    return component;
  }
  
  public void setTitleComponent(JComponent newComponent) {
    remove(component);
    add(newComponent);
    border.setTitleComponent(newComponent);
    component = newComponent;
  }
  
  public JPanel getContentPane() {
    return panel;
  }
  
  public void doLayout() {
    Insets insets = getInsets();
    Rectangle rect = getBounds();
    rect.x = 0;
    rect.y = 0;
 
    Rectangle compR = border.getComponentRect(rect,insets);
    component.setBounds(compR);
    rect.x += insets.left;
    rect.y += insets.top;
    rect.width  -= insets.left + insets.right;
    rect.height -= insets.top  + insets.bottom;
    panel.setBounds(rect);   
  }
   
  public void setTransmittingAllowed(boolean enable) {
    transmittingAllowed = enable;
  }
  
  public boolean getTransmittingAllowed() {
    return transmittingAllowed;
  }
  
  public void setTransmitter(StateTransmitter transmitter) {
    this.transmitter = transmitter;
  }
  
  public StateTransmitter getTransmitter() {
    return transmitter;
  }
  
  public void setEnabled(boolean enable) {
    super.setEnabled(enable);
    if (transmittingAllowed && transmitter != null) {
      transmitter.setChildrenEnabled(enable);
    }
  } 
    
}

StateTransmitter.java:

/**
 * @version 1.0 08/12/99
 */
public interface StateTransmitter {

  public void setChildrenEnabled(boolean enable);
  
}

// sample
//
//  public void setChildrenEnabled(boolean enable) {
//    
//    Component[] children = panel.getComponents();
//    for(int i=0; i<children.length; i++) { 
//      System.out.println(" " + i + 
//                         " " + children[i].getClass().getName() +
//                         " " + enable);
//      children[i].setEnabled(enable); 
//    }
//  }
//

CompTitledBorder.java:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
  
/**
 * @version 1.0 08/12/99
 */
public class CompTitledBorder extends TitledBorder { 
  protected JComponent component;
  
  public CompTitledBorder(JComponent component) {
    this(null, component, LEFT, TOP);
  }
  
  public CompTitledBorder(Border border) {
    this(border, null, LEFT, TOP);
  }
  
  public CompTitledBorder(Border border, JComponent component) {
    this(border, component, LEFT, TOP);
  }
    
  public CompTitledBorder(Border     border,
                          JComponent component,
                          int        titleJustification,
                          int        titlePosition)      {
    super(border, null, titleJustification,
                        titlePosition, null, null);
    this.component = component;
    if (border == null) {
      this.border = super.getBorder();
    }
  }
  
  
  public void paintBorder(Component c, Graphics g,
                          int x, int y, int width, int height) {
    Rectangle borderR = new Rectangle(x      +  EDGE_SPACING,
                                      y      +  EDGE_SPACING,
                                      width  - (EDGE_SPACING * 2),
                                      height - (EDGE_SPACING * 2));
    Insets borderInsets;
    if (border != null) {
      borderInsets = border.getBorderInsets(c);
    } else {
      borderInsets = new Insets(0, 0, 0, 0);
    }
    
    Rectangle rect = new Rectangle(x,y,width,height);
    Insets insets = getBorderInsets(c);
    Rectangle compR = getComponentRect(rect, insets);
    int diff;
    switch (titlePosition) {
      case ABOVE_TOP:
        diff = compR.height + TEXT_SPACING;
        borderR.y += diff;
        borderR.height -= diff;
        break;
      case TOP:
      case DEFAULT_POSITION:
        diff = insets.top/2 - borderInsets.top - EDGE_SPACING;
        borderR.y += diff;
        borderR.height -= diff;
        break;
      case BELOW_TOP:
      case ABOVE_BOTTOM:
        break;
      case BOTTOM:
        diff = insets.bottom/2 - borderInsets.bottom - EDGE_SPACING;
        borderR.height -= diff;
        break;
      case BELOW_BOTTOM:
        diff = compR.height + TEXT_SPACING;
        borderR.height -= diff;
        break;
    }
    border.paintBorder(c, g, borderR.x,     borderR.y, 
                             borderR.width, borderR.height);    
    Color col = g.getColor();
    g.setColor(c.getBackground());
    g.fillRect(compR.x, compR.y, compR.width, compR.height);
    g.setColor(col);
    component.repaint();
  }
   
  public Insets getBorderInsets(Component c, Insets insets) {
    Insets borderInsets;
    if (border != null) {
      borderInsets  = border.getBorderInsets(c);
    } else {
      borderInsets  = new Insets(0,0,0,0);
    }
    insets.top    = EDGE_SPACING + TEXT_SPACING + borderInsets.top;
    insets.right  = EDGE_SPACING + TEXT_SPACING + borderInsets.right;
    insets.bottom = EDGE_SPACING + TEXT_SPACING + borderInsets.bottom;
    insets.left   = EDGE_SPACING + TEXT_SPACING + borderInsets.left;
 
    if (c == null || component == null) {
      return insets;
    }
 
    int compHeight = 0;
    if (component != null) {
      compHeight = component.getPreferredSize().height;
    }
 
    switch (titlePosition) {
      case ABOVE_TOP:
        insets.top    += compHeight + TEXT_SPACING;
        break;
      case TOP:
      case DEFAULT_POSITION:
        insets.top    += Math.max(compHeight,borderInsets.top) - borderInsets.top;
        break;
      case BELOW_TOP:
        insets.top    += compHeight + TEXT_SPACING;
        break;
      case ABOVE_BOTTOM:
        insets.bottom += compHeight + TEXT_SPACING;
        break;
      case BOTTOM:
        insets.bottom += Math.max(compHeight,borderInsets.bottom) - borderInsets.bottom;
        break;
      case BELOW_BOTTOM:
        insets.bottom += compHeight + TEXT_SPACING;
        break;
    }
    return insets;
  }
  
  public JComponent getTitleComponent() {
    return component;
  }
  
  public void setTitleComponent(JComponent component) {
    this.component = component;
  }
  
  
  public Rectangle getComponentRect(Rectangle rect,Insets borderInsets) {
    Dimension compD = component.getPreferredSize();
    Rectangle compR = new Rectangle(0,0,compD.width,compD.height);
    switch (titlePosition) {
      case ABOVE_TOP:
        compR.y = EDGE_SPACING;
        break;
      case TOP:
      case DEFAULT_POSITION:
        compR.y = EDGE_SPACING + 
                 (borderInsets.top -EDGE_SPACING -TEXT_SPACING -compD.height)/2;
        break;
      case BELOW_TOP:
        compR.y = borderInsets.top - compD.height - TEXT_SPACING;
        break;
      case ABOVE_BOTTOM:
        compR.y = rect.height - borderInsets.bottom + TEXT_SPACING;
        break;
      case BOTTOM:
        compR.y = rect.height - borderInsets.bottom + TEXT_SPACING +
                 (borderInsets.bottom -EDGE_SPACING -TEXT_SPACING -compD.height)/2;
        break;
      case BELOW_BOTTOM:
        compR.y = rect.height - compD.height - EDGE_SPACING;
        break;
    }
    switch (titleJustification) {
      case LEFT:
      case DEFAULT_JUSTIFICATION:
        compR.x = TEXT_INSET_H + borderInsets.left;
        break;
      case RIGHT:
        compR.x = rect.width - borderInsets.right -TEXT_INSET_H -compR.width;
        break;
      case CENTER:
        compR.x = (rect.width - compR.width) / 2;
        break;
    }
    return compR;
  }
 
}

Disabling the maximize-icon in a JFrame

First thing you can try is changing the resizable property from the Frame class:

 
import javax.swing.*;
import java.awt.*;
  
public class DisableMax extends JFrame {
 
   public DisableMax() {
      setSize(100, 100);

      setResizable(false);
   }
 
   public static void main(String args[]) {
      new DisableMax().setVisible(true);     
   }
}

However, in this example the maximize icons will still be there. Moreover, the frame will not be resizable anymore (might not be the functionality you want!).
Extend from a JDialog instead and your window will only have the X icon visible.

 
import javax.swing.*;
import java.awt.*;
   
public class DisableMax2 extends JDialog {
 
   public DisableMax2() {
      setSize(100, 100);
 
      setResizable(false);
   }
 
   public static void main(String args[]) {
      new DisableMax2().setVisible(true);     
   }
}

Force the JScrollPane to scroll to the next JTextField when I press the TAB-Key

There are three steps to this:

1. Listen for focus events coming from the components that you want to scroll to. (This works for most component classes, not just JTextField.)

2. In the event listener, find the location of the component that now has focus with getBounds().

3. Ask the scrolled component to make that location visible with scrollRectToVisible(). NOTE! The obvious thing to call scrollRectToVisible on is the JScrollPane, which will compile fine but won’t do what you want. You must call scrollRectToVisible on the object contained in viewport of the scrollpane.

This Forte-generated example shows how this works for a simple panel containing a number of JTextFields. Notice that scrollRectToVisible is called on the JPanel containing the text fields.

TestFocus.java:

public class TestFocus extends javax.swing.JFrame {
 
    /** Creates new form TestFocus */
    public TestFocus() {
        initComponents();
    }
 
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {
        scrollPane = new javax.swing.JScrollPane();
        panel = new javax.swing.JPanel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
         
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });
        
        scrollPane.setPreferredSize(new java.awt.Dimension(120, 80));
        panel.setLayout(new java.awt.GridLayout(0, 1, 0, 15));
        
        jTextField1.setFont(new java.awt.Font("Dialog", 0, 18));
        jTextField1.setText("jTextField1");
        jTextField1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextFieldFocusGained(evt);
            }
        });
         
        panel.add(jTextField1);
        
        jTextField2.setFont(new java.awt.Font("Dialog", 0, 18));
        jTextField2.setText("jTextField2");
        jTextField2.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextFieldFocusGained(evt);
            }
        });
        
        panel.add(jTextField2);
        
        jTextField3.setFont(new java.awt.Font("Dialog", 0, 18));
        jTextField3.setText("jTextField3");
        jTextField3.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextFieldFocusGained(evt);
            }
        });
        
        panel.add(jTextField3);
        
        jTextField4.setFont(new java.awt.Font("Dialog", 0, 18));
        jTextField4.setText("jTextField4");
        jTextField4.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextFieldFocusGained(evt);
            }
        });
        
        panel.add(jTextField4);
        
        scrollPane.setViewportView(panel);
        
        getContentPane().add(scrollPane, java.awt.BorderLayout.CENTER);
        
        pack();
    }
 
    private void jTextFieldFocusGained(java.awt.event.FocusEvent evt) {
        java.awt.Component focusedComponent = evt.getComponent();
        panel.scrollRectToVisible(focusedComponent.getBounds(null));
        repaint();
    }
 
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
 
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        new TestFocus().show();
    }
 
    // Variables declaration - do not modify
    private javax.swing.JScrollPane scrollPane;
    private javax.swing.JPanel panel;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    // End of variables declaration

}

If your panel of components contains a JTextArea, or another JTextComponent subclass, then be aware that those components will absorb TABs into themselves instead of allowing the TAB to change focus. You can disable this behavior by creating a subclass that overrides isManagingFocus() to always return false.

Creating a JToolBar with a drop-down menu as one of the items

Main.java:

import javax.swing.border.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
  
public class Main extends JFrame {
   public Main() {
      super("JToolBar example");
 
      try {
         UIManager.setLookAndFeel(
   	    "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
      }
      catch(Exception e) { }
 
      final JTextArea textArea = new JTextArea(5, 30);
      JToolBar toolBar = new JToolBar();
      toolBar.setLayout(new FlowLayout(FlowLayout.LEFT));
 
      JButton actionButton = new JButton("Back", new ImageIcon("backdis.gif"));
      actionButton.setRolloverIcon(new ImageIcon("backen.gif"));
      actionButton.setRolloverEnabled(true);
 
      JPopupMenu popupMenu = new JPopupMenu();
      popupMenu.add("JToolBar (Java 2 Platform SE v1.4.0)");
      popupMenu.add("Overview (Java 2 Platform SE v1.4.0)");
      popupMenu.add("Google");
 
      DropDownButton dropDownButton = new DropDownButton(actionButton, popupMenu);
 
      toolBar.add(dropDownButton);
      toolBar.add(new JButton("Forward"));
      toolBar.add(new JButton("Stop"));
      toolBar.add(new JButton("Home"));
      toolBar.setRollover(true);
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            System.exit(0);
         }
      });
 
      getContentPane().setLayout(new BorderLayout());
      getContentPane().add(BorderLayout.NORTH, toolBar);
      getContentPane().add(BorderLayout.CENTER, new JScrollPane(textArea));
   }
 
   public static void main(String[] args) {
      Main main = new Main();
      main.setSize(300, 300);
      main.setVisible(true);
   }
}
 
class DropDownButton extends AbstractButton
{
   JButton actionButton;
   JToggleButton menuButton;
   JPopupMenu popupMenu;
 
   public DropDownButton(JButton _actionButton, JPopupMenu _popupMenu) {
      this.popupMenu = _popupMenu;
      this.actionButton = _actionButton;
 
      setLayout(new BorderLayout());
      actionButton.setBorderPainted(false);
      add(BorderLayout.CENTER, actionButton);
      menuButton = new JToggleButton(new ImageIcon("down.jpg"));
      menuButton.setPreferredSize(new Dimension(15, 10));
      add(BorderLayout.EAST, menuButton);
      menuButton.setBorderPainted(false);
  
      MouseAdapter ma = new MouseAdapter() {
         public void mouseClicked(MouseEvent me) { }
         public void mousePressed(MouseEvent me) { 
            if (me.getSource() == actionButton) {
               menuButton.setSelected(true);
            }
         }
         public void mouseReleased(MouseEvent me) {
            if (me.getSource() == actionButton) {
               menuButton.setSelected(false);
            }
         }
         public void mouseEntered(MouseEvent me) { 
            setRolloverBorder(); 
         }
         public void mouseExited(MouseEvent me) { 
            unsetRolloverBorder();
         }
      };
 
      actionButton.addMouseListener(ma);
      menuButton.addMouseListener(ma);
 
      menuButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            popupMenu.show(actionButton, 0, actionButton.getSize().height);
         }
      });
   }  
 
   protected void setRolloverBorder() {
      actionButton.setBorderPainted(true);
      menuButton.setBorderPainted(true);
   }
 
   protected void unsetRolloverBorder() {
      actionButton.setBorderPainted(false);
      menuButton.setBorderPainted(false);
   }
}

Images used:

backdis.gif:
backen.gif:
down.jpg: