Java by API/java.awt/Container

Материал из Java эксперт
Версия от 14:23, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Container: addPropertyChangeListener(PropertyChangeListener listener)

  

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import javax.swing.JFileChooser;
public class Main {
  public static void main(String[] argv) throws Exception {
    JFileChooser chooser = new JFileChooser();
    chooser.addPropertyChangeListener(new PropertyChangeListener() {
      public void propertyChange(PropertyChangeEvent evt) {
        if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(evt.getPropertyName())) {
          JFileChooser chooser = (JFileChooser) evt.getSource();
          File oldFile = (File) evt.getOldValue();
          File newFile = (File) evt.getNewValue();
          
          File curFile = chooser.getSelectedFile();
        }       
      }
    });
  }
}





Container: getComponentCount()

  
import java.awt.ruponent;
import java.awt.ruponentOrientation;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
public class MainClass extends JFrame {
  public MainClass() {
    super("MainClass");
    Container contentPane = getContentPane();
    contentPane.setLayout(new FlowLayout());
    contentPane.add(new JButton("OK"));
    contentPane.add(new JButton("Cancel"));
    applyOrientation(this, ComponentOrientation.RIGHT_TO_LEFT);
  }
  public static void main(String[] argv) {
    JFrame frame = new MainClass();
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    frame.pack();
    frame.setVisible(true);
  }
  private void applyOrientation(Component c, ComponentOrientation o) {
    c.setComponentOrientation(o);
    if (c instanceof JMenu) {
      JMenu menu = (JMenu) c;
      int ncomponents = menu.getMenuComponentCount();
      for (int i = 0; i < ncomponents; ++i) {
        applyOrientation(menu.getMenuComponent(i), o);
      }
    } else if (c instanceof Container) {
      Container container = (Container) c;
      int ncomponents = container.getComponentCount();
      for (int i = 0; i < ncomponents; ++i) {
        applyOrientation(container.getComponent(i), o);
      }
    }
  }
}





Container: getFocusTraversalPolicy()

  

import java.awt.ruponent;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.FocusTraversalPolicy;
import java.awt.KeyboardFocusManager;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main {
  public static void main(String[] argv) {
    JFrame frame = new JFrame();
    JButton component1 = new JButton("1");
    JButton component2 = new JButton("2");
    JButton component3 = new JButton("3");
    frame.setLayout(new FlowLayout());
    frame.add(component1);
    frame.add(component2);
    frame.add(component3);
    frame.pack();
    frame.setVisible(true);
    System.out.println(findNextFocus().getName());
  }
  public static Component findNextFocus() {
    Component c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
    Container root = c.getFocusCycleRootAncestor();
    FocusTraversalPolicy policy = root.getFocusTraversalPolicy();
    Component nextFocus = policy.getComponentAfter(root, c);
    if (nextFocus == null) {
      nextFocus = policy.getDefaultComponent(root);
    }
    return nextFocus;
  }
}





Container: setFocusTraversalPolicy(FocusTraversalPolicy policy)

  
import java.awt.ruponent;
import java.awt.Container;
import java.awt.FocusTraversalPolicy;
import java.awt.GridLayout;
import java.util.Arrays;
import java.util.ruparator;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SortingFocusTraversalPolicy;
public class Main {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Reverse Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridLayout(3, 3));
    for (int i = 0; i < 9; i++) {
      JButton button = new JButton(Integer.toString(i));
      frame.add(button);
    }
    final Container contentPane = frame.getContentPane();
    Comparator<Component> comp = new Comparator<Component>() {
      public int compare(Component c1, Component c2) {
        Component comps[] = contentPane.getComponents();
        List list = Arrays.asList(comps);
        int first = list.indexOf(c1);
        int second = list.indexOf(c2);
        return second - first;
      }
    };
    FocusTraversalPolicy policy = new SortingFocusTraversalPolicy(comp);
    frame.setFocusTraversalPolicy(policy);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}