Benutzer-Werkzeuge

Webseiten-Werkzeuge


en:gui-usage

GUI-Usage

JLabel

A JLabel component can display just text, or an image, or both. To display an image, just use a JLabel component.

Example:

With the method setLocation(int x, int y) of a JLabel-component you can animate an image.


JTextField

With a JTextField component you can enter a line of text. Use the getText() method of the JTextField component to read the entered text.

Example:

  String Name = tfName.getText();



JNumberField

With JNumbertField components you can easily display or enter numbers.

With getDouble(), getFloat(), getInt() and getLong() you get a number of a specific type:

Example:

  int Age = nfAge.getInt();

You can display decimal numbers with maximum or a specific amount of decimal places

Example:

  nfSum.setDouble(Sum);    // maximum decimal places: 3657.42323426347834    
  nfSum.setDouble(Sum, 2); // two decimal places:     3657.42 



JTextArea

A JTextArea component displays multiline text, a JTextField component a single line. With the object-inspector you can enter the text. During runtime you can add text with the append()-method respectively set text with setText() or read it with getText(). The lines are separated with the control character „\n“ (new line).

Example:

  taAusgabe.setText("Output:\n\n");
  taAusgabe.append("Number " + Number + " found! \n");



JButton

Every JButton-component gets an event-method for the actionPerformed-event, which is created if you push a button. In a GUI-form doubleclick a button to place the cursor at the beginning of the corrsponding event-method.

Example:

  public void jButton1_ActionPerformed(ActionEvent evt) {
    // TODO add your code here
  } // end of jButton1_ActionPerformed



JCheckBox

A JCheckBox component is selected or not selected. You get the current state with isSelected().

Example:

  if (myCheckBox.isSelected()) ...



ButtonGroup

A Buttongroup groups Radiobuttons or Checkboxes. Set the attribute Checkboxes to true if you want a group of Checkboxes. Enter your options using the Items attribute.

For a buttongroup of radiobuttons the Java-Editor places this method into the java source code:

  public String buttonGroup1_getSelectedButtonGroupLabel() { ... }

so it's easy to get the selected JRadioButton of a Buttongroup. Example:

  if (bgColor_getSelectedButtonGroupLabel().equals("green"))



JList

A JList component shows a list of objects. The user can select on or more objects. With the Items attribute in the object-inspector you can enter strings into a JList.

The data of a list are managed in a ListModel. During runtime you can edit the list objects with methods of the ListModel.

Examples

Access through ListModel:

  myListModel.addElement("Vera");
  myListModel.remove(0);
  String s = (String) myListModel.elementAt(3);

The ListModel knows all elements, but an element is selected in the List, not in the ListModel.

Access through List:

  int i = myList.getSelectedIndex();
  String s = (String) myList.getSelectedValue();

If the List contains numbers, the selected string has to be converted into a number:

  String s = (String) myList.getSelectedValue();
  int number = Integer.parseInt(s);

JComboBox

A JComboBox component is a combination of an editable inputline and and a drop down list. At runtime the user can select an item from th drop down list or input text.

With the object-inspector you can enter strings into the Items attribute for the drop down list of the JComboBox. To enable input in the inputline you set the editable attribute to true.

The data of a JCombobBox are managed in a ComboBoxModel. During runtime you can edit the combobox data with methods of the ComboBoxModel.

Examples

Access through ComboBoxModel:

  myComboBoxModel.addElement("Vera");
  myComboBoxModel.removeElementAt(0);
  String s = (String) myComboBoxModel.getElementAt(3);

The ComboBoxModel knows all elements, but an element is selected in the ComboBox, not in the ComboBoxModel.

Access through ComboBox:

  int i = myComboBox.getSelectedIndex();
  String s = (String) myComboBox.getSelectedItem();

To react automatically on an input in the inputline or selection from the drop down list you set the actionPerformed-event in the object-inspector.


JSpinner

With a JSpinner component you can select numbers from a range between Minimum and Maximum. You set the values of Minimum, Maximum, StepSize and Value with the object-inspector.

The data of a JSpinner component are managed in the SpinnerModel. During runtime you can edit the data with the SpinnerModel-methods.

Examples:

Access through SpinnerModel

mySpinnerModel.setStepSize(2);
  int Value = mySpinnerModel.getNumber().intValue();

Access through Spinner

int Value = (int) mySpinner.getValue();



JScrollBar

With a JScrollBar-component text, pictures or anything else can be scrolled, i.e. viewed even if it does not fit into the space of a window.

For this you create with the object-inspector an adjustmentValueChanged-eventmethod, in which with

  int Value = myScrollBar.getValue();

you get the JScrollBar-value and do the scrolling according to it.


JScrollPane

A JScrollPane-component provides a horizontal and a vertical JScrollBar-component for a twodimensional scrolling.

The Java-Editor adds to JTextArea, JList, JTable, JTree, JEditorPane und JTextPane-components automatically a JScrollPane-component.


JPanel

JPanel-components are used for structuring of graphical user interfaces. Each JPanel-component is a container which can hold other gui-components. With the object-inspector you can set a border to a JPanel-component.


Canvas

A Canvas-component provides a drawing-area.

To draw on the drawing-area you can get a graphic-context from the Canvas-component. This graphic-context is used as a paint-box with many drawing-possibilities.

You get the graphics-context from your Canvas-component in this way:

  Graphics g = myCanvas.getGraphics();

With the graphics-context you are able to draw e. g. a rectangle:

  g.fillRect(100, 50, 200, 250);

If you minimize your Java-Application with a Canvas-component und show it up again, the content of the Canvas-Component is lost. To avoid this, you have to create a subclass from Canvas and do the drawing in the paint(Graphics g) method. Such a derived class can be placed in a GUI-form with right-click on the Canvas-Symbol.


Turtle

Since Java-Editor version 14.04 we have an animated Turtle-component for JavaFX. It's conception is based on the former Turtle-component and supports a cartesian coordinate system.

Since Java-Editor version 13.00 the Turtle-component has changed. We now have an animated Turtle-Component based on the Turtle from Ägidius Plüss. The Playground- and a Turtle-component are on the new Utilities-tab. You can place one or more Turtles on a Playground.

Due to didactic reduction and technical issues the GUI doesn't react when a Turtle is drawing.

Before Version 13.00 we had another Turtle-component. If you want to use a program with this older Turtle you have to import it like this:

import je.util.*;

As a didactic reduction the turtle has a setOrigin(double x, double y)-method, with which you can define a coordinatesystem as known from mathematics.

Example:

Since V13.00 the setOrigin-method is a method of the playground.



JMenuBar

With a JMenuBar-component you can create a menu bar. The configuration is done via the menus (JMenu-components) to be displayed in the menu bar.


JMenu

With a JMenu-component you create a menu for a menu bar. In the object-inspector you specify the desired menu bar at the MenuBar attribute, the menu commands (e. g. New, Open, Save, Print, Exit) at the MenuItems attribute and the caption of the menu (e. g. File) in the menu bar at the Text attribute.

For each menu command the Java-Editor creates a corresponding event method.



JPopupMenu

With a JPopupMenu-component you create a popup menu. In the example a canvas component with yellow background reacts on a rightclick and shows the popup menu.

In the object inspector you input at the attribute Listener the gui-component, which shows the popup menu when rightcklicked. By the default this the background of the application window recacts on a rightclick. At the attribute MenuItems you input the menu commands (e. g. Clear, Draw, Print). Usually the attribute Text as label of a popup menu isn't shown.

For each menu command the Java-Editor creates a corresponding event method.



Timer

With a Timer component, one can control time-dependent processes. Operations can be started and stopped after a predetermined period of time once or periodically.

The Timer component uses the class javax.swing.Timer, not the class java.util.Timer.

Example:

  timer1.start();
  ... do anything
  timer1.stop();



JTable

With a jTable-component you display a table.

Set the number of columns and rows with the attributes ColCount and RowCount in the object-inspector. Use the attribute Columns to set the column-names. Set AutoCreateRowSorter to true to enable sorting after column-names.

With the method setValueAt(Object aValue, int row, int column) you set a value at the position (row, column) and with getValueAt(int row, int column) you read it. getSelectedRow() gets the index of the first selected row.

Example: „Anna“ ist written in the first column of the selected row.

  int i = jTabelle.getSelectedRow();  
  jTabelle.setValueAt("Anna", i, 0);



en/gui-usage.txt · Zuletzt geändert: 2022/09/02 10:20 von 127.0.0.1