In the previous section of JRuby Tutorials you have studied how to use Java classes in JRuby examples to show results on your console. Now we are going to describe you how to use Swing in JRuby program.
In this example we will create our first "HelloWorld" swing application in JRuby. First line in our program tells that we are making our JRuby program enabled to use java classes.
frame = javax.swing.JFrame.new() creates a new variable object "frame" which is an instance of javax.swing.JFrame .
frame.getContentPane().add(javax.swing.JLabel.new("Hello World")) will add a new JLabel to this frame via getting the content pane.
frame.set_visible(true) makes frame to be visible. Here is the full example code of JRubySwingHello.rb as follows:
frame = javax.swing.JFrame.new()
Type the following command in your command prompt to
execute this example:
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.