Presentation is loading. Please wait.

Presentation is loading. Please wait.

JSplitPane & JTabbedPane

Similar presentations


Presentation on theme: "JSplitPane & JTabbedPane"— Presentation transcript:

1 JSplitPane & JTabbedPane
By Harini Pillalamarri

2 JSplitPane Introduction
SplitPane is used to graphically divide two components based on their look and feel. The components can be left/right aligned or top/bottom aligned. Components can be resized by the user by using either the setDividerLocation method or resetToPreferredSizes method. When resizing the Components the minimum size of the Components is used to determine the maximum/minimum position the Components can be set to. If the minimum size of the two components is greater than the size of the split pane you will not be allowed to resize it.

3 SplitPane Constructors
JSplitPane() JSplitPane(int newOrientation) JSplitPane(int newOrientation, boolean newContinuousLayout) JSplitPane(int newOrientation, boolean newContinuousLayout, ComponentnewLeftComponent, Component newRightComponent) JSplitPane(int newOrientation, Component newLeftComponent, Component newRightComponent)

4 Properties ORIENTATION PROPERTY RESIZE WEIGHT PROPERTY
Horizontal or Vertical RESIZE WEIGHT PROPERTY ONE TOUCH EXPANDABLE PROPERTY CONTINOUS PROPERTY LAYOUT Methods : getDividerLocation() getMaximumDividerLocation() remove(Component component) setBottomComponent(Component comp) setDividerSize(int newSize)

5 Events JSplitPane class does not provide any methods to respond to events, therefore we can use methods from JComponent such as addPropertyChangeListener(). Property Change listener – We can attach this Listener directly to the SplitPane and observe changes in the divider by using DIVIDER LOCATION PROPERTY. PropertyChangeListener propertyChangeListener = newPropertyChangeListener() { public void propertyChange(PropertyChangeEvent changeEvent) { JSplitPane sourceSplitPane = (JSplitPane) changeEvent.getSource(); int current = sourceSplitPane.getDividerLocation(); String string = Integer.toString(current); textarea.setText("Current divider location: " + string); } };

6 JSplitPane Demo

7 JSplitPane Sample Code
public SplitPane() { textarea = new JTextArea("textarea1"); textarea2 = new JTextArea("textarea2"); splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, textarea, textarea2); splitPane.setOneTouchExpandable(true); splitPane.setDividerLocation(150); Dimension minimumSize = new Dimension(100, 50); textarea.setMinimumSize(minimumSize); textarea2.setMinimumSize(minimumSize); splitPane.setPreferredSize(new Dimension(400, 200)); splitPane.setDividerSize(20); }

8 JTabbedPane Introduction
TabbedPane is used to switch between a group of components by clicking on the tab. Generally addTab and insertTab methods are used to add TabbedPane objects in a frame. To switch to a particular tab we can use , a mouse, keyboard arrows or key mnemonics. The set of tabs are represented by indices based on the position where they are added. The first tab would have the index value of 0 and last would have an index value of n-1.

9 We can use add() method(s) to add components to the tabs
add(Component component) add(Component component, int index) add(Component component, Object constraints) add(Component component, Object constraints, int index) add(String title, Component component)

10 Constructors setToolTipTextAt(int index, String toolTipText)
JTabbedPane() JTabbedPane(int tabPlacement) JTabbedPane(int tabPlacement, int tabLayoutPolicy) Properties: tabPlacement – top/bottom or left/right SCROLL_TAB_LAYOUT WRAP_TAB_LAYOUT Methods: setMnemoicAt(int tabIndex, int mnemonic) setToolTipTextAt(int index, String toolTipText) remove(Component component) getComponentAt(int index)

11 Events An event is fired by the TabbedPane whenever the selected tab is changed. We use Change Listener to handle the event. ChangeListener listener = new ChangeListener() { public void stateChanged(ChangeEvent e) { JTabbedPane tabSource = (JTabbedPane) e.getSource(); String tabtitle = tabSource.getTitleAt(tabSource.getSelectedIndex()); if (tabtitle.equals(“title")) { Do Something.. } Do Something…

12 Other.. We can attach Listeners to the components that have been added to specific tabs to handle other events. For instance if we were to close a tab. Generally we could do that by adding a JButton to the tab and when the button is pressed an action is performed and the tab is removed. public void actionPerformed(ActionEvent e) { int i = pane.indexOfTabComponent(Component ); if (i != -1) { pane.remove(i); }

13 JTabbedPane Demo

14 JTabbedPane sample code
tpane = new JTabbedPane(); tpane.setForeground(Color.red); tpane.setBackground(Color.yellow); textarea = new JTextArea(); tpane.addTab("Tab1", textarea); button = new JButton("button1"); tpane.addTab("Tab2", button); textarea2 = new JTextArea(); String tooltip = "tooltip added"; tpane.addTab(null, null, textarea2, tooltip); tpane.setTabComponentAt(2, new JButton("Tab3")); button2 = new JButton("button2"); button2.setSize(50, 50); panel = new JPanel(); panel.add(button2); tpane.addTab("Tab4", panel);

15 Conclusion Both SplitPane and TabbedPane are primarily used as containers for other components. JSplitPane can only hold two components, whereas JTabbedPane can hold multiple components under multiple tabs. With JSplitPane the user can resize the components by changing the location of the divider. The divider also can be modified. But JSplitPane does not provide methods to handle events related to the divider. With JTabbedPane the user can add tabs to any particular location in the container or by default it is added at the very end. Tabs can be selected and modified in many ways. Tab selection can be kept in track by using Change Listener.


Download ppt "JSplitPane & JTabbedPane"

Similar presentations


Ads by Google