На главную Наши проекты:
Журнал   ·   Discuz!ML   ·   Wiki   ·   DRKB   ·   Помощь проекту
ПРАВИЛА FAQ Помощь Участники Календарь Избранное RSS
msm.ru
! Правила раздела Java FAQ
1. Данный раздел предназначен только для публикации готовых статей, с вопросами обращайтесь в соответствующие подразделы.
2. Все вопросы, связанные с ошибками или неточностями в представленных материалах направляйте модераторам персональным сообщением.
3. Все темы и сообщения в разделе премодерируются. Любое сообщение или тема будут доступны остальным участникам после одобрения модераторами.
Модераторы: dark_barker, wind
  
> Индикатор раскладки клавиатуры
    ExpandedWrap disabled
      import java.awt.GridBagLayout;
      import java.awt.Window;
      import java.awt.event.HierarchyEvent;
      import java.awt.event.HierarchyListener;
      import java.util.ArrayList;
      import java.util.List;
      import java.util.Locale;
      import java.util.Map;
      import java.util.WeakHashMap;
       
      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.SwingUtilities;
       
      public class JInputLocaleIndicator extends JLabel {
       
          private static class MonitoringThread extends Thread {
              
              private static final long REFRESH_TIMEOUT = 1000L;
       
              public MonitoringThread() {
                  setPriority(NORM_PRIORITY);
                  setDaemon(true);
              }
              
              public void run() {
                  while (!interrupted()) {
                      synchronized (indicatorLists) {
                          for (Map.Entry<Window, List<JLabel>> entry : indicatorLists.entrySet()) {
                              Locale locale = entry.getKey().getInputContext().getLocale();
                              for (JLabel label : entry.getValue()) {
                                  label.setText(locale.getLanguage());
                                  label.setToolTipText(locale.getDisplayLanguage());
                              }
                          }
                      }
                      
                      try {
                          Thread.sleep(REFRESH_TIMEOUT);
                      }
                      catch (InterruptedException thrown) {
                          break;
                      }
                  }
              }
              
          }
          
          private static Map<Window, List<JLabel>> indicatorLists = new WeakHashMap<Window, List<JLabel>>();
          
          static {
              MonitoringThread monitoringThread = new MonitoringThread();
              monitoringThread.start();
          }
       
          public JInputLocaleIndicator() {
              addHierarchyListener(
                  new HierarchyListener() {
                      public void hierarchyChanged(HierarchyEvent event) {
                          if ((event.getChangeFlags() & HierarchyEvent.DISPLAYABILITY_CHANGED) != 0) {
                              Window window = SwingUtilities.getWindowAncestor(JInputLocaleIndicator.this);
       
                              synchronized (indicatorLists) {
                                  List<JLabel> indicators = indicatorLists.get(window);
       
                                  if (isDisplayable()) {
                                      if (indicators == null) {
                                          indicatorLists.put(window, indicators = new ArrayList<JLabel>());
                                      }
                                      
                                      indicators.add(JInputLocaleIndicator.this);
                                  }
                                  else {
                                      if (indicators != null) {
                                          indicators.remove(JInputLocaleIndicator.this);
                                      }
                                  }
                              }
                          }
                      }
                  }
              );
          }
          
          public static void main(String... args) {
              JFrame frame = new JFrame();
              JInputLocaleIndicator indicator = new JInputLocaleIndicator();
              
              frame.setLayout(
                  new GridBagLayout()
              );
              
              frame.add(indicator);
              frame.setSize(512, 384);
              frame.setLocationRelativeTo(null);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setVisible(true);
          }
          
      }
    0 пользователей читают эту тему (0 гостей и 0 скрытых пользователей)
    0 пользователей:


    Рейтинг@Mail.ru
    [ Script execution time: 0,0189 ]   [ 16 queries used ]   [ Generated: 28.03.24, 13:14 GMT ]