Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

Changing Background Color Using ActionListener in JShell in Java

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The Java Shell tool (JShell) is an interactive tool for learning the Java programming language and prototyping Java code. JShell is a Read-Evaluate-Print Loop (REPL), which evaluates declarations, statements, and expressions as they are entered and immediately shows the results. The tool is run from the command line. JShell was introduced in java 9 and hence can not be used in java 8.
Jshell has reduced all the efforts that are required to run a Java program and test business logic. If we don’t use Jshell, creating of Java program involves the following steps.

Using the concept of Swing class for dealing with the frames one can create a frame in JShell and can add a Button on the Frame and works the same as ActionListener in Swing. 


  1. Firstly import awt package for creating a Frame
  2. Create Frame Object
  3. Set Visibility
  4. Set Size of the Frame
  5. Set Location of the Frame
  6. Set Background Color  of the Frame
  7. Create a Button
  8. Add Button to the Frame
  9. Set the Frame Layout
  10. Use ActionListener


Step 1: Firstly import awt package for creating a Frame


jshell>import java.awt.* ;

Step 2: Creating Frame Object


jshell>Frame f = new frame() ;   || 'f' is object name
f==>java.awt.Frame[frame, 0, 0, 0, 0*0, invalid, hidden, layout...t, title = resizable, normal ]

Step 3: Setting Visibility for a frame as after creating a Frame object, the frame is still not visible on the screen.

Syntax: Setting Visibility to true

jshell>f.setVisible(true) ;

Output: When the visibility of the frame is true.

  • By default, Visibility of the Frame is False.
  • This Frame is displayed after setting Visibility “True”.
  • This Frame will be visible on top left corner.
  • If frame visibility is set to “False”, then frame will be hidden.

Syntax: Setting Visibility to false

jshell>f.setVisible(false) ;

Output: When the visibility of the frame is false.

No image

Step 4: Setting Size of the Frame

Syntax: Here the first parameter is height and the second is the width in setSize() function.

jshell>f.setSize(length_pixelValue , breadth_pixelValue) ;

Arbitral considering values:

  • length_pixelValue   = 500
  • breadth_pixelValue = 500
jshell>f.setSize(500, 500) ;

Output: This Frame will be visible in top left corner.

Step 5: Setting Location of the Frame


jshell>f.setLocation(100,100) ;
  • Setting Location of Frame refers to the distance from the X and Y axis.
    • The first parameter defines the distance from the left margin of the screen.
    • Second Parameter r defines the distance from the top margin of the screen.

Step 6: Setting Background Color of the Frame


jshell>f.setBackground(Color.yellow) ;


Step 7: Creating a Button

jshell> Button b1=new Button(“Click!!”)

b1 ==> java.awt.Button[button0,0,0,0×0,invalid,label=Click!!]


Step 8: Adding Button to the Frame

jshell> f.add(b1)

$8 ==> java.awt.Button[button0,0,0,0×0,invalid,label=Click!!]


Step 9: Setting the Frame Layout

jshell> f.setLayout(new FlowLayout());

Step 10: Using ActionListener


jshell>b1.addActionListener(e->setBackground(Colour.cyan)) ;


On Clicking on the button the background color is changed to Cyan. Here we can also say that in JShell we can use Lambda Expression. Hence, successfully able to change the background color.



// Java Program to change Background Color
// Using ActionListener in JShell
// Importing awt package
jshell > import java.awt.*
             // Creating  a frame object
    > Frame f
    = new Frame()
      // Setting object created above
      > java.awt
              .Frame[frame0, 0, 0, 0x0, invalid, hidden,
                     layo... t, title =, resizable, normal]
          // Setting visibility to frame
          > f.setVisible(true)
            // Setting size of frame
          > f.setSize(500, 500)
            // Setting location of frame from X and Y axis
          > f.setLocation(100, 100)
            // Setting background color
          > f.setBackground(Color.yellow)
          > Button
          b1 = new Button("Click!!") b1 ==
               > java.awt.Button[button0, 0, 0, 0x0,
                                 invalid, label = Click !!]
                   > f.add(b1) $8
               > java.awt.Button[button0, 0, 0, 0x0,
                                 invalid, label = Click !!]
                   > f.setLayout(new FlowLayout());
// Changing background color
    > b1.addActionListener(e -> f.setBackground(Color.cyan))
      // Terminate

My Personal Notes arrow_drop_up
Last Updated : 13 Jan, 2021
Like Article
Save Article
Similar Reads
Related Tutorials