Skip to main content

Java Swing: JToolBar, JViewport & JFrame

Java JToolBar

JToolBar container allows us to group other components, usually buttons with icons in a row or column. JToolBar provides a component which is useful for displaying commonly used actions or controls.

Nested Classes

Modifier and TypeClassDescription
protected classJToolBar.AccessibleJToolBarThis class implements accessibility support for the JToolBar class.
static classJToolBar.SeparatorA toolbar-specific separator.

Constructors

ConstructorDescription
JToolBar()It creates a new tool bar; orientation defaults to HORIZONTAL.
JToolBar(int orientation)It creates a new tool bar with the specified orientation.
JToolBar(String name)It creates a new tool bar with the specified name.
JToolBar(String name, int orientation)It creates a new tool bar with a specified name and orientation.

Useful Methods

Modifier and TypeMethodDescription
JButtonadd(Action a)It adds a new JButton which dispatches the action.
protected voidaddImpl(Component comp, Object constraints, int index)If a JButton is being added, it is initially set to be disabled.
voidaddSeparator()It appends a separator of default size to the end of the tool bar.
protected PropertyChangeListenercreateActionChangeListener(JButton b)It returns a properly configured PropertyChangeListener which updates the control as changes to the Action occur, or null if the default property change listener for the control is desired.
protected JButtoncreateActionComponent(Action a)Factory method which creates the JButton for Actions added to the JToolBar.
ToolBarUIgetUI()It returns the tool bar's current UI.
voidsetUI(ToolBarUI ui)It sets the L&F object that renders this component.
voidsetOrientation(int o)It sets the orientation of the tool bar.

Java JToolBar Example

  1. import java.awt.BorderLayout;  
  2. import java.awt.Container;  
  3. import javax.swing.JButton;  
  4. import javax.swing.JComboBox;  
  5. import javax.swing.JFrame;  
  6. import javax.swing.JScrollPane;  
  7. import javax.swing.JTextArea;  
  8. import javax.swing.JToolBar;  
  9.   
  10. public class JToolBarExample {  
  11.     public static void main(final String args[]) {  
  12.         JFrame myframe = new JFrame("JToolBar Example");  
  13.         myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  14.         JToolBar toolbar = new JToolBar();  
  15.         toolbar.setRollover(true);  
  16.         JButton button = new JButton("File");  
  17.         toolbar.add(button);  
  18.         toolbar.addSeparator();  
  19.         toolbar.add(new JButton("Edit"));  
  20.         toolbar.add(new JComboBox(new String[] { "Opt-1""Opt-2""Opt-3""Opt-4" }));  
  21.         Container contentPane = myframe.getContentPane();  
  22.         contentPane.add(toolbar, BorderLayout.NORTH);  
  23.         JTextArea textArea = new JTextArea();  
  24.         JScrollPane mypane = new JScrollPane(textArea);  
  25.         contentPane.add(mypane, BorderLayout.EAST);  
  26.         myframe.setSize(450250);  
  27.         myframe.setVisible(true);  
  28.     }  
  29. }  

Java JViewport

The JViewport class is used to implement scrolling. JViewport is designed to support both logical scrolling and pixel-based scrolling. The viewport's child, called the view, is scrolled by calling the JViewport.setViewPosition() method.

Nested Classes

Modifier and TypeClassDescription
protected classJViewport.AccessibleJViewportThis class implements accessibility support for the Jviewport class.
protected classJViewport.ViewListenerA listener for the view.

Fields

Modifier and TypeFieldDescription
static intBACKINGSTORE_SCROLL_MODEIt draws viewport contents into an offscreen image.
protected ImagebackingStoreImageThe view image used for a backing store.
static intBLIT_SCROLL_MODEIt uses graphics.copyArea to implement scrolling.
protected booleanisViewSizeSetTrue when the viewport dimensions have been determined.
protected PointlastPaintPositionThe last viewPosition that we've painted, so we know how much of the backing store image is valid.
protected booleanscrollUnderwayThe scrollUnderway flag is used for components like JList.
static intSIMPLE_SCROLL_MODEThis mode uses the very simple method of redrawing the entire contents of the scrollpane each time it is scrolled.

