Google Tag Manager

2010/09/21

SearchBar JComboBox

Code

public class BasicSearchBarComboBoxUI extends SearchBarComboBoxUI {
  public static javax.swing.plaf.ComponentUI createUI(JComponent c) {
    return new BasicSearchBarComboBoxUI();
  }
  protected boolean isEditable = true;
  @Override protected void installDefaults() {
    super.installDefaults();
    //comboBox.setEditable(true);
    comboBox.setPreferredSize(new Dimension(0, 20));
    comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
    //comboBox.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
  }
  @Override protected LayoutManager createLayoutManager() {
    return new LayoutManager() {
      @Override public void addLayoutComponent(String name, Component comp) {}
      @Override public void removeLayoutComponent(Component comp) {}
      @Override public Dimension preferredLayoutSize(Container parent) {
        return parent.getPreferredSize();
      }
      @Override public Dimension minimumLayoutSize(Container parent) {
        return parent.getMinimumSize();
      }
      @Override public void layoutContainer(Container parent) {
        if(!(parent instanceof JComboBox)) return;
        JComboBox cb   = (JComboBox)parent;
        int width    = cb.getWidth();
        int height     = cb.getHeight();
        Insets insets  = cb.getInsets();
        int buttonHeight = height - (insets.top + insets.bottom);
        int buttonWidth  = buttonHeight;
        int loupeWidth   = buttonHeight;

        JButton arrowButton = (JButton)cb.getComponent(0);
        if(arrowButton != null) {
          Insets arrowInsets = arrowButton.getInsets();
          buttonWidth = arrowButton.getPreferredSize().width +
                        arrowInsets.left + arrowInsets.right;
          arrowButton.setBounds(insets.left, insets.top,
                                buttonWidth, buttonHeight);
        }
        JButton loupeButton = null;
        for(Component c: cb.getComponents()) {
          if("ComboBox.loupeButton".equals(c.getName())) {
            loupeButton = (JButton)c;
            break;
          }
        }
        //= (JButton)cb.getComponent(3);
        if(loupeButton != null) {
          Insets loupeInsets = loupeButton.getInsets();
          loupeWidth = loupeButton.getPreferredSize().width +
                       loupeInsets.left + loupeInsets.right;
          loupeButton.setBounds(width - (insets.right + loupeWidth), insets.top,
                                loupeWidth, buttonHeight);
        }
        JTextField editor = (JTextField)cb.getEditor().getEditorComponent();
        //JTextField editor = (JTextField)cb.getComponent(1);
        if(editor != null) {
          editor.setBounds(insets.left + buttonWidth, insets.top,
                   width  - (insets.left + insets.right + buttonWidth + loupeWidth),
                   height - (insets.top  + insets.bottom));
        }
      }
    };
  }
//......

References

2010/09/13

Placeholder for an empty JTable

Code

JEditorPane hint = new JEditorPane("text/html", ">html<>a href='dummy'<No data!>/a<>/html<");

table.setFillsViewportHeight(true);
table.setLayout(new GridBagLayout());
table.add(hint);

model.addTableModelListener(new TableModelListener() {
  @Override public void tableChanged(TableModelEvent e) {
    DefaultTableModel model = (DefaultTableModel)e.getSource();
    hint.setVisible(model.getRowCount()==0);
  }
});

References