You are on page 1of 17

Swing

(Progress Indicators ,component


organizers ,text components)
Progress Indicators purpose

 Sometimes a task running within a program


might take a while to complete. A user-
friendly program provides some indication to
the user that the task is occurring, how long
the task might take, and how much work has
already been done. One way of indicating
work, and perhaps the amount of progress, is
to use an animated image.
Progress Indicators

 Classes for indicating the progress of a slow activity.


 Three classes are available
1. JProgressBar
2. ProgressMonitor
3. ProgressMonitorInputStream
Progress Bars

 A progress bar is a simple component – just a


reactangle that is partially filled with color to
indicate the progress of an operation.
 By default the progress is indicated by a string
“%n”.
 Example:
Construct progress bar

 By using JProgressBar
 Supply the minimum and maximum value.
 progressBar = new JProgressBar(0, 100);
 progressBar.setStringPainted(true) :-progress bar
computes the completion percentage and displays a
string “%n”.
 For displaying a different string,supply it with the
setString method Ex
if(progressBar.getValue()>900)
progressBar.setString(“almost Done”);
Progress Monitors

 A progress bar is simple component that can be


placed inside a window. In contrast, a progress
monitor is a complete dialog box that contains a
progress bar.
 It also contains a Cancel button. By clicking it
monitor dialog box will be closed.
Construct progress monitor

 progressMonitor = new ProgressMonitor(parent


component, "Running a Long Task", "", minValue,
maxValue);
 The first argument provides the parent component to the
dialog popped up by the progress monitor.
 The second argument is a string that describes the
nature of the task being monitored.
 The third argument is an optional note to display below
the object.
 Finally min and max values
Swing Text Components
Demo example
Text Controls:

 JTextField ,JPasswordField ,JFormattedTextField : Also


known simply as text fields, text controls can
display only one line of editable text.
 Use them to get a small amount of textual
information from the user and perform an action
after the text entry is complete.
Plain Text Areas

 JTextArea : can display multiple lines of


editable text. Although a text area can
display text in any font, all of the text is in the
same font.
 Use a text area to allow the user to enter
unformatted text of any length or to display
unformatted help information.
Styled Text Areas

 A styled text component can display editable


text using more than one font.
 Some styled text components allow
embedded images and even embedded
components.
Component Organizers

 Helps organize other components.


1. Split pane
2. Tabbed pane
Split pane

 A mechanism for splitting an area into multiple


parts with boundaries that can be adjusted.
 Split panes split a component into two parts
 Split pane is constructed by specifying the
orientation followed by the two components. For
ex,
JSplitPane innerpane=new
JSplitPane(JSplitPane.HORIZONTAL_SPLIT,firsttcomponent,second
Component)
Split pane demo
Tabbed pane

 With the JTabbedPane class, you can have several


components, such as panels, share the same space.
 The user chooses which component to view by
selecting the tab corresponding to the desired
component
 JTabbedPane tabbedPane = new JTabbedPane();
 ImageIcon icon = createImageIcon("images/middle.gif");
 JComponent panel1 = makeTextPanel("Panel #1");
 tabbedPane.addTab("Tab 1", icon, panel1,
"Does nothing");

You might also like