Java –Swing

 

Creating  Windows & Applets

 

(lectures programs)

 

OnLine Documentations

 

Swing Tutorial

 


 

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");
 

TextFields

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

TextAreas

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

CheckBoxs

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

RadioButtons

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

ComboBoxes

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();

 

FlowLayout

    
   cp.setLayout(new FlowLayout());

 

BorderLayout

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

 

GridLayout

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

 

BoxLayout

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:

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

import   com.bruceeckel.swing.*;

Make sure to modify CLASSPATH
e.g.,  in file
.cshrc in UNIX,  to include where this package is
e.g.,  in UNIX /home/cs476/java/ThinkingInJava.2ndEditionR12/code

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 the code of main or any other class:
 
replace:     System.out.println("string");
 
with:        Xout.append("string");         
  }
 
 class A {}
 class B {}
 
 public static void main(String[] args) {
 
    Console.run(new FileName(), 350, 250);
 
 }
}
 
To execute the program:
 
% java FileName
 
% appletviewer FileName.java
 
Alternatively: 
 
            Create a file called FileName.html that contains: 
 
<applet code=FileName width=600 height=250> </applet>
  
 Then use:
     % appletviewer FileName.html
 Or:
     open  FileName.html from within a browser
 
Example: 
 
Application: allother/SimpleThread.java 
 
Applet (and also application): applets/SimpleThread.java 
 
html:  applets/SimpleThreadFrame.html 
 
The content of this file is:
 
<applet code=SimpleThread width=600 height=250> </applet>