User Tools

Site Tools


java:swing

Lightweight Event Notification:

wird bei Events verwendet, die häufig auftreten

 slider.addChangeListener(new ChangeListener() {
     public void stateChanged(ChangeEvent e) {
         JSlider s = (JSlider)e.getSource();
         showStatus(Integer.toString(s.getValue()));
     }
 });

Stateful Event Notification

wird bei Events verwendet, die weniger häufig auftreten

 ... new PropertyChangeListener() {
     public void propertyChange(PropertyChangeEvent e) {
         e.getNewValue();
         e.getOldValue();
         e.getPropertyName();
     }
 }

misc

  • f.setBounds(x,y,w,h);
  • Math.random() –> 0..1
  • Boolean.TRUE, Boolean.FALSE –> (statische true/false-Objekte?)
  • set/getClientProperty: bound property!
  • Swing insets: am besten über EmptyBorder (S. 205)
  • ColorIcon-Implementierung: S. 223
  • ImageIcon loads synchronously!!
  • ImageIcon.getImageLoadStatus() returns MediaTracker.«constants»
  • SwingUtilities.isRightMouseButton();
  • ProgressMonitor S. 283
  • JLabel grau machen: JLabel.setEnabled(false);

Timer

 Timer aTimer = new Timer(periodicDelay, ActionListener);
 aTimer.setInitialDelay(ms); aTimer.setRepeats(false);
 aTimer.start(), stop(), restart(), isRunning();

MsgBox

JOptionPane.showMessageDialog(pa, msg, title, icon_type);

Swing Multithreading

  • repaint(), revalidate(), invalidate()
  • add/removeEventListener()
  • JCheckBoxMenuItem.setState() → sind thread-safe
  • SwingUtilities.invokeAndWait() → nicht vom Event-Disp-Thread aufrufen!
  • SwingUtilities.isEventDispatchThread();

Painting

repaint() erzeugt Message in Event Queue; wenn sofort gezeichnet werden soll, kann man paintImmediately() aufrufen.

RepaintManager.setDoubleBufferingEnabled(boolean);

ToolTips

ToolTipManager.setXxx() nachschauen
 JComponent.setToolTipText("...");

Keyboard Action (Hotkeys)

 JComponent.registerKeyboardAction(...);

plaf

 UIManager.setLookAndFeel("javax.swing.plaf.motif.MotifLookAndFeel");
     windows.WindowsLookAndFeel, metal.MetalLookAndFeel
 SwingUtilities.updateComponentTreeUI(getContentPane());

Button

 b.getActionCommand()
 b.addChangeListener(new ChangeListener() {
     public void stateChanged(ChangeEvent e) {
         ButtonModel model = b.getModel();
         if (model.isSelected()) ...
         if (model.isPressed()) ...
         if (model.isArmed()) ...
         if (model.isRollover()) ...
     }
 });
 rootPane.setDefaultButton(b);
 b.doClick(); // programmatically clicking a button
 b.setMnemonic(int/char);

ToggleButton

 b.addItemListener(new ItemListener() {
     public void itemStateChanged(ItemEvent e) {
         if (ItemEvent.SELECTED == e.getStateChange()) ...
     }
 });
 ButtonGroup grp = new ButtonGroup();
 for(i=1..N) grp.add(button[i]);
 
 startField.requestFocus(); // JS: obj.focus()

JMenu

 public void stateChanged(ChangeEvent e) {
     JMenuItem b = (JMenuItem)e.getSource();
     if (b.isArmed())
         applet.showStatus(b.getActionCommand());
 }
 
 JMenuItem.setAccelerator(
     KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.ALT_MASK));

JPopupMenu

 MouseEvent e; e.isPopupTrigger()
 JPopup.setDefaultLightWeightPopupEnabled(boolean);

JToolBar

 // Elemente: x.setAlignmentY(0.5f), x.setAlignmentX(0.5f)
 // Combos: c.setMaximumSize(c.getPreferredSize());
 // Text von Button wegnehmen:
 JButton b = toolbar.add(aAction); b.setText(null);
 toolbar.setFloatable(false);

Action (für Toolbar, Menu)

// single point of control for logical operation

Workerthread Pattern mit UI-Update

 class UpdateThread extends Thread {
     Runnable update;
     public UpdateThread() {
         update = new Runnable() {
             public void run() {
                 aSwingComponent.changeSomething();
             }
         }
     }
     public void run() {
         while(!fertig) {
             ... // Mann, braucht das lang!
             SwingUtitlities.invokeLater(update);
         }
     }
 }
 
 aStartButton.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e) {
         UpdateThread thread = new UpdateThread();
         thread.start();
     }
 });

JTabbedPane

 tp.addTab("Extras", new ImageIcon("x.gif"), extraPanel, "Tooltip");
 tp.setTabPlacement(TOP/BOTTOM/LEFT/RIGHT);
 tp.addChangeListener();
 tp.getSelectedIndex(); tp.getSelectedComponent()
 tp.getTitleAt(index); // index start at 0
 tp.setEnabledAt(index);
 tp.indexOfComponent(Component);

JSplitPane

 sp.getTopComponent() = sp.getLeftComponent();
 aLeftComponent.setMinimumSize();
 sp.setContinuousLayout(boolean);
 // Dividerlocation: sp.addPropertyChangeListener()
 sp.setDividerLocation(double proportionalLocation);
java/swing.txt · Last modified: 2010/05/29 00:08 (external edit)