Download presentation
Presentation is loading. Please wait.
Published byEsmond Singleton Modified over 9 years ago
1
SWING
2
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.
4
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); }
5
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); }
6
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); } }
7
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.
9
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); }
10
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); }
11
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.
13
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" };
14
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(); } }
15
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.
17
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.
18
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.
19
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
20
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); }}
21
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.
22
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.
23
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
24
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");} }
25
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); }}
26
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); }
27
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[ ])
28
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); }}
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.