Using JRadioButtons in Frame-based Applications

For this demonstration we used much of the code of our JRadioButton applet. We based the changes on Brian Long's Desktop "SwingSample" included with Oxygene for Java.

The essential steps in the conversion are as follows.

  • Base the class on a Frame instead of an Applet with JRadioButtonDemo = public class(JFrame, ActionListener).
  • Add verticalBox : Box; to the private declarations.
  • In the public section replace the init method with constructor and class method Main(args: array of String).
  • Copy the code from init to constructor in the implementation section.
  • Create a vertical box with the statement verticalBox := Box.createVerticalBox.
  • Add widgets to the box instead of to the applet e.g. verticalBox.add(radioPanel) instead of add(radioPanel).
  • Add the box to the frame with the instruction add(verticalBox).
  • Include code to set the title, size and visibility of the frame.

The complete converted code follows a screenshot.

Application in action

Application in action

The code in jradio_app.pas

namespace jradiobutton_app_demo;

interface

uses
  java.util, java.awt.*, javax.swing;
   
type
  JRadioButtonDemo = public class(JFrame, ActionListener)
  private
    rbtnAppend, rbtnReplace, rbtnInsert, rbtnCopy : JRadioButton;
    txtNew : TextField;
    taEdit, taCopy : JTextArea;
    initText : String := 'This is the starting text to modify by pressing the buttons.';
    lblInstruction : Label;
    radioPanel : JPanel;
    rgroup : ButtonGroup;
    verticalBox : Box;
   
  public
    constructor;
    class method Main(args: array of String);
    method actionPerformed(e : ActionEvent);
  end;

implementation

constructor JRadioButtonDemo;
begin
  lblInstruction := new Label('Enter text to append, insert, or replace with.');
  txtNew := new TextField('', 40);
  radioPanel := new JPanel(new GridLayout(0, 1));
  rgroup := new ButtonGroup;
  rbtnAppend := new JRadioButton('Append');
  rbtnReplace := new JRadioButton('Replace selected text');
  rbtnInsert := new JRadioButton('Insert at cursor position');
  rbtnCopy := new JRadioButton('Copy to lower TextArea');
  rgroup.add(rbtnAppend);
  rgroup.add(rbtnReplace);
  rgroup.add(rbtnInsert);
  rgroup.add(rbtnCopy);
  radioPanel.add(rbtnAppend);
  radioPanel.add(rbtnReplace);
  radioPanel.add(rbtnInsert);
  radioPanel.add(rbtnCopy);

  rbtnAppend.addActionListener(self);
  rbtnReplace.addActionListener(self);
  rbtnInsert.addActionListener(self);
  rbtnCopy.addActionListener(self);
  rbtnAppend.setselected(true);

  taEdit := new JTextArea(initText); 
  taCopy := new JTextArea('');
  taEdit.setLineWrap(true);
  taCopy.setLineWrap(true);
  taEdit.setWrapStyleWord(true);
  taCopy.setWrapStyleWord(true);

  taEdit.setForeground (Color.blue.brighter);
  taCopy.setForeground (Color.red);

  verticalBox := Box.createVerticalBox;
  verticalBox.setBackground(Color.green);
  verticalBox.add(lblInstruction);
  verticalBox.add(txtNew);
  verticalBox.add(taEdit);
  verticalBox.add(radioPanel);
  verticalBox.add(taCopy);
  add(verticalBox);

  //Ensure app closes when you click the close button
  DefaultCloseOperation := JFrame.EXIT_ON_CLOSE;
  //Set window caption and size
  Title := 'JRadioButton Demo';
  Size := new Dimension(270, 300);		
  setVisible(true); 
end;

class method JRadioButtonDemo.Main(args: array of String);
begin
  new JRadioButtonDemo;
end;

method JRadioButtonDemo.actionPerformed(e : ActionEvent);
begin
  if e.getSource = rbtnAppend then
    taEdit.append(txtNew.getText);
 
  if e.getSource = rbtnReplace then
    taEdit.replaceRange(txtNew.getText,  taEdit.getSelectionStart, taEdit.getSelectionEnd); 

  if e.getSource = rbtnInsert then 
    taEdit.insert(txtNew.getText, taEdit.getCaretPosition); 

  if e.getSource = rbtnCopy then 
    taCopy.setText(taEdit.getText);  
end;

end.

The code of jradio_app.oxygene

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
  <PropertyGroup>
    <OutputType>Executable</OutputType>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="rt.jar" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="jradio_app.pas" />
  </ItemGroup>
  <Import Project="$(MSBuildExtensionsPath)\RemObjects Software\Oxygene\RemObjects.Oxygene.Cooper.targets" />
</Project>
Programming - a skill for life!

How to use widgets such as JButtons, JRadioButtons, JLists, JTables and JDesktopPanes in Oxygene for Java Applications