|
In This Issue Welcome to the Java Technology Fundamentals Newsletter This monthly newsletter provides a way for you to learn the basics of the Java programming language, discover new resources, and keep up-to-date on the latest additions to Sun Developer Network's New to Java Center. Note: For the code in this issue of Fundamentals to compile, you need to use the JDK 5.0 software. This issue covers: Java Programming Language Basics
The
CardLayout Manager and JTabbedPaneIn this article, you'll take a look at the The
The following program demonstrates the use of
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutTest {
public static void main(String args[]) {
Runnable runner = new Runnable() {
public void run() {
final JFrame frame = new JFrame("Card Layout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final CardLayout layout = new CardLayout();
frame.setLayout(layout);
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
layout.next(frame.getContentPane());
}
};
for (int i=0; i<5; i++) {
String label = "Card " + i;
JButton button = new JButton(label);
frame.add(button, label);
button.addActionListener(listener);
}
frame.setSize(300, 200);
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
Notice the
To demonstrate the ability to cycle through the cards, this program
was made to listen for action from any of the buttons; the next card
is displayed whenever you click on one of the buttons. The
You probably expected to see a full-fledged rendering of the five
different cards, as in Figure 1, including the clickable tabs to
select any one of the cards. Unfortunately,
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TabbedPaneTest {
public static void main(String args[]) {
Runnable runner = new Runnable() {
public void run() {
JFrame frame = new JFrame("JTabbedPane");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane jtp = new JTabbedPane();
frame.add(jtp, BorderLayout.CENTER);
for (int i=0; i<5; i++) {
JButton button = new JButton("Card " + i);
jtp.add("Btn " + i, button);
}
frame.setSize(300, 200);
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
Each card is added with the
The
You can also move the tabs to different sides of the container with
One other option says what to do with the tabs when they are too
wide for one line. Using
Keep in mind that what goes on each tab is just one component. So if
you want to create a tab like that in Figure 1, you'd have to place
the components within their own container (
Vocal Java
Several years back, I configured a blind gentleman's Microsoft- Windows-based computer to vocally identify the window under the mouse pointer. As he moved the pointer around the screen, the computer spoke the name of the underlying window. I have never forgotten how beneficial that speaking computer was to that gentleman's life. My earlier work on configuring a Windows-based computer to speak inspired me to create an equivalent assistive technology for use in Java technology contexts. This technology transparently helps blind users interact with Swing-based graphical user interfaces (GUIs). It also can be used with Abstract Windowing Toolkit- or AWT-based GUIs, provided that those GUIs are made accessible to the technology. This article introduces my Speaker assistive technology. Because Speaker depends on Sun's Java Speech API -- Sun's preferred choice for supporting various speech technologies on any Java platform -- and FreeTTS -- my preferred Java Speech implementation for making Speaker speak -- the article first reviews those technologies. I developed and tested this article's code with Sun's J2SE 5.0 SDK and FreeTTS 1.2.1. Windows 98 SE was the underlying platform.
Java Speech API Overview
The Java Speech API is a specification that describes a standard set of classes and interfaces for integrating speech technologies into Java software. Sun released version 1.0 (the only version to date) of this specification on October 26, 1998. It is important to keep in mind that Java Speech is only a specification -- no implementation is included. Java Speech supports two kinds of speech technologies: speech recognition and speech synthesis. Speech recognition converts speech to text. Special input devices called recognizers make speech recognition possible. In contrast, speech synthesis converts text to speech. Special output devices called synthesizers make speech synthesis possible.
The
Creating a Sorted JList Component
My personal digital assistant (PDA) provides contact information for friends, family, and coworkers. The PDA allows me to find a person's phone number easily by searching on that person's name. The information is sorted according to standard dictionary-sort order for the contact's name. That is, the name Michele comes before Robert in the list. Without this sort order, I would have difficulty finding names quickly and easily.
Java technology programmers often use the javax.swing.JList
component to provide list views of similar data, whether it be a
phone contact list or a grocery list. Despite the convenience of
this user interface (UI) component, a JList doesn't sort its
elements. It displays them in the same order provided by its
underlying
This article describes how to produce sorted lists and uses a simple
application to demonstrate concepts. You can download all the demo
source code using the link at the end of this article. Although I
developed the demo source as a NetBeans IDE 5.0 project, the demo's
ANT script does not require that you use that IDE to compile or
execute the application. The demo application uses the decorator
design pattern to provide additional functionality to the The content of the tab of a See possible solution:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class TabSelection {
static Color colors[] = {Color.RED, Color.ORANGE, Color.YELLOW,
Color.GREEN, Color.BLUE, Color.MAGENTA};
static String titles[] = {"Red", "Orange", "Yellow", "Green",
"Blue", "Magenta"};
static int mnemonic[] =
{KeyEvent.VK_R, KeyEvent.VK_O, KeyEvent.VK_Y,
KeyEvent.VK_G, KeyEvent.VK_B, KeyEvent.VK_M};
static void add(
JTabbedPane tabbedPane, String label, int mnemonic) {
int count = tabbedPane.getTabCount();
JButton button = new JButton(label);
button.setBackground(colors[count]);
tabbedPane.addTab(label,
new DiamondIcon(colors[count]), button, label);
tabbedPane.setMnemonicAt(count, mnemonic);
}
public static void main(final String args[]) {
Runnable runner = new Runnable() {
public void run() {
JFrame frame = new JFrame("Tab Selection");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.setTabLayoutPolicy(
JTabbedPane.SCROLL_TAB_LAYOUT);
for (int i=0, n=titles.length; i<n; i++) {
add(tabbedPane, titles[i], mnemonic[i]);
}
ChangeListener changeListener = new ChangeListener() {
public void stateChanged(ChangeEvent changeEvent) {
JTabbedPane sourceTabbedPane =
(JTabbedPane)changeEvent.getSource();
int index = sourceTabbedPane.getSelectedIndex();
System.out.println ("Tab changed to: "
+ sourceTabbedPane.getTitleAt(index));
}
};
tabbedPane.addChangeListener(changeListener);
frame.add(tabbedPane, BorderLayout.CENTER);
frame.setSize(400, 150);
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
import javax.swing.*;
import java.awt.*;
public class DiamondIcon implements Icon {
private Color color;
private boolean selected;
private int width;
private int height;
private Polygon poly;
private static final int DEFAULT_WIDTH = 10;
private static final int DEFAULT_HEIGHT = 10;
public DiamondIcon(Color color) {
this (color, true, DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
public DiamondIcon(Color color, boolean selected) {
this (color, selected, DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
public DiamondIcon (Color color, boolean selected,
int width, int height) {
this.color = color;
this.selected = selected;
this.width = width;
this.height = height;
initPolygon();
}
private void initPolygon() {
poly = new Polygon();
int halfWidth = width/2;
int halfHeight = height/2;
poly.addPoint (0, halfHeight);
poly.addPoint (halfWidth, 0);
poly.addPoint (width, halfHeight);
poly.addPoint (halfWidth, height);
}
public int getIconHeight() {
return height;
}
public int getIconWidth() {
return width;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor (color);
g.translate (x, y);
if (selected) {
g.fillPolygon (poly);
} else {
g.drawPolygon (poly);
}
g.translate (-x, -y);
}
}
Blending Images and Text on a Web Page in Sun Java Studio Creator 2
For application web pages you create in the Sun Java Studio Creator 2 IDE (or the Sun Java Studio Creator 2 IDE, Update 1), you might want to have background images on which you superimpose some text. However, when you run the application, although the background images give the page a nice look, it's often the case that the text on top of the images is hard to read or even lost. This tech tip shows you some tricks for getting around the problem of text vanishing within a background image. For example, you'll learn how to insert translucent masks between the text and the background so that the text stands out. To get you there, the tip covers the following:
This tutorial is intended as an introduction to using the built-in Concurrent Versions System (CVS) module in NetBeans IDE 5.0. This tutorial assumes no familiarity with the CVS modules from previous IDE releases and will not address differences between the CVS support in version 5.0 and previous versions of the NetBeans IDE. This tutorial assumes that you have access to a CVS repository. Expected duration: 30 minutes
Introduction
In this tutorial, you will create a new project based on your CVS repository and explore how to use the CVS tools available in NetBeans IDE 5.0.
Prerequisites
This tutorial assumes you have a basic familiarity with the NetBeans IDE 5.0 and that you have access to a CVS repository. We will use this IDE's sources repository as an example.
Software Needed for the Tutorial
Before you begin, install the following software on your computer:
Notations Used in the Tutorial
<NETBEANS_HOME> -- the NetBeans IDE installation directory <PROJECT_HOME> -- directory that contains the project you create Tutorial Exercises
Read the tutorial and do the exercises
Java Programming Language Workshop (VC-SL-285)
The Java Programming Language Workshop course provides students with practical experience in designing a vertical solution for a distributed, multitier application. Students use graphical user interface (GUI) design principles and network communications capabilities to code a functional Java technology application that interacts with a networked database server. The significant amount of lab time illustrates the workshop nature of this course.
Format
This course is presented using Sun's web-based Live Virtual Class (LVC). The LVC is a dynamic and fully interactive online learning environment that features live teaching collaboration and instructor-assisted activities. Need help with a programming problem? The Sun Developer Expert Assistance program offers the following:
Ask a question about any aspect of the supported products and technologies shown, and an engineer will respond to you by email within 24 hours, either with an answer or a commitment to track down the information.
Supported Products and Technologies
For most Java technology development, you need the class libraries, compiler, tools, and runtime environment provided with the Java SE development kit. Reader Feedback Manage your current newsletter subscriptions: You can subscribe to these and other publications on the Newsletters and Publications page.
Join Sun Developer Network to get your free tools!
Sun is offering the award-winning Sun Java Studio Enterprise and Sun Java Studio Creator IDEs at no cost to all developers worldwide who join Sun Developer Network (SDN). | ||||||||||||||||||||
|
| ||||||||||||