Constructor

ConstructorDescription
JViewport()Creates a JViewport.

Methods

Modifier and TypeMethodDescription
voidaddChangeListener(ChangeListener l)It adds a ChangeListener to the list that is notified each time the view's size, position, or the viewport's extent size has changed.
protected LayoutManagercreateLayoutManager()Subclassers can override this to install a different layout manager (or null) in the constructor.
protected Jviewport.ViewListenercreateViewListener()It creates a listener for the view.
intgetScrollMode()It returns the current scrolling mode.
ComponentgetView()It returns the JViewport's one child or null.
PointgetViewPosition()It returns the view coordinates that appear in the upper left hand corner of the viewport, or 0,0 if there's no view.
DimensiongetViewSize()If the view's size hasn't been explicitly set, return the preferred size, otherwise return the view's current size.
voidsetExtentSize(Dimension newExtent)It sets the size of the visible part of the view using view coordinates.
voidsetScrollMode(int mode)It used to control the method of scrolling the viewport contents.
voidsetViewSize(Dimension newSize)It sets the size of the view.

JViewport Example

  1. import java.awt.BorderLayout;  
  2. import java.awt.Color;  
  3. import java.awt.Dimension;  
  4. import javax.swing.JButton;  
  5. import javax.swing.JFrame;  
  6. import javax.swing.JLabel;  
  7. import javax.swing.JScrollPane;  
  8. import javax.swing.border.LineBorder;  
  9. public class ViewPortClass2 {  
  10.     public static void main(String[] args) {  
  11.         JFrame frame = new JFrame("Tabbed Pane Sample");  
  12.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  13.   
  14.         JLabel label = new JLabel("Label");  
  15.         label.setPreferredSize(new Dimension(10001000));  
  16.         JScrollPane jScrollPane = new JScrollPane(label);  
  17.   
  18.         JButton jButton1 = new JButton();  
  19.         jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);  
  20.         jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);  
  21.         jScrollPane.setViewportBorder(new LineBorder(Color.RED));  
  22.         jScrollPane.getViewport().add(jButton1, null);  
  23.   
  24.         frame.add(jScrollPane, BorderLayout.CENTER);  
  25.         frame.setSize(400150);  
  26.         frame.setVisible(true);  
  27.     }  
  28. }  

Output:

Java Jviewport


Java JFrame

The javax.swing.JFrame class is a type of container which inherits the java.awt.Frame class. JFrame works like the main window where components like labels, buttons, textfields are added to create a GUI.

Unlike Frame, JFrame has the option to hide or close the window with the help of setDefaultCloseOperation(int) method.

Nested Class

Modifier and TypeClassDescription
protected classJFrame.AccessibleJFrameThis class implements accessibility support for the JFrame class.

Fields

Modifier and TypeFieldDescription
protected AccessibleContextaccessibleContextThe accessible context property.
static intEXIT_ON_CLOSEThe exit application default window close operation.
protected JRootPanerootPaneThe JRootPane instance that manages the contentPane and optional menuBar for this frame, as well as the glassPane.
protected booleanrootPaneCheckingEnabledIf true then calls to add and setLayout will be forwarded to the contentPane.

Constructors

ConstructorDescription
JFrame()It constructs a new frame that is initially invisible.
JFrame(GraphicsConfiguration gc)It creates a Frame in the specified GraphicsConfiguration of a screen device and a blank title.
JFrame(String title)It creates a new, initially invisible Frame with the specified title.
JFrame(String title, GraphicsConfiguration gc)It creates a JFrame with the specified title and the specified GraphicsConfiguration of a screen device.

Useful Methods

Modifier and TypeMethodDescription
protected voidaddImpl(Component comp, Object constraints, int index)Adds the specified child Component.
protected JRootPanecreateRootPane()Called by the constructor methods to create the default rootPane.
protected voidframeInit()Called by the constructors to init the JFrame properly.
voidsetContentPane(Containe contentPane)It sets the contentPane property
static voidsetDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated)Provides a hint as to whether or not newly created JFrames should have their Window decorations (such as borders, widgets to close the window, title...) provided by the current look and feel.
voidsetIconImage(Image image)It sets the image to be displayed as the icon for this window.
voidsetJMenuBar(JMenuBar menubar)It sets the menubar for this frame.
voidsetLayeredPane(JLayeredPane layeredPane)It sets the layeredPane property.
JRootPanegetRootPane()It returns the rootPane object for this frame.
TransferHandlergetTransferHandler()It gets the transferHandler property.

JFrame Example

  1. import java.awt.FlowLayout;  
  2. import javax.swing.JButton;  
  3. import javax.swing.JFrame;  
  4. import javax.swing.JLabel;  
  5. import javax.swing.Jpanel;  
  6. public class JFrameExample {  
  7.     public static void main(String s[]) {  
  8.         JFrame frame = new JFrame("JFrame Example");  
  9.         JPanel panel = new JPanel();  
  10.         panel.setLayout(new FlowLayout());  
  11.         JLabel label = new JLabel("JFrame By Example");  
  12.         JButton button = new JButton();  
  13.         button.setText("Button");  
  14.         panel.add(label);  
  15.         panel.add(button);  
  16.         frame.add(panel);  
  17.         frame.setSize(200300);  
  18.         frame.setLocationRelativeTo(null);  
  19.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  20.         frame.setVisible(true);  
  21.     }  
  22. }  

Output

Java Jframe



Anurag Rana Educator CSE/IT

Comments

Popular posts from this blog

JAVA Scrollbar, MenuItem and Menu, PopupMenu

ava AWT Scrollbar The  object  of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a  GUI  component allows us to see invisible number of rows and columns. AWT Scrollbar class declaration public   class  Scrollbar  extends  Component  implements  Adjustable, Accessible   Java AWT Scrollbar Example import  java.awt.*;   class  ScrollbarExample{   ScrollbarExample(){               Frame f=  new  Frame( "Scrollbar Example" );               Scrollbar s= new  Scrollbar();               s.setBounds( 100 , 100 ,  50 , 100 );               f.add(s);               f.setSize( 400 , 400 );               f.setLayout( null );               f.setVisible( true );   }   public   static   void  main(String args[]){           new  ScrollbarExample();   }   }   Output: Java AWT Scrollbar Example with AdjustmentListener import  java.awt.*;   import  java.awt.event.*;   class  ScrollbarExample{        ScrollbarExample(){               Frame f=  new  Frame( "Scro

Difference between net platform and dot net framework...

Difference between net platform and dot net framework... .net platform supports programming languages that are .net compatible. It is the platform using which we can build and develop the applications. .net framework is the engine inside the .net platform which actually compiles and produces the executable code. .net framework contains CLR(Common Language Runtime) and FCL(Framework Class Library) using which it produces the platform independent codes. What is the .NET Framework? The Microsoft .NET Framework is a platform for building, deploying, and running Web Services and applications. It provides a highly productive, standards-based, multi-language environment for integrating existing investments with next-generation applications and services as well as the agility to solve the challenges of deployment and operation of Internet-scale applications. The .NET Framework consists of three main parts: the common language runtime, a hierarchical set of unified class librari

C++ this Pointer, static, struct and Enumeration

C++ this Pointer In C++ programming,  this  is a keyword that refers to the current instance of the class. There can be 3 main usage of this keyword in C++. It can be used  to pass current object as a parameter to another method. It can be used  to refer current class instance variable. It can be used  to declare indexers. C++ this Pointer Example Let's see the example of this keyword in C++ that refers to the fields of current class. #include <iostream>    using   namespace  std;   class  Employee {       public :           int  id;  //data member (also instance variable)               string name;  //data member(also instance variable)            float  salary;          Employee( int  id, string name,  float  salary)             {                   this ->id = id;                  this ->name = name;                  this ->salary = salary;            }             void  display()             {                 cout<<id<< "  " <<name<&