=====GUI-Usage=====
==== JLabel ====
{{:en:Jlabelicon.png}}
A JLabel component can display just text, or an image, or both. To display an image, just use a JLabel component.
Example: {{:en:Jlabel.png}}
With the method setLocation(int x, int y) of a JLabel-component you can animate an image.
\\
\\
----
==== JTextField ====
{{:en:Jtextfieldicon.png}}
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:
{{:en:Jtextfield.png}}
String Name = tfName.getText();
\\
----
==== JNumberField ====
{{:en:Jnumberfieldicon.png}}
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:
{{:en:JNumberfielden.png}}
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 ====
{{:en:jtextareaicon.png}}
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:
{{:en:Jtextareaen.png}}
taAusgabe.setText("Output:\n\n");
taAusgabe.append("Number " + Number + " found! \n");
\\
----
==== JButton ====
{{:en:Jbuttonicon.png}}
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 ====
{{:en:jcheckboxicon.png}}
A JCheckBox component is selected or not selected. You get the current state with ''isSelected()''.
Example:
{{:en:isselecteden.png}}
if (myCheckBox.isSelected()) ...
\\
----
==== ButtonGroup ====
{{:en:radiobuttonen.png}}
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 ====
{{:en:list.png}}
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 ====
{{:de:combobox.png}}
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 ====
{{:en:jspinner.png}}
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 ====
{{:de:jscrollbar.png}}
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 ====
{{:de:scrollpane.png}}
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 ====
{{:de:panel.png}}
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 ====
{{:de:canvas.png}}
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 ====
{{:de:fxturtle.png}}
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.
{{:de:playgroundturtle.png}}
Since Java-Editor version 13.00 the Turtle-component has changed. We now have an animated Turtle-Component based on the Turtle from [[http://www.aplu.ch/home/apluhomex.jsp|Ä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.
{{:de:turtle.png}}
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:
{{:de:turtlesetorigin.png}}
Since V13.00 the //setOrigin-method// is a method of the playground.
\\
----
==== JMenuBar ====
{{:de:menubardemo.png}}
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 ====
{{:de:jmenudemo.png}}
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 ====
{{:de:jpopupmenudemo.png}}
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.
{{:en:jpopupmenudemo2.png}}
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 ====
{{:de:timer.png}}
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 ====
{{:en:jtableicon.png}} 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);
\\
----