use of java.awt.event.FocusListener in project jdk8u_jdk by JetBrains.
the class bug4973721 method main.
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel("javax.swing.plaf.synth.SynthLookAndFeel");
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
final JFrame frame = new JFrame();
spinner = new JSpinner();
frame.getContentPane().add(spinner);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
spinner.addChangeListener((ChangeListener) listener);
spinner.addFocusListener((FocusListener) listener);
spinner.requestFocus();
}
});
synchronized (listener) {
if (!bFocusGained) {
System.out.println("waiting focusGained...");
try {
listener.wait(5000);
} catch (InterruptedException e) {
}
}
}
boolean hasFocus = Util.invokeOnEDT(new java.util.concurrent.Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return spinner.hasFocus();
}
});
if (!bFocusGained && !hasFocus) {
throw new RuntimeException("Couldn't request focus for spinner");
}
Robot robot = new Robot();
robot.setAutoDelay(50);
Util.hitKeys(robot, KeyEvent.VK_UP);
toolkit.realSync();
Thread.sleep(1000);
if (!bStateChanged) {
throw new RuntimeException("Up arrow key button doesn't work for a spinner in Synth L&F");
}
bStateChanged = false;
Util.hitKeys(robot, KeyEvent.VK_DOWN);
toolkit.realSync();
Thread.sleep(1000);
if (!bStateChanged) {
throw new RuntimeException("Down arrow key button doesn't work for a spinner in Synth L&F");
}
}
use of java.awt.event.FocusListener in project knime-core by knime.
the class LimitRowsPanel method getLimitPanel.
private Container getLimitPanel() {
m_limitRows = new JCheckBox("Read only the first");
m_maxNumber = new JTextField();
m_maxNumber.setColumns(6);
m_maxNumber.setPreferredSize(new Dimension(125, 25));
m_maxNumber.setMaximumSize(new Dimension(125, 25));
m_limitRows.setSelected(false);
m_maxNumber.setEnabled(false);
// make sure we always have a valid value. Reject invalid characters.
m_maxNumber.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void removeUpdate(final DocumentEvent e) {
checkAndFixTextfield();
}
@Override
public void insertUpdate(final DocumentEvent e) {
checkAndFixTextfield();
}
@Override
public void changedUpdate(final DocumentEvent e) {
checkAndFixTextfield();
}
});
m_maxNumber.addFocusListener(new FocusListener() {
@Override
public void focusLost(final FocusEvent e) {
if (m_maxNumber.getText().trim().length() == 0) {
// can't handle no empty strings.
m_lastValidValue = "0";
m_maxNumber.setText(m_lastValidValue);
}
}
@Override
public void focusGained(final FocusEvent e) {
}
});
m_limitRows.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(final ChangeEvent e) {
// en/disable the textfield
m_maxNumber.setEnabled(m_limitRows.isSelected());
// also - if the textfield is enabled and empty set a value
if (m_maxNumber.isEnabled()) {
if ((m_maxNumber.getText() == null) || (m_maxNumber.getText().trim().length() == 0)) {
// set a valid value, so we can safely assume that if there is a value in there it's always valid.
m_maxNumber.setText("1000");
m_lastValidValue = "1000";
}
}
}
});
Box result = Box.createHorizontalBox();
result.add(m_limitRows);
result.add(Box.createHorizontalStrut(3));
result.add(m_maxNumber);
result.add(Box.createHorizontalStrut(3));
result.add(new JLabel("table rows from the file."));
result.add(Box.createHorizontalGlue());
return result;
}
use of java.awt.event.FocusListener in project keystore-explorer by kaikramer.
the class DDateTimeChooser method initComponents.
private void initComponents(Date date) {
jcbMonth = new JComboBox<Object>(MONTH_NAMES);
jcbMonth.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent evt) {
update();
}
});
jsYear = new JSpinner(new SpinnerListModel(YEARS));
jsYear.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent evt) {
update();
}
});
jlDaysOfMonth = new JLabel[7][7];
for (int i = 0; i < 7; i++) {
jlDaysOfMonth[0][i] = new JLabel(DAY_NAMES[i], SwingConstants.RIGHT);
jlDaysOfMonth[0][i].setOpaque(true);
jlDaysOfMonth[0][i].setForeground(WEEK_DAY_FOREGROUND);
jlDaysOfMonth[0][i].setBackground(WEEK_DAY_BACKGROUND);
jlDaysOfMonth[0][i].setBorder(new EmptyBorder(0, 0, 0, 3));
}
for (int i = 1; i < 7; i++) {
for (int j = 0; j < 7; j++) {
jlDaysOfMonth[i][j] = new JLabel(EMPTY_DAY, SwingConstants.RIGHT);
jlDaysOfMonth[i][j].setOpaque(true);
jlDaysOfMonth[i][j].setForeground(DAY_FOREGROUND);
jlDaysOfMonth[i][j].setBackground(DAY_BACKGROUND);
jlDaysOfMonth[i][j].addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent evt) {
selectDay((JLabel) evt.getSource());
}
});
jlDaysOfMonth[i][j].setBorder(new CompoundBorder(new MatteBorder(2, 0, 0, 0, Color.WHITE), new EmptyBorder(0, 0, 0, 3)));
}
}
jpMonthYear = new JPanel();
jpMonthYear.add(jcbMonth);
jpMonthYear.add(jsYear);
jpDaysOfMonth = new JPanel(new DayOfMonthGridLayout(7, 7));
jpDaysOfMonth.setBackground(DAY_BACKGROUND);
jpDaysOfMonth.setBorder(new CompoundBorder(BorderFactory.createLoweredBevelBorder(), new EmptyBorder(2, 2, 2, 2)));
jpDaysOfMonth.setFocusable(true);
jpDaysOfMonth.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent evt) {
setSelectedDay(jlSelectedDayOfMonth);
}
@Override
public void focusLost(FocusEvent evt) {
setSelectedDay(jlSelectedDayOfMonth);
}
});
jpDaysOfMonth.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent evt) {
calendarKeyboardNavigation(evt);
}
});
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
jpDaysOfMonth.add(jlDaysOfMonth[i][j]);
}
}
jsHour = new JSpinner(new SpinnerListModel(HOURS));
jlTimeSeparator1 = new JLabel(":");
jsMinute = new JSpinner(new SpinnerListModel(MINUTES));
jlTimeSeparator2 = new JLabel(":");
jsSecond = new JSpinner(new SpinnerListModel(SECONDS));
jpTime = new JPanel(new FlowLayout(FlowLayout.CENTER));
jpTime.add(jsHour);
jpTime.add(jlTimeSeparator1);
jpTime.add(jsMinute);
jpTime.add(jlTimeSeparator2);
jpTime.add(jsSecond);
JPanel jpDateTime = new JPanel(new BorderLayout(0, 0));
jpDateTime.setBorder(new CompoundBorder(new EmptyBorder(5, 5, 5, 5), new CompoundBorder(new EtchedBorder(), new EmptyBorder(5, 5, 5, 5))));
jpDateTime.add(BorderLayout.NORTH, jpMonthYear);
jpDateTime.add(BorderLayout.CENTER, jpDaysOfMonth);
jpDateTime.add(BorderLayout.SOUTH, jpTime);
jbOK = new JButton(res.getString("DDateTimeChooser.jbOK.text"));
jbOK.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
okPressed();
}
});
jbCancel = new JButton(res.getString("DDateTimeChooser.jbCancel.text"));
jbCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
cancelPressed();
}
});
jbCancel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), CANCEL_KEY);
jbCancel.getActionMap().put(CANCEL_KEY, new AbstractAction() {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent evt) {
cancelPressed();
}
});
jpButtons = PlatformUtil.createDialogButtonPanel(jbOK, jbCancel, false);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(BorderLayout.CENTER, jpDateTime);
getContentPane().add(BorderLayout.SOUTH, jpButtons);
if (date == null) {
date = new Date();
}
populate(date);
setResizable(false);
getRootPane().setDefaultButton(jbOK);
pack();
}
use of java.awt.event.FocusListener in project Spark by igniterealtime.
the class FrequentContactsPlugin method initialize.
public void initialize() {
transcriptDir = new File(SparkManager.getUserDirectory(), "transcripts");
contacts = new JList(model);
contacts.setCellRenderer(new InternalRenderer());
window = new Window(SparkManager.getMainWindow());
final JPanel mainPanel = new JPanel(new BorderLayout());
final JLabel titleLabel = new JLabel(Res.getString("label.frequent.contacts"));
titleLabel.setFont(new Font("Dialog", Font.BOLD, 11));
titleLabel.setHorizontalAlignment(JLabel.CENTER);
mainPanel.add(titleLabel, BorderLayout.NORTH);
mainPanel.add(contacts, BorderLayout.CENTER);
mainPanel.setBorder(BorderFactory.createLineBorder(Color.gray));
window.add(mainPanel);
// Add Listeners
contacts.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
contacts.setSelectedIndex(contacts.locationToIndex(e.getPoint()));
String user = jidMap.get(contacts.getSelectedValue());
ContactItem contact = SparkManager.getContactList().getContactItemByJID(user);
SparkManager.getContactList().setSelectedUser(contact.getJID());
SparkManager.getContactList().showPopup(contacts, e, contact);
}
if (e.getClickCount() == 2) {
final JLabel label = (JLabel) contacts.getSelectedValue();
String user = jidMap.get(label);
if (user != null) {
final String contactUsername = SparkManager.getUserManager().getUserNicknameFromJID(user);
SparkManager.getChatManager().activateChat(user, contactUsername);
window.dispose();
}
}
}
});
contacts.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
if (e.getKeyChar() == KeyEvent.VK_ENTER) {
final JLabel label = (JLabel) contacts.getSelectedValue();
String user = jidMap.get(label);
if (user != null) {
final String contactUsername = SparkManager.getUserManager().getUserNicknameFromJID(user);
SparkManager.getChatManager().activateChat(user, contactUsername);
window.dispose();
}
} else if (e.getKeyChar() == KeyEvent.VK_ESCAPE) {
window.dispose();
}
}
});
contacts.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
}
public void focusLost(FocusEvent e) {
window.dispose();
}
});
// Add KeyMappings
SparkManager.getMainWindow().getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_T, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "favoritePeople");
SparkManager.getMainWindow().getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_T, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "favoritePeople");
SparkManager.getMainWindow().getRootPane().getActionMap().put("favoritePeople", new AbstractAction("favoritePeople") {
private static final long serialVersionUID = 6836584242669218932L;
public void actionPerformed(ActionEvent e) {
// Show History Popup
showPopup();
}
});
}
use of java.awt.event.FocusListener in project airavata by apache.
the class SwingUtil method addPlaceHolder.
public static void addPlaceHolder(final JTextField field, final String placeHolderText) {
field.addFocusListener(new FocusListener() {
private Color fontColor = field.getForeground();
// private String previousText=field.getText();
public void focusGained(FocusEvent arg0) {
if (field.getText().equals(placeHolderText)) {
field.setText("");
}
field.setForeground(fontColor);
}
public void focusLost(FocusEvent arg0) {
if (field.getText().trim().equals("")) {
fontColor = field.getForeground();
field.setForeground(Color.GRAY);
field.setText(placeHolderText);
}
}
});
if (field.getText().trim().equals("")) {
field.setText(placeHolderText);
field.setForeground(Color.GRAY);
}
}
Aggregations