6.1.4. Jowidgets Code in Swing Projekte integrieren

Die Integration in Swing funktioniert analog zu SWT. Es folgen die gleichen Beispiele wie für SWT jetzt für Swing.

SwingToJoFramesSnipped

Das SwingToJoFramesSnipped demonstriert die Erzeugung von Fenstern innerhalb einer nativen Swing Applikation:

  1  package org.jowidgets.examples.swing.snipped;
  2  
  3  import java.awt.event.ActionEvent;
  4  import java.awt.event.ActionListener;
  5  
  6  import javax.swing.JButton;
  7  import javax.swing.JFrame;
  8  import javax.swing.SwingUtilities;
  9  import javax.swing.UIManager;
 10  
 11  import net.miginfocom.swing.MigLayout;
 12  
 13  import org.jowidgets.api.toolkit.Toolkit;
 14  import org.jowidgets.api.widgets.IFrame;
 15  import org.jowidgets.api.widgets.blueprint.IDialogBluePrint;
 16  import org.jowidgets.api.widgets.blueprint.IFrameBluePrint;
 17  import org.jowidgets.common.types.Dimension;
 18  import org.jowidgets.tools.widgets.blueprint.BPF;
 19  
 20  public final class SwingToJoFramesSnipped {
 21  
 22      private SwingToJoFramesSnipped() {}
 23  
 24      public static void main(final String[] args) throws Exception {
 25          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
 26          SwingUtilities.invokeLater(new Runnable() {
 27              @Override
 28              public void run() {
 29                  createAndShowJFrame();
 30              }
 31          });
 32      }
 33  
 34      private static void createAndShowJFrame() {
 35          //create the root frame with swing
 36          final JFrame frame = new JFrame();
 37          frame.setSize(1024, 768);
 38          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 39          frame.setLayout(new MigLayout("", "[]", "[][]"));
 40  
 41          //add button to open a jowidgets root frame
 42          final JButton frameButton = new JButton("Create root frame");
 43          frame.add(frameButton);
 44          frameButton.addActionListener(new ActionListener() {
 45              @Override
 46              public void actionPerformed(final ActionEvent e) {
 47                  final IFrameBluePrint frameBp = BPF.frame("Jowidgets Root Frame");
 48                  frameBp.setSize(new Dimension(400, 300));
 49                  frameBp.setAutoDispose(true);
 50                  final IFrame frame = Toolkit.createRootFrame(frameBp);
 51                  frame.add(BPF.textLabel("This is a jowidgets root frame"));
 52                  frame.setVisible(true);
 53              }
 54          });
 55  
 56          //add button to open a jowidgets modal dialog
 57          final JButton dialogButton = new JButton("Create dialog");
 58          frame.add(dialogButton);
 59          dialogButton.addActionListener(new ActionListener() {
 60              @Override
 61              public void actionPerformed(final ActionEvent e) {
 62                  final IDialogBluePrint dialogBp = BPF.dialog("Jowidgets dialog");
 63                  dialogBp.setSize(new Dimension(400, 300));
 64                  final IFrame dialog = Toolkit.getWidgetFactory().create(frame, dialogBp);
 65                  dialog.add(BPF.textLabel("This is a jowidgets modal dialog"));
 66                  dialog.setVisible(true);
 67              }
 68          });
 69  
 70          //show the frame
 71          frame.setVisible(true);
 72      }
 73  
 74  }
SwingToJoCompositeSnipped

Das SwingToJoCompositeSnipped demonstriert die Erzeugung eines IComposite Wrapper innerhalb einer nativen Swing Applikation:

  1  package org.jowidgets.examples.swing.snipped;
  2  
  3  import javax.swing.JFrame;
  4  import javax.swing.JPanel;
  5  import javax.swing.SwingUtilities;
  6  import javax.swing.UIManager;
  7  
  8  import net.miginfocom.swing.MigLayout;
  9  
 10  import org.jowidgets.api.widgets.IComposite;
 11  import org.jowidgets.api.widgets.IInputField;
 12  import org.jowidgets.common.widgets.controller.IInputListener;
 13  import org.jowidgets.common.widgets.layout.MigLayoutDescriptor;
 14  import org.jowidgets.spi.impl.swing.addons.SwingToJoWrapper;
 15  import org.jowidgets.tools.widgets.blueprint.BPF;
 16  
 17  public final class SwingToJoCompositeSnipped {
 18  
 19      private SwingToJoCompositeSnipped() {}
 20  
 21      public static void main(final String[] args) throws Exception {
 22          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
 23          SwingUtilities.invokeLater(new Runnable() {
 24              @Override
 25              public void run() {
 26                  createAndShowJFrame();
 27              }
 28          });
 29      }
 30  
 31      private static void createAndShowJFrame() {
 32          //create the root frame with swing
 33          final JFrame frame = new JFrame("SwingToJo composite snipped");
 34          frame.setSize(400, 300);
 35          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 36          frame.setLayout(new MigLayout("", "0[grow]0", "0[grow]0"));
 37  
 38          //create a jpanel
 39          final JPanel panel = new JPanel();
 40          frame.add(panel, "growx, growy");
 41  
 42          //create a jowidgets composite wrapper and do some jowidgets stuff
 43          final IComposite joComposite = SwingToJoWrapper.create(panel);
 44          joComposite.setLayout(new MigLayoutDescriptor("[][grow]", "[]"));
 45  
 46          //add a label
 47          joComposite.add(BPF.textLabel("Name"));
 48  
 49          //add a input field for double values
 50          final IInputField<String> nameField = joComposite.add(BPF.inputFieldString(), "grow x");
 51          nameField.addInputListener(new IInputListener() {
 52              @Override
 53              public void inputChanged() {
 54                  //CHECKSTYLE:OFF
 55                  System.out.println("Hello " + nameField.getValue());
 56                  //CHECKSTYLE:ON
 57              }
 58          });
 59  
 60          //show the frame
 61          frame.setVisible(true);
 62      }
 63  
 64  }

Siehe auch PDF Version dieses Dokuments, Jowidgets API Spezifikation