javax.swing.plaf.basic.BasicComboBoxUI Java Examples

The following examples show how to use javax.swing.plaf.basic.BasicComboBoxUI. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MaterialComboBox.java    From swing-material with MIT License 6 votes vote down vote up
public MaterialComboBox() {
    setModel(new DefaultComboBoxModel<T>());
    setRenderer(new FieldRenderer<T>(this));
    setUI(new BasicComboBoxUI() {
        @Override
        protected ComboPopup createPopup() {
            BasicComboPopup popup = new Popup(comboBox);
            popup.getAccessibleContext().setAccessibleParent(comboBox);
            return popup;
        }

        @Override
        protected JButton createArrowButton() {
            JButton button = new javax.swing.plaf.basic.BasicArrowButton(
                    javax.swing.plaf.basic.BasicArrowButton.SOUTH,
                    MaterialColor.TRANSPARENT,
                    MaterialColor.TRANSPARENT,
                    MaterialColor.TRANSPARENT,
                    MaterialColor.TRANSPARENT);
            button.setName("ComboBox.arrowButton");
            return button;
        }
    });
    setOpaque(false);
    setBackground(MaterialColor.TRANSPARENT);
}
 
Example #2
Source File: FlatComboBoxUI.java    From FlatLaf with Apache License 2.0 5 votes vote down vote up
@Override
protected LayoutManager createLayoutManager() {
	return new BasicComboBoxUI.ComboBoxLayoutManager() {
		@Override
		public void layoutContainer( Container parent ) {
			super.layoutContainer( parent );

			if ( editor != null && padding != null ) {
				// fix editor bounds by subtracting padding
				editor.setBounds( FlatUIUtils.subtractInsets( editor.getBounds(), padding ) );
			}
		}
	};
}
 
Example #3
Source File: FlatComboBoxUI.java    From FlatLaf with Apache License 2.0 5 votes vote down vote up
@Override
protected PropertyChangeListener createPropertyChangeListener() {
	return new BasicComboBoxUI.PropertyChangeHandler() {
		@Override
		public void propertyChange( PropertyChangeEvent e ) {
			super.propertyChange( e );

			Object source = e.getSource();
			String propertyName = e.getPropertyName();

			if( editor != null &&
				((source == comboBox && propertyName == "foreground") ||
				 (source == editor && propertyName == "enabled")) )
			{
				// fix editor component colors
				updateEditorColors();
			} else if( editor != null && source == comboBox && propertyName == "componentOrientation" ) {
				ComponentOrientation o = (ComponentOrientation) e.getNewValue();
				editor.applyComponentOrientation( o );
			} else if( editor != null && FlatClientProperties.PLACEHOLDER_TEXT.equals( propertyName ) )
				editor.repaint();
			else if( FlatClientProperties.COMPONENT_ROUND_RECT.equals( propertyName ) )
				comboBox.repaint();
			else if( FlatClientProperties.MINIMUM_WIDTH.equals( propertyName ) )
				comboBox.revalidate();
		}
	};
}
 
Example #4
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private static <E> JComboBox<E> makeComboBox(ComboBoxModel<E> model) {
  return new JComboBox<E>(model) {
    @Override public void updateUI() {
      super.updateUI();
      setBorder(BorderFactory.createEmptyBorder());
      setUI(new BasicComboBoxUI() {
        @Override protected JButton createArrowButton() {
          JButton button = super.createArrowButton();
          button.setContentAreaFilled(false);
          button.setBorder(BorderFactory.createEmptyBorder());
          return button;
        }
      });
      // JTextField editor = (JTextField) getEditor().getEditorComponent();
      // editor.setBorder(BorderFactory.createEmptyBorder());
      // editor.setOpaque(true);
      // editor.setEditable(false);
    }
  };
  // combo.setBorder(BorderFactory.createEmptyBorder());
  // ((JTextField) combo.getEditor().getEditorComponent()).setBorder(null);
  // ((JTextField) combo.getEditor().getEditorComponent()).setMargin(null);
  // combo.setBackground(Color.WHITE);
  // combo.setOpaque(true);
  // combo.setEditable(true);
  // return combo;
}
 
Example #5
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private static Component makePanel() {
  JPanel p = new JPanel(new BorderLayout(5, 5));
  p.add(new JComboBox<>(new String[] {"1111", "2222222", "3333333"}));

  String[] items = {"JComboBox 11111:", "JComboBox 222:", "JComboBox 33:"};
  JComboBox<String> comboBox = new JComboBox<String>(items) {
    @Override public void updateUI() {
      super.updateUI();
      UIManager.put("ComboBox.squareButton", Boolean.FALSE);
      UIManager.put("ComboBox.background", p.getBackground());
      setUI(new BasicComboBoxUI() {
        @Override protected JButton createArrowButton() {
          JButton button = new JButton(); // .createArrowButton();
          button.setBorder(BorderFactory.createEmptyBorder());
          button.setVisible(false);
          return button;
        }
      });
      ListCellRenderer<? super String> r = getRenderer();
      setRenderer((list, value, index, isSelected, cellHasFocus) -> {
        JLabel c = (JLabel) r.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        c.setHorizontalAlignment(SwingConstants.RIGHT);
        if (isSelected) {
          c.setForeground(list.getSelectionForeground());
          c.setBackground(list.getSelectionBackground());
        } else {
          c.setForeground(list.getForeground());
          c.setBackground(list.getBackground());
        }
        return c;
      });
      setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 2));
      setOpaque(false);
      setFocusable(false);
    }
  };
  p.add(comboBox, BorderLayout.WEST);
  p.setBorder(BorderFactory.createTitledBorder("JComboBox + JComboBox"));
  return p;
}
 
Example #6
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private static <E> JComboBox<E> makeComboBox(ComboBoxModel<E> model) {
  return new JComboBox<E>(model) {
    @Override public void updateUI() {
      super.updateUI();
      setBorder(BorderFactory.createEmptyBorder());
      setUI(new BasicComboBoxUI() {
        @Override protected JButton createArrowButton() {
          JButton button = super.createArrowButton();
          button.setContentAreaFilled(false);
          button.setBorder(BorderFactory.createEmptyBorder());
          return button;
        }
      });
      // JTextField editor = (JTextField) getEditor().getEditorComponent();
      // editor.setBorder(BorderFactory.createEmptyBorder());
      // editor.setOpaque(true);
      // editor.setEditable(false);
    }
  };
  // combo.setBorder(BorderFactory.createEmptyBorder());
  // ((JTextField) combo.getEditor().getEditorComponent()).setBorder(null);
  // ((JTextField) combo.getEditor().getEditorComponent()).setMargin(null);
  // combo.setBackground(Color.WHITE);
  // combo.setOpaque(true);
  // combo.setEditable(true);
  // return combo;
}
 
Example #7
Source File: MainPanel.java    From java-swing-tips with MIT License 4 votes vote down vote up
private MainPanel() {
  super(new BorderLayout());
  // Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
  //   @Override public void eventDispatched(AWTEvent event) {
  //     if (event instanceof MouseWheelEvent) {
  //       Object source = event.getSource();
  //       if (source instanceof JScrollPane) {
  //         System.out.println("JScrollPane");
  //         return;
  //       }
  //       ((MouseWheelEvent) event).consume();
  //     }
  //   }
  // }, AWTEvent.MOUSE_WHEEL_EVENT_MASK);

  JComboBox<String> combo1 = makeComboBox(5);
  if (combo1.getUI() instanceof WindowsComboBoxUI) {
    combo1.setUI(new WindowsComboBoxUI() {
      @Override protected ComboPopup createPopup() {
        return new BasicComboPopup2(comboBox);
      }
    });
  } else {
    combo1.setUI(new BasicComboBoxUI() {
      @Override protected ComboPopup createPopup() {
        return new BasicComboPopup2(comboBox);
      }
    });
  }

  JComboBox<String> combo2 = makeComboBox(20);
  if (combo2.getUI() instanceof WindowsComboBoxUI) {
    combo2.setUI(new WindowsComboBoxUI() {
      @Override protected ComboPopup createPopup() {
        return new BasicComboPopup3(comboBox);
      }
    });
  } else {
    combo2.setUI(new BasicComboBoxUI() {
      @Override protected ComboPopup createPopup() {
        return new BasicComboPopup3(comboBox);
      }
    });
  }

  Box box = Box.createVerticalBox();
  box.add(makeTitledPanel("default:", makeComboBox(5)));
  box.add(Box.createVerticalStrut(5));
  box.add(makeTitledPanel("default:", makeComboBox(20)));
  box.add(Box.createVerticalStrut(5));
  box.add(makeTitledPanel("disable right click in drop-down list:", combo1));
  box.add(Box.createVerticalStrut(5));
  box.add(makeTitledPanel("disable right click and scroll in drop-down list:", combo2));
  box.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
  add(box, BorderLayout.NORTH);
  setPreferredSize(new Dimension(320, 240));
}
 
Example #8
Source File: MainPanel.java    From java-swing-tips with MIT License 4 votes vote down vote up
private MainPanel() {
  super(new BorderLayout(15, 15));

  UIManager.put("ScrollBar.width", 10);
  UIManager.put("ScrollBar.thumbHeight", 20); // GTKLookAndFeel, SynthLookAndFeel, NimbusLookAndFeel
  UIManager.put("ScrollBar.minimumThumbSize", new Dimension(30, 30));
  UIManager.put("ScrollBar.incrementButtonGap", 0);
  UIManager.put("ScrollBar.decrementButtonGap", 0);
  UIManager.put("ScrollBar.thumb", THUMB);
  UIManager.put("ScrollBar.track", BACKGROUND);

  UIManager.put("ComboBox.foreground", FOREGROUND);
  UIManager.put("ComboBox.background", BACKGROUND);
  UIManager.put("ComboBox.selectionForeground", SELECTION_FOREGROUND);
  UIManager.put("ComboBox.selectionBackground", BACKGROUND);
  UIManager.put("ComboBox.buttonDarkShadow", BACKGROUND);
  UIManager.put("ComboBox.buttonBackground", FOREGROUND);
  UIManager.put("ComboBox.buttonHighlight", FOREGROUND);
  UIManager.put("ComboBox.buttonShadow", FOREGROUND);

  JComboBox<String> combo = new JComboBox<String>(makeModel()) {
    private transient MouseListener handler;
    private transient PopupMenuListener listener;
    @Override public void updateUI() {
      removeMouseListener(handler);
      removePopupMenuListener(listener);
      UIManager.put(KEY, new TopRoundedCornerBorder());
      super.updateUI();
      setUI(new BasicComboBoxUI() {
        @Override protected JButton createArrowButton() {
          JButton b = new JButton(new ArrowIcon(BACKGROUND, FOREGROUND));
          b.setContentAreaFilled(false);
          b.setFocusPainted(false);
          b.setBorder(BorderFactory.createEmptyBorder());
          return b;
        }

        @Override protected ComboPopup createPopup() {
          return new BasicComboPopup(comboBox) {
            @Override protected JScrollPane createScroller() {
              JScrollPane sp = new JScrollPane(list) {
                @Override public void updateUI() {
                  super.updateUI();
                  getVerticalScrollBar().setUI(new WithoutArrowButtonScrollBarUI());
                  getHorizontalScrollBar().setUI(new WithoutArrowButtonScrollBarUI());
                }
              };
              sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
              sp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
              sp.setHorizontalScrollBar(null);
              return sp;
            }
          };
        }
      });
      handler = new ComboRolloverHandler();
      addMouseListener(handler);
      listener = new HeavyWeightContainerListener();
      addPopupMenuListener(listener);
      Object o = getAccessibleContext().getAccessibleChild(0);
      if (o instanceof JComponent) {
        JComponent c = (JComponent) o;
        c.setBorder(new BottomRoundedCornerBorder());
        c.setForeground(FOREGROUND);
        c.setBackground(BACKGROUND);
      }
    }
  };

  JPanel p = new JPanel(new GridLayout(0, 1, 15, 15));
  p.setOpaque(true);
  p.add(combo);

  JTree tree = new JTree();
  int row = 0;
  while (row < tree.getRowCount()) {
    tree.expandRow(row);
    row++;
  }

  JScrollPane scroll = new JScrollPane(tree) {
    @Override public void updateUI() {
      super.updateUI();
      getVerticalScrollBar().setUI(new WithoutArrowButtonScrollBarUI());
      getHorizontalScrollBar().setUI(new WithoutArrowButtonScrollBarUI());
    }
  };
  scroll.setBackground(tree.getBackground());
  scroll.setBorder(new RoundedCornerBorder());

  setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
  add(scroll);
  add(p, BorderLayout.NORTH);
  setOpaque(true);
  setPreferredSize(new Dimension(320, 240));
}
 
Example #9
Source File: ComboBoxButtonImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
public HackyComboBoxUI(ComboBoxUI ui, ComboBoxButtonImpl button) {
  myButton = button;
  myDelegateUI = (BasicComboBoxUI)ui;
}