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,

Slides:



Advertisements
Similar presentations
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Advertisements

Introduction to JFC Swing Written by Adam Carmi. Agenda About JFC and Swing Pluggable Look and Feel Swing Components Borders Layout Management Events.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Scott Grissom, copyright 2006Ch 11: GUI Slide 1 Graphical User Interfaces (Ch 11) Careful design of a graphical user interface is key to a viable software.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
GUI Basics: Introduction. Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your.
F27SB2 Software Development 2 Lecture 9: Java GUIs 6.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington User Interface COMP 112 #30.
Intro to GUIs (Graphical User Interfaces) Section 2.5Intro. to GUIs: a GUI Greeter Section 3.7Graphical/Internet Java: Einstein's Equation.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
Java Programming, Second Edition Chapter Five Input and Selection.
Java Swing. Swing is a set of classes that provides more powerful and flexible components than are possible with the AWT. In addition to the familiar.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
FEN IntroJava2006 AAU1 GUI: Graphical User Interface AWT/SWING: Components Drag and Drop in NetBeans Events Listeners.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
1 Java Swing - Lecture 2 Components and Containment Boriana Koleva
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Option Panes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Graphical User Interfaces. Graphical input and output with JOptionPane.
GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven.
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
Swinging in Your Java Playground. Background Swing is a part of the Java Foundation Classes (JFC). The JFC is made up of features intended to give a programmer.
Swing. Introduction to Swing What is Swing? “ Swing is a diverse collection of lightweight components that can be used to build sophisticated user interfaces.”
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
JSplitPane & JTabbedPane
Basics of GUI Programming Chapter 11 and Chapter 22.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
JOptionPane Class JOptionPane makes it easy to pop up a standard dialog box that prompts users for a value or informs them of something. While the JOptionPane.
Lecture # 6 Graphical User Interface(GUI). Introduction A graphical user interface (GUI) presents a user- friendly mechanism for interacting with an application.
Creating and Using Dialogs ● A dialog is a box that pops up and prompts the user for a value or informs them of something ● One way: directly create objects.
1 IM103 week 10 Enhancing the user interface By the end of lecture you should be able to:  distinguish between heavyweight and lightweight components;
Casting, Wrapper Classes, Static Methods, JOptionPane Class.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Graphical User Interface (GUI)
Getting Started with GUI Programming Chapter 10 CSCI 1302.
제 14 장 고급 스윙 컴포넌트.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
Dept. of CSIE, National University of Tainan 10/21/2012 Working with Swing.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit 6 Graphical user interfaces 1.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Module 13: Swing API Object Oriented Programming(Java)
A Quick Java Swing Tutorial
Building a Swing Interface
A Quick Java Swing Tutorial
CSE 114 – Computer Science I Event Programming
Message, Input, Confirm, and Specialized Dialogs
JOptionPane Dialogs javax.swing.JOptionPane is a class for creating dialog boxes. Has both static methods and instance methods for dialogs. Easy to create.
CS18000: Problem Solving and Object-Oriented Programming
Steps to Creating a GUI Interface
A Quick Java Swing Tutorial
Advanced Programming in Java
CS431 ws99 Half Text Half Graphics
Ch. No Name Marks 01 AWT Networking JDBC Swing 05
Ch. No Name Marks 01 AWT Networking JDBC Swing 05
Graphical User Interface
Presentation transcript:

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, 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.

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

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

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

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.

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

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

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 Description HORIZONTAL_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.

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

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

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.

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.

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.

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

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

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.

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.

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

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

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

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

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[ ])

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