swing. split panes jsplitpane is used to divide two (and only two) components. the two components...

28
SWING

Upload: esmond-singleton

Post on 29-Dec-2015

223 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: SWING. Split Panes JSplitPane is used to divide two (and only two) Components. The two Components are graphically divided based on the look and feel implementation,

SWING

Page 2: SWING. Split Panes JSplitPane is used to divide two (and only two) Components. The two Components are graphically divided based on the look and feel implementation,

Split Panes JSplitPane is used to divide two (and only two) Components. The two Components are graphically divided based on the look and feel

implementation, and the two Components can then be interactively resized by the user.

The total display area does not change. This gives applications a more modern and sophisticated view.

A JSplitPane displays two components, either side by side or one on top of the other.

It the user to dynamically change the size of two or more components displayed side-by-side.

Methods-

void setOrientation(int) / int getOrientation() -Set or get the split pane's orientation. Use either HORIZONTAL_SPLIT or VERTICAL_SPLIT defined in JSplitPane. If left unspecified, the split pane will be horizontally split.

The divider between the side components represents the only visible part of JSplitPane. It's size can be managed with the setDividerSize() method, and its position by the overloaded setDividerLocation() methods.

Has TouchExpandable property which, when true, places two small arrows inside the divider that will move the divider to its extremities when clicked.

Page 3: SWING. Split Panes JSplitPane is used to divide two (and only two) Components. The two Components are graphically divided based on the look and feel implementation,
Page 4: SWING. Split Panes JSplitPane is used to divide two (and only two) Components. The two Components are graphically divided based on the look and feel implementation,

EXAMPLE-1 import java.awt.*; import javax.swing.*;

public class splitpane { public static void main(String args[]) { JFrame frame = new JFrame("JSplitPane Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JButton leftComponent = new JButton("left"); JButton rightComponent = new JButton("right");

JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,leftComponent,

rightComponent); frame.getContentPane().add(splitPane, BorderLayout.CENTER); frame.setSize(300, 200); frame.setVisible(true); } }

Page 5: SWING. Split Panes JSplitPane is used to divide two (and only two) Components. The two Components are graphically divided based on the look and feel implementation,

Example-2 import java.awt.*; import javax.swing.*; public class splitpane2 { public static void main(String args[]) { JFrame frame = new JFrame("JSplitPane Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JButton top = new JButton("TOP"); JButton bottom = new JButton("BOTTOM");

JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,top, bottom);

frame.getContentPane().add(splitPane, BorderLayout.CENTER); frame.setSize(300, 200); frame.setVisible(true); } }

Page 6: SWING. Split Panes JSplitPane is used to divide two (and only two) Components. The two Components are graphically divided based on the look and feel implementation,

import java.awt.*; import javax.swing.*; public class splitpane3 extends JFrame {

static String sometext = "This is a simple text string that is long enough " + "to wrap over a few lines in the simple demo we're about to build. " + "We'll put two text areas side by side in a split pane.";

public splitpane3() { super("Simple SplitPane Frame"); setSize(450, 200); setDefaultCloseOperation(EXIT_ON_CLOSE); JTextArea jt1 = new JTextArea(sometext); JTextArea jt2 = new JTextArea(sometext); jt1.setLineWrap(true); jt2.setLineWrap(true); jt1.setMinimumSize(new Dimension(150, 150)); jt2.setMinimumSize(new Dimension(150, 150)); jt1.setPreferredSize(new Dimension(250, 200)); JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jt1, jt2); getContentPane().add(sp, BorderLayout.CENTER); } public static void main(String args[]) { splitpane3 ssb = new splitpane3(); ssb.setVisible(true); } }

Page 7: SWING. Split Panes JSplitPane is used to divide two (and only two) Components. The two Components are graphically divided based on the look and feel implementation,

How to Use Tabbed Panes A tabbed pane is a component that appears as a group of folders/tab in a file

cabinet. Each folder/tab has a title.

The user chooses which component to view by selecting the tab corresponding to the desired component.

Only one of the tab may be selected at a time. With the JTabbedPaneclass, you can have several components, such as

panels, share the same space.

Created using a default constructor.

Tabs are defined by void addTab (String str, Component comp)

Steps to use a tabbed pane in an applet

1. Create a JTabbedPane object.

2. Call addTab() to add tab to the pane.

3. Repeat the step 2 for each tab.

4. Add the tabbed pane to the content pane of the applet.

Page 8: SWING. Split Panes JSplitPane is used to divide two (and only two) Components. The two Components are graphically divided based on the look and feel implementation,
Page 9: SWING. Split Panes JSplitPane is used to divide two (and only two) Components. The two Components are graphically divided based on the look and feel implementation,

import javax.swing.*;

public class jtabbedpane extends JApplet { public void init() { JTabbedPane jtp = new JTabbedPane(); jtp.addTab("Fruits", new fruitpanel()); jtp.addTab("Colors", new colorpanel()); jtp.addTab("Cities", new citypanel()); getContentPane().add(jtp); } } class fruitpanel extends JPanel { public fruitpanel() { JButton b1= new JButton("Apple"); add(b1); JButton b2= new JButton("Grapes"); add(b2); JButton b3= new JButton("Peach"); add(b3); } }

Page 10: SWING. Split Panes JSplitPane is used to divide two (and only two) Components. The two Components are graphically divided based on the look and feel implementation,

class colorpanel extends JPanel { public colorpanel() { JCheckBox cb1 = new JCheckBox("Red"); add(cb1); JCheckBox cb2 = new JCheckBox("Blue"); add(cb2); JCheckBox cb3 = new JCheckBox("Green"); add(cb3); } } class citypanel extends JPanel { public citypanel() { JComboBox jcb = new JComboBox(); jcb.addItem("Delhi"); jcb.addItem("London"); jcb.addItem("Paris"); add(jcb); } }

Page 11: SWING. Split Panes JSplitPane is used to divide two (and only two) Components. The two Components are graphically divided based on the look and feel implementation,

Scroll Pane A Scroll Pane is a component that presents a rectangular area in which a

component may be viewed. Horizontal and/or vertical scroll bars may be provided if necessary. Scroll Panes are implemented in Swing by the JScrollPane class, which extends

JComponent. Constructors:JScrollPane (Component comp)JScrollPane (int vsb, int hsb)JScrollPane (Component comp, int vsb, int hsb) vsb and hsb constants are defined by the ScrollPaneConstants interface.

Constants DescriptionHORIZONTAL_SCROLLBAR_ALWAYS Always Provide horizontal scroll bar.HORIZONTAL_SCROLLBAR_AS_NEEDED Provide horizontal scroll bar, if needed.VERTICAL_SCROLLBAR_ALWAYS Always Provide vertical scroll bar.VERTICAL_SCROLLBAR_AS_NEEDED Provide horizontal scroll bar, if needed.

Steps to use scroll pane – 1. Create a JComponent object.2. Create a JScrollPane object.3. Add the scroll pane to the content pane of the applet.

Page 12: SWING. Split Panes JSplitPane is used to divide two (and only two) Components. The two Components are graphically divided based on the look and feel implementation,
Page 13: SWING. Split Panes JSplitPane is used to divide two (and only two) Components. The two Components are graphically divided based on the look and feel implementation,

import java.awt.*; import javax.swing.*;

public class scrollpanedemo extends JFrame {

JScrollPane scrollpane;

public scrollpanedemo() { super("JScrollPane Demonstration"); setSize(300, 200); setDefaultCloseOperation(EXIT_ON_CLOSE); init(); setVisible(true); }

public void init() { JRadioButton form[][] = new JRadioButton[7][5]; String counts[] = { "", "0-1", "2-5", "6-10", "11-100", "101+" }; String categories[] = { "Household", "Office", "Extended Family", "Company", "Team", "Birthday Card List", "High School" };

Page 14: SWING. Split Panes JSplitPane is used to divide two (and only two) Components. The two Components are graphically divided based on the look and feel implementation,

JPanel p = new JPanel(); p.setSize(300, 400); p.setLayout(new GridLayout(8, 6, 10, 0)); for (int row = 0; row < 8; row++) { ButtonGroup bg = new ButtonGroup(); for (int col = 0; col < 6; col++) { if (row == 0) { p.add(new JLabel(counts[col])); } else { if (col == 0) { p.add(new JLabel(categories[row - 1])); } else { form[row - 1][col - 1] = new JRadioButton(); bg.add(form[row - 1][col - 1]); p.add(form[row - 1][col - 1]); } } } } scrollpane = new JScrollPane(p); getContentPane().add(scrollpane, BorderLayout.CENTER); } public static void main(String args[]) { new scrollpanedemo(); } }

Page 15: SWING. Split Panes JSplitPane is used to divide two (and only two) Components. The two Components are graphically divided based on the look and feel implementation,

Layered Panes JLayeredPane is one of the most powerful and robust components in the

Swing package. class javax.swing.JLayeredPane

It is a container with a practically infinite number of layers in which components can reside.

Not only is there no limit to the number or type of components in each layer, but components can overlap one another.

A layered pane is a Swing container that provides a third dimension for positioning components: depth, also known as Z order.

When adding a component to a layered pane, specify its depth as an integer.

The higher the number, closer the component is to the "top" position within the container.

If components overlap, the "closer" components are drawn on top of components at a lower depth.

Page 16: SWING. Split Panes JSplitPane is used to divide two (and only two) Components. The two Components are graphically divided based on the look and feel implementation,
Page 17: SWING. Split Panes JSplitPane is used to divide two (and only two) Components. The two Components are graphically divided based on the look and feel implementation,

DEFAULT_LAYER The standard layer, where most components

go. This the bottommost layer.

PALETTE_LAYER The palette layer sits over the default layer.

Useful for floating toolbars so they can be positioned above other components.

MODAL_LAYER The layer used for modal dialogs. They will

appear on top of any toolbars, standard components in the container.

POPUP_LAYER The popup layer displays above dialogs.

That way, the popup windows associated with combo boxes, tooltips, and other help text will appear above the component, or dialog that generated them.

DRAG_LAYER When dragging a component, reassigning it

to the drag layer ensures that it is positioned over every other component in the container. When finished dragging, it can be reassigned to its normal layer.

Page 18: SWING. Split Panes JSplitPane is used to divide two (and only two) Components. The two Components are graphically divided based on the look and feel implementation,

A component's position determines its relationship with other components on the same layer

Positions are specified with an int between -1 and (n - 1), where n is the number of components at the depth.

Unlike layer numbers, the smaller the position number, the higher the component within its depth

Using -1 is the same as using n - 1; it indicates the bottom-most position.

Using 0 specifies that the component should be in the top-most position within its depth.

Both a component's layer and its relative position within its layer can change.

Page 19: SWING. Split Panes JSplitPane is used to divide two (and only two) Components. The two Components are graphically divided based on the look and feel implementation,

To change a component's layer- use the setLayer() method.

To change a component's position within its layer- use the moveToBack() and moveToFront() methods provided by JLayeredPane.

void add(Component, Integer) void add(Component, Integer, int)

- Add the specified component to the layered pane. The second argument indicates the layer. The third argument, when present, indicates the component's position within its layer.

Layered Panes Methods

Page 20: SWING. Split Panes JSplitPane is used to divide two (and only two) Components. The two Components are graphically divided based on the look and feel implementation,

import javax.swing.*; import java.awt.Color; public class SimpleLayers extends JFrame { public SimpleLayers() { super("LayeredPane Demonstration"); setSize(200, 150); setDefaultCloseOperation(EXIT_ON_CLOSE); JLayeredPane lp = getLayeredPane(); JButton top = new JButton(); top.setBackground(Color.blue); top.setBounds(20, 20, 50, 50); JButton middle = new JButton(); middle.setBackground(Color.cyan); middle.setBounds(40, 40, 50, 50); JButton bottom = new JButton(); bottom.setBackground(Color.red); bottom.setBounds(60, 60, 50, 50); // Place the buttons in different layers lp.add(middle, new Integer(2)); lp.add(top, new Integer(3)); lp.add(bottom, new Integer(1)); } public static void main(String[] args) { SimpleLayers sl = new SimpleLayers(); sl.setVisible(true); }}

Page 21: SWING. Split Panes JSplitPane is used to divide two (and only two) Components. The two Components are graphically divided based on the look and feel implementation,

Dialog Boxes

A Dialog window is an independent subwindow meant to carry temporary notice apart from the main Swing Application Window.

Most Dialogs present an error message or warning to a user, but Dialogs can present images, directory trees, or just about anything compatible with the main Swing Application that manages them.

Every dialog is dependent on a Frame component. When that Frame is destroyed, so are its dependent Dialogs. When the

frame is iconified, its dependent Dialogs also disappear from the screen. When the frame is deiconified, its dependent Dialogs return to the screen.

A swing JDialog class inherits this behavior from the AWT Dialog class. A Dialog can be modal. When a modal Dialog is visible, it blocks user input to all other windows in

the program. JOptionPane creates JDialog that are modal.

To create a non-modal Dialog, you must use the JDialog class directly. All dialog boxes must created with parent frame.

Page 22: SWING. Split Panes JSplitPane is used to divide two (and only two) Components. The two Components are graphically divided based on the look and feel implementation,

JOptionPane Class Using JOptionPane, you can quickly create and customize several different kinds of

dialogs.  JOptionPane provides support for laying out standard dialogs, providing icons,

specifying the dialog title and text, and customizing the button text.

JOptionPane class is available in the javax.swing.*; package. This class provide various types of message dialog box as follows:

A simple message dialog box which has only one button i.e. "Ok". A message dialog box which has two or three buttons. You can set several values for viewing several message dialog box as follows:

1.)    "Yes" and "No"2.)    "Yes", "No" and "Cancel"3.)    "Ok", and "Cancel"

A input dialog box which contains two buttons "Ok" and "Cancel".

showMessageDialog() - Method to create and display a message dialog box to present regular, warning or error messages.

showConfirmDialog() - Method to create and display a confirmation dialog box with yes, no and cancel buttons.

showInputDialog() - Method to create and display an input dialog box to prompt for some input.

showOptionDialog() - Method to create and display a generic dialog box to present message, take a confirmation, or take some input.

Page 23: SWING. Split Panes JSplitPane is used to divide two (and only two) Components. The two Components are graphically divided based on the look and feel implementation,

Parameters: The parameters to these methods follow consistent patterns: parentComponent - Defines the Component that is to be the parent of this

dialog box message -A descriptive message to be placed in the dialog box messageType

Defines the style of the message. The Look and Feel manager may lay out the dialog differently depending on this value, and will often provide a default icon. The possible values are: 1. ERROR_MESSAGE 2. INFORMATION_MESSAGE 3. WARNING_MESSAGE 4. QUESTION_MESSAGE 5. PLAIN_MESSAGE

optionType -Defines the set of option buttons that appear at the bottom of the dialog box:

1. DEFAULT_OPTION 2. YES_NO_OPTION 3. YES_NO_CANCEL_OPTION 4.OK_CANCEL_OPTION

When one of the showXxxDialog methods returns an integer, the possible values are:

YES_OPTION NO_OPTION CANCEL_OPTION OK_OPTION CLOSED_OPTION

Page 24: SWING. Split Panes JSplitPane is used to divide two (and only two) Components. The two Components are graphically divided based on the look and feel implementation,

Example-1 ShowMeassageDialog import javax.swing.*; import java.awt.event.*; public class ShowDialogBox{ JFrame frame; public static void main(String[] args){ ShowDialogBox db = new ShowDialogBox(); } public ShowDialogBox(){ frame = new JFrame("Show Message Dialog"); JButton button = new JButton("Click Me"); button.addActionListener(new MyAction()); frame.getContentPane().add(button); frame.setSize(400, 400); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public class MyAction implements ActionListener{ public void actionPerformed(ActionEvent e){ JOptionPane.showMessageDialog(frame,"welcome to swing world");} } }

Page 25: SWING. Split Panes JSplitPane is used to divide two (and only two) Components. The two Components are graphically divided based on the look and feel implementation,

Example-2 ShowInputDialog import javax.swing.*; import java.awt.event.*; public class inputdialog{ public static void main(String[] args){ JFrame frame = new JFrame("Input Dialog Box Frame"); JButton button = new JButton("Show Input Dialog Box"); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ String str = JOptionPane.showInputDialog(null, "Enter some text : ", "Input Dailog Box", 1); if(str != null) JOptionPane.showMessageDialog(null, "You entered the text : " + str, "Input Dailog Box", 1); else JOptionPane.showMessageDialog(null, "You pressed cancel button.", "Input Dailog Box", 1); } }); JPanel panel = new JPanel(); panel.add(button); frame.getContentPane().add(panel); frame.setSize(200, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }}

Page 26: SWING. Split Panes JSplitPane is used to divide two (and only two) Components. The two Components are graphically divided based on the look and feel implementation,

Example-3 showconfirmdialog import javax.swing.JFrame; import javax.swing.JOptionPane;

public class confirmdialog { public static void main(String argv[]) { JOptionPane.showConfirmDialog(new JFrame(), "Do you want to quit this application ?","QUESTION_MESSAGE", JOptionPane.YES_NO_OPTION);

if(JOptionPane.YES_NO_OPTION == JOptionPane.YES_OPTION) System.exit(0);

} }

Page 27: SWING. Split Panes JSplitPane is used to divide two (and only two) Components. The two Components are graphically divided based on the look and feel implementation,

Tables

Swings implement tables with JTable class which is subclass of JComponent.

JTabel is the most flexible swing component that allows user to store and edit data in tabular format.

It is user interface component that represents data of two-dimensional tabular format.

Table model: All tables contains its data from an object that implements the TableModel interface of java swing package.

The TableModel interface uses the methods of JTable class that integrates a tabular data model.

JTable(Object data[ ][ ] ,Object col[ ])

Page 28: SWING. Split Panes JSplitPane is used to divide two (and only two) Components. The two Components are graphically divided based on the look and feel implementation,

import javax.swing.*; import java.awt.*; public class table2 { public static void main(String[] args) { new table2(); } public table2() { JFrame frame = new JFrame("Creating JTable Component Example!"); JPanel panel = new JPanel(); String data[][] = {{"vinod","BCA","A"},{"Raju","MCA","b"}, {"Ranjan","MBA","c"},{"Rinku","BCA","d"}};

String col[] = {"Name","Course","Grade"}; JTable table = new JTable(data,col);

panel.add(table,BorderLayout.CENTER); frame.getContentPane().add(panel); frame.setSize(300,200); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}