Java –Swing

Creating  Windows & Applets

(lectures programs)

OnLine Documentations

 

Buttons   Show a variety of buttons.

JButton jb = new JButton("JButton");

BasicArrowButton up = new BasicArrowButton(BasicArrowButton.NORTH),

 

      To run, we may use one of the following methods:

% appletviewer  Buttons.java

% appletviewer  ButtonsFrame.html

% java   Buttons

     Also use any  browser  and open:  ButtonsFrame.html


BASIC COMPONENTS

                                                                                           

Buttons and Lables 

JButton     but = new JButton("Button 1");
but.setBackground(Color.red);
but.setEnabled(false);
 
ActionListener AL = new ActionListener() {...}
but.addActionListener(AL);
 
JLabel     lab = new JLabel(“This is a Lebel”);
lab.setText("Hello");
 

Text Fields

JTextField     txt = new JTextField(30);
String s = new String();
s = txt.getText();
s = txt.getSelectedText();
txt.setText(" ");
 

Text Areas

JTextArea tar = new JTextArea(5, 20);
tar.setText(tar.getText()+ "Old Dominion University \n");
 

Check Boxs

JCheckBox     cb1 = new JCheckBox("Check Box 1");
if(cb1.isSelected()) ..
 

Radio Buttons

ButtonGroup grp = new ButtonGroup();
JRadioButton     rb1 = new JRadioButton("one", false);
grb.add(rb1);
 

Combo Boxes

JComboBox cbox = new JComboBox();
cbox.addItem(description[count++]);
cbox.getSelectedIndex();
cbox.getSelectedItem());
 

Lists

DefaultListModel ListItems=new DefaultListModel();
JList List = new JList(ListItems);
 
ListItems.addElement(flavors[count++]);
Object[] items=List.getSelectedValues();
 
ListSelectionListener LL =  new ListSelectionListener() {...}
List.addListSelectionListener(LL);
 

Borders

JPanel jp = new JPanel();
Border brd = new TitledBorder("Title");
// or new EtchedBorder();
// or new LineBorder(Color.blue);
// or …
jp.setBorder(brd);

 

CONTROLLING  LAYOUT 

 

Container cp = getContentPane();

 

Flow Layout

cp.setLayout(new FlowLayout());

 

Border Layout

cp.add(BorderLayout.NORTH, new JButton("North"));

 

Grid Layout

cp.setLayout(new GridLayout(7,3));

 

Box Layout

Box bh = Box.createHorizontalBox();
Box bv = Box.createVerticalBox();
 
bh.add(...);
cp.add(bh);

COMPOUND COMPONENTS

 

Dailog Boxes

JOptionPane.showMessageDialog(null, "There's a bug on you!", 
    "Message(Alert)!", JOptionPane.ERROR_MESSAGE);
 
int answer = JOptionPane.showConfirmDialog(null, 
   "Quit? ", "Confirm(Yes/NO”", JOptionPane.YES_NO_OPTION);
 
int sel = JOptionPane.showOptionDialog(null, "Choose a Color!",
    "Option(Color)", JOptionPane.DEFAULT_OPTION, 
     JOptionPane.WARNING_MESSAGE, null, options, options[0]);
 
String val = JOptionPane.showInputDialog("How many fingers do you see?"); 
 

Menus

JMenuBar mbar = new JMenuBar();
JMenu    menu = new JMenu("Flavors");
JMenuItem item = new JMenuItem(flavors[i]);
menu.add(item);
mbar.add(menu);
setJMenuBar(mbar);
 

Popup

JPopupMenu popup = new JPopupMenu();
JMenuItem item = new JMenuItem(flavors[i]);
popup.add(item);
. . .
popup.show(. . .); 

 

Converting Applications  to  Applets

Application:

class A {}
class B {}
public class FileName { 
  public static void main(String[] args) {
  <main code>
}

Applete:

Create a file called: FileName.html:

<applet code = FileName width=600 height=250> </applet>

Modify FileName.java:

public class  FileName extends JApplet {
  JTextArea  Xout  = new JTextArea (10, 30);
  public void init() {  
       Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(new JLabel("FileName"));
cp.add(new JScrollPane(Xout)); 
 
     <main code>
 
Anywhere inside  <main code> or any other class:
replace:     System.out.println("string");
with:        Xout.append("string");         
  }
  class A {}
  class B {}
}
 
To execute the program:
 
% appletviewer FileName.html
Or  
open  FileName.html from within a browser
 
Example 1: 
 
     Application: allother/Hello.java 
Applet:    applets/Hello.java 
 html:     applets/Hello.html 
 
 
Example 2: 
 
Application: allother/Shapes1.java 
     Applet:    applets/Shapes1.java 
      html:     applets/Shapes1.html