Benutzer-Werkzeuge

Webseiten-Werkzeuge


en:gui-usage

Dies ist eine alte Version des Dokuments!


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()) ...



JRadioButton & JButtonGroup

JRadiobuttons offer different options, from which you can choose just one. To group the JRadioButtons you have to place an invisible ButtonGroup component on the form. Write the name of this ButtonGroup for each JRadioButton as value of the attribute ButtonGroup in the object inspector.

The Java-Editor places this method into the java source code:

  public String buttonGroup1_getSelectedRadioButtonLabel() { ... }

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

  if (bgColor_getSelectedRadioButtonLabel().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.

The turtle which belongs to the Java-Editor is an example of a Canvas-subclass.


Turtle

The Turtle-component provides a drawing-area, on which you can draw with turtle-commands. You find them in the documentation of the Turtle-class.

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:

The Turtle and it's documentation must be installed inside des Java-Editor configuration.



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.


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);

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();
en/gui-usage.1420145760.txt.gz · Zuletzt geändert: 2015/01/01 22:56 (Externe Bearbeitung)