Skip to content

Commit d58731f

Browse files
committed
Add swing dynamic width dropdown
1 parent 2db02ea commit d58731f

2 files changed

Lines changed: 109 additions & 0 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.openapex.samples.misc.swing;
2+
3+
import javax.swing.*;
4+
import java.awt.*;
5+
import java.awt.font.FontRenderContext;
6+
import java.awt.geom.AffineTransform;
7+
import java.util.Arrays;
8+
9+
public class JComboBoxDynamicWidth57224561 {
10+
private JFrame f;
11+
12+
public JComboBoxDynamicWidth57224561() {
13+
f = new JFrame("ComboBox Example");
14+
String country[] = {"Long Item 5", "Long Item 2", "Long Item 1", "Long Item 8", "Long Item 4"};
15+
String longest = Arrays.stream(country).max((e1, e2) -> e1.length() - e2.length()).get();
16+
System.out.println(longest);
17+
MyDropdown cb = new MyDropdown(country);
18+
int width = findTextWidth(longest);
19+
//Graphics2D g = (Graphics2D)f.getGraphics();
20+
//cb.setBounds(50, 50, (int)g.getFont().getStringBounds(longest, g.getFontRenderContext()).getWidth()*2 ,20);
21+
cb.setBounds(50, 50, findTextWidth(longest) * 2, 20);
22+
f.add(cb);
23+
f.setLayout(null);
24+
f.setSize(400, 500);
25+
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
26+
//f.pack();
27+
System.out.println(f.getGraphics());
28+
f.setVisible(true);
29+
}
30+
31+
public static int findTextWidth(String text) {
32+
AffineTransform affinetransform = new AffineTransform();
33+
FontRenderContext frc = new FontRenderContext(affinetransform, true, true);
34+
Font font = new Font("Lucida Grande", Font.PLAIN, 13);
35+
return (int) (font.getStringBounds(text, frc).getWidth());
36+
}
37+
38+
public static void main(String[] args) {
39+
new JComboBoxDynamicWidth57224561();
40+
}
41+
42+
private static class MyDropdown extends JComboBox {
43+
private boolean layoutInProgress = false;
44+
45+
public MyDropdown(Object[] items) {
46+
super(items);
47+
}
48+
49+
public void doLayout() {
50+
layoutInProgress = true;
51+
try {
52+
super.doLayout();
53+
} finally {
54+
layoutInProgress = false;
55+
}
56+
}
57+
58+
public Dimension getSize() {
59+
Dimension dimension = getSize();
60+
if (!layoutInProgress) {
61+
dimension.width = Math.max(dimension.width, super.getPreferredSize().width);
62+
}
63+
return dimension;
64+
}
65+
}
66+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
-- fiddle: https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=88d26c1814168079d555e6bc2e214daa
2+
create table appt
3+
(
4+
target int,
5+
target_cumulative int,
6+
unique_id int,
7+
resource_id int,
8+
start_date datetime
9+
);
10+
create table inserted
11+
(
12+
unique_id int,
13+
value int
14+
);
15+
16+
insert into appt
17+
values(10, 0, 1, 1, '2019-07-26 08:00:00');
18+
insert into appt
19+
values(20, 0, 2, 2, '2019-07-26 07:00:00');
20+
insert into appt
21+
values(30, 0, 3, 1, '2019-07-26 10:00:00');
22+
insert into appt
23+
values(10, 0, 4, 1, '2019-07-26 11:00:00');
24+
insert into appt
25+
values(20, 0, 5, 2, '2019-07-26 09:00:00');
26+
27+
insert into inserted
28+
values(1, 10);
29+
insert into inserted
30+
values(2, 10);
31+
insert into inserted
32+
values(3, 10);
33+
insert into inserted
34+
values(4, 10);
35+
insert into inserted
36+
values(5, 10);
37+
38+
update appt set target_cumulative=xtarget
39+
from (select t1.resource_id, sum(t1.target)as xtarget
40+
from appt t1
41+
inner join inserted as i on t1.unique_id=i.unique_id
42+
group by convert(date, t1.start_date), t1.resource_id) t2
43+
where appt.resource_id=t2.resource_id;

0 commit comments

Comments
 (0)