From 9d733bb72ae9efd65913e423ea387fc609abb125 Mon Sep 17 00:00:00 2001 From: Alessandro Falappa Date: Fri, 12 Feb 2016 18:05:06 +0100 Subject: [PATCH 1/2] Created small demo application using javax.swing.GroupLayout --- demo/combos/forms/ComboDemo.java | 89 ++++++++++++++++++++++++++++++++ demo/demo.iml | 4 +- 2 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 demo/combos/forms/ComboDemo.java diff --git a/demo/combos/forms/ComboDemo.java b/demo/combos/forms/ComboDemo.java new file mode 100644 index 0000000..52f29b7 --- /dev/null +++ b/demo/combos/forms/ComboDemo.java @@ -0,0 +1,89 @@ +package forms; + +import com.bulenkov.darcula.DarculaLaf; + +import javax.swing.*; +import java.awt.*; + +/** + * Combo box demo. Created by sasha on 12/02/16. + */ +public class ComboDemo { + public static void main(String[] args) throws UnsupportedLookAndFeelException { + UIManager.setLookAndFeel(new DarculaLaf()); + JFrame f = new JFrame("Combo boxes"); + f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + Container contentPane = f.getContentPane(); + + JLabel label = new JLabel(); + JComboBox comboBox = new JComboBox(); + JTextField textField = new JTextField(); + JComboBox comboBox2 = new JComboBox(); + JButton button = new JButton(); + JComboBox comboBox3 = new JComboBox(); + JTextArea textArea = new JTextArea(); + JComboBox comboBox4 = new JComboBox(); + + label.setText("label"); + comboBox.setModel(new DefaultComboBoxModel(new String[]{"Item 1", "Item 2", "Item 3", "Item 4"})); + textField.setText("textfield"); + comboBox2.setModel(new DefaultComboBoxModel(new String[]{"Item 1", "Item 2", "Item 3", "Item 4"})); + button.setText("button"); + comboBox3.setModel(new DefaultComboBoxModel(new String[]{"Item 1", "Item 2", "Item 3", "Item 4"})); + textArea.setRows(5); + textArea.setColumns(10); + textArea.setText("textArea\nother text\nanother text"); + comboBox4.setModel(new DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); + GroupLayout layout = new GroupLayout(contentPane); + contentPane.setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addComponent(label) + .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) + .addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) + .addGroup(layout.createSequentialGroup() + .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) + .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) + .addComponent(comboBox2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) + .addGroup(layout.createSequentialGroup() + .addComponent(button) + .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) + .addComponent(comboBox3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) + .addGroup(layout.createSequentialGroup() + .addComponent(textArea, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) + .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) + .addComponent(comboBox4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))) + .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + ); + layout.setVerticalGroup( + layout.createParallelGroup(GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) + .addComponent(label) + .addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) + .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) + .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) + .addComponent(comboBox2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) + .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) + .addComponent(button) + .addComponent(comboBox3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) + .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) + .addComponent(textArea, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) + .addComponent(comboBox4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) + .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + ); + f.pack(); + f.setVisible(true); + System.out.printf("comboBox baseline %d%n",comboBox.getBaseline(comboBox.getWidth(), comboBox3.getHeight())); + System.out.printf("comboBox baseline resize behaviour %s%n", comboBox.getBaselineResizeBehavior()); + } + +} diff --git a/demo/demo.iml b/demo/demo.iml index f2ac100..cd0cb1b 100644 --- a/demo/demo.iml +++ b/demo/demo.iml @@ -5,6 +5,7 @@ + @@ -12,5 +13,4 @@ - - + \ No newline at end of file From b75da472e03627fc663d203be81c0a7e41c696a6 Mon Sep 17 00:00:00 2001 From: Alessandro Falappa Date: Fri, 12 Feb 2016 18:05:27 +0100 Subject: [PATCH 2/2] DarculaComboBoxUI.java: removed overridden getDisplaySize method and two not deeded anymore variables. --- .../darcula/ui/DarculaComboBoxUI.java | 74 +++---------------- 1 file changed, 10 insertions(+), 64 deletions(-) diff --git a/src/com/bulenkov/darcula/ui/DarculaComboBoxUI.java b/src/com/bulenkov/darcula/ui/DarculaComboBoxUI.java index 0835492..16b4fab 100755 --- a/src/com/bulenkov/darcula/ui/DarculaComboBoxUI.java +++ b/src/com/bulenkov/darcula/ui/DarculaComboBoxUI.java @@ -42,11 +42,6 @@ import java.awt.geom.Path2D; @SuppressWarnings("GtkPreferredJComboBoxRenderer") public class DarculaComboBoxUI extends BasicComboBoxUI implements Border { private final JComboBox myComboBox; - // Flag for calculating the display size - private boolean myDisplaySizeDirty = true; - - // Cached the size that the display needs to render the largest item - private Dimension myDisplaySizeCache = new Dimension(0, 0); private Insets myPadding; public DarculaComboBoxUI(JComboBox comboBox) { @@ -128,65 +123,6 @@ public class DarculaComboBoxUI extends BasicComboBoxUI implements Border { return new InsetsUIResource(4, 7, 4, 5); } - protected Dimension getDisplaySize() { - Dimension display = new Dimension(); - - ListCellRenderer renderer = comboBox.getRenderer(); - if (renderer == null) { - renderer = new DefaultListCellRenderer(); - } - - boolean sameBaseline = true; - - Object prototypeValue = comboBox.getPrototypeDisplayValue(); - if (prototypeValue != null) { - display = getSizeForComponent(renderer.getListCellRendererComponent(listBox, prototypeValue, -1, false, false)); - } else { - final ComboBoxModel model = comboBox.getModel(); - - int baseline = -1; - Dimension d; - - if (model.getSize() > 0) { - for (int i = 0; i < model.getSize(); i++) { - Object value = model.getElementAt(i); - Component rendererComponent = renderer.getListCellRendererComponent(listBox, value, -1, false, false); - d = getSizeForComponent(rendererComponent); - if (sameBaseline && value != null && (!(value instanceof String) || !"".equals(value))) { - int newBaseline = rendererComponent.getBaseline(d.width, d.height); - if (newBaseline == -1) { - sameBaseline = false; - } - else if (baseline == -1) { - baseline = newBaseline; - } - else if (baseline != newBaseline) { - sameBaseline = false; - } - } - display.width = Math.max(display.width, d.width); - display.height = Math.max(display.height, d.height); - } - } - else { - display = getDefaultSize(); - if (comboBox.isEditable()) { - display.width = 100; - } - } - } - - if (myPadding != null) { - display.width += myPadding.left + myPadding.right; - display.height += myPadding.top + myPadding.bottom; - } - - myDisplaySizeCache.setSize(display.width, display.height); - myDisplaySizeDirty = false; - - return display; - } - protected Dimension getSizeForComponent(Component comp) { currentValuePane.add(comp); comp.setFont(comboBox.getFont()); @@ -401,4 +337,14 @@ public class DarculaComboBoxUI extends BasicComboBoxUI implements Border { public boolean isBorderOpaque() { return false; } + + @Override + public Component.BaselineResizeBehavior getBaselineResizeBehavior(JComponent c) { + return super.getBaselineResizeBehavior(c); + } + + @Override + public int getBaseline(JComponent c, int width, int height) { + return super.getBaseline(c, width, height); + } } \ No newline at end of file