JScrollBar: setFocusable(boolean focusable) : JScrollBar « javax.swing « Java by API






JScrollBar: setFocusable(boolean focusable)

 
import java.awt.Dimension;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

public class Main extends JPanel implements FocusListener {
  public Main() {
    JTextField textField = new JTextField("A TextField");
    textField.addFocusListener(this);
    JLabel label = new JLabel("A Label");
    label.addFocusListener(this);
    add(label);

    String comboPrefix = "ComboBox Item #";
    final int numItems = 15;
    Vector vector = new Vector(numItems);
    for (int i = 0; i < numItems; i++) {
      vector.addElement(comboPrefix + i);
    }
    JComboBox comboBox = new JComboBox(vector);
    comboBox.addFocusListener(this);
    add(comboBox);

    JButton button = new JButton("A Button");
    button.addFocusListener(this);
    add(button);

    String listPrefix = "List Item #";
    Vector listVector = new Vector(numItems);
    for (int i = 0; i < numItems; i++) {
      listVector.addElement(listPrefix + i);
    }
    JList list = new JList(listVector);
    list.setSelectedIndex(1);
    list.addFocusListener(this);
    JScrollPane listScrollPane = new JScrollPane(list);

    listScrollPane.getVerticalScrollBar().setFocusable(false);
    listScrollPane.getHorizontalScrollBar().setFocusable(false);
    add(listScrollPane);

    setPreferredSize(new Dimension(450, 450));
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
  }

  void displayMessage(String prefix, FocusEvent e) {
    System.out.println(prefix
        + (e.isTemporary() ? " (temporary):" : ":")
        + e.getComponent().getClass().getName()
        + "; Opposite component: "
        + (e.getOppositeComponent() != null ? e.getOppositeComponent().getClass().getName()
            : "null"));
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame("FocusEventDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JComponent newContentPane = new Main();
    newContentPane.setOpaque(true); // content panes must be opaque
    frame.setContentPane(newContentPane);
    frame.pack();
    frame.setVisible(true);

  }

  public void focusGained(FocusEvent e) {
    displayMessage("Focus gained", e);
  }

  public void focusLost(FocusEvent e) {
    displayMessage("Focus lost", e);
  }
}

   
  








Related examples in the same category

1.JScrollBar.HORIZONTAL
2.new JScrollBar(int orientation)
3.new JScrollBar(int orientation, int value, int extent, int min, int max)
4.JScrollBar: addAdjustmentListener(AdjustmentListener l)
5.JScrollBar: getBlockIncrement(int direction)
6.JScrollBar: getMinimum()
7.JScrollBar: getModel()
8.JScrollBar: getUnitIncrement(int direction)
9.JScrollBar: setBlockIncrement(int blockIncrement)
10.JScrollBar: setUnitIncrement(int unitIncrement)
11.JScrollBar: setValue(int value)