Skip to content

Commit db7437f

Browse files
committed
Make list panel resize with window
1 parent c4cc506 commit db7437f

File tree

2 files changed

+62
-13
lines changed

2 files changed

+62
-13
lines changed

tmc-plugin/src/fi/helsinki/cs/tmc/ui/CourseListWindow.java

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
import java.awt.event.ActionListener;
1515
import java.awt.event.MouseAdapter;
1616
import java.awt.event.MouseEvent;
17+
import java.awt.event.ComponentEvent;
18+
import java.awt.event.ComponentListener;
1719
import java.util.List;
20+
import javax.swing.BoxLayout;
1821
import javax.swing.JButton;
1922
import javax.swing.JFrame;
2023
import javax.swing.JLabel;
@@ -32,7 +35,7 @@ public class CourseListWindow extends JPanel {
3235
private static JFrame frame;
3336
private final JList<CourseCard> courses;
3437
private PreferencesPanel prefPanel;
35-
private final JButton button;
38+
private static JButton button;
3639

3740
public CourseListWindow(List<Course> courses, PreferencesPanel prefPanel) {
3841
this.prefPanel = prefPanel;
@@ -42,9 +45,9 @@ public CourseListWindow(List<Course> courses, PreferencesPanel prefPanel) {
4245
}
4346
this.courses = new JList<>(courseCards);
4447
this.courses.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
45-
setLayout(new BorderLayout());
48+
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
4649
this.button = new JButton("Select");
47-
this.button.addActionListener(new SelectCourseListener(this));
50+
button.addActionListener(new SelectCourseListener(this));
4851

4952
this.courses.setCellRenderer(new CourseCellRenderer());
5053
this.courses.setVisibleRowCount(4);
@@ -64,8 +67,8 @@ public void mouseClicked(MouseEvent event) {
6467
}
6568
}
6669
});
67-
add(pane, BorderLayout.NORTH);
68-
add(this.button, BorderLayout.SOUTH);
70+
add(pane);
71+
add(button);
6972
}
7073

7174
public static void display(PreferencesPanel prefPanel) throws Exception {
@@ -75,12 +78,34 @@ public static void display(PreferencesPanel prefPanel) throws Exception {
7578
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
7679
List<Course> courses = prefPanel.getAvailableCourses();
7780
CourseDb.getInstance().setAvailableCourses(courses);
78-
frame.setContentPane(new CourseListWindow(courses, prefPanel));
81+
final CourseListWindow courseListWindow = new CourseListWindow(courses, prefPanel);
82+
frame.setContentPane(courseListWindow);
7983
if (hasCourses(courses, prefPanel)) {
8084
frame.pack();
8185
frame.setLocationRelativeTo(null);
8286
frame.setVisible(true);
8387
}
88+
button.setMinimumSize(new Dimension(courseListWindow.getWidth(), button.getHeight()));
89+
button.setMaximumSize(new Dimension(courseListWindow.getWidth(), button.getHeight()));
90+
courseListWindow.addComponentListener(new ComponentListener() {
91+
@Override
92+
public void componentResized(ComponentEvent event) {
93+
button.setMinimumSize(new Dimension(courseListWindow.getWidth(), button.getHeight()));
94+
button.setMaximumSize(new Dimension(courseListWindow.getWidth(), button.getHeight()));
95+
}
96+
97+
@Override
98+
public void componentMoved(ComponentEvent e) {
99+
}
100+
101+
@Override
102+
public void componentShown(ComponentEvent e) {
103+
}
104+
105+
@Override
106+
public void componentHidden(ComponentEvent e) {
107+
}
108+
});
84109
}
85110

86111
public static boolean isWindowVisible() {

tmc-plugin/src/fi/helsinki/cs/tmc/ui/OrganizationListWindow.java

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
import java.awt.event.ActionListener;
1414
import java.awt.event.MouseAdapter;
1515
import java.awt.event.MouseEvent;
16+
import java.awt.event.ComponentEvent;
17+
import java.awt.event.ComponentListener;
1618
import java.util.List;
19+
import javax.swing.BoxLayout;
1720
import javax.swing.JButton;
1821
import javax.swing.JFrame;
1922
import javax.swing.JLabel;
@@ -30,7 +33,7 @@ public class OrganizationListWindow extends JPanel {
3033

3134
private static JFrame frame;
3235
private final JList<OrganizationCard> organizations;
33-
private final JButton button;
36+
private static JButton button;
3437

3538
public OrganizationListWindow(List<Organization> organizations) {
3639
OrganizationCard[] organizationCards = new OrganizationCard[organizations.size()];
@@ -39,10 +42,9 @@ public OrganizationListWindow(List<Organization> organizations) {
3942
}
4043
this.organizations = new JList<>(organizationCards);
4144
this.organizations.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
42-
43-
setLayout(new BorderLayout());
45+
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
4446
this.button = new JButton("Select");
45-
this.button.addActionListener(new SelectOrganizationListener(this));
47+
button.addActionListener(new SelectOrganizationListener(this));
4648

4749
this.organizations.setCellRenderer(new OrganizationCellRenderer());
4850
this.organizations.setVisibleRowCount(4);
@@ -64,8 +66,8 @@ public void mouseClicked(MouseEvent event) {
6466
}
6567
});
6668

67-
add(pane, BorderLayout.NORTH);
68-
add(this.button, BorderLayout.SOUTH);
69+
add(pane);
70+
add(button);
6971
}
7072

7173
public static void display() throws Exception {
@@ -74,10 +76,32 @@ public static void display() throws Exception {
7476
}
7577
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
7678
List<Organization> organizations = TmcCore.get().getOrganizations(ProgressObserver.NULL_OBSERVER).call();
77-
frame.setContentPane(new OrganizationListWindow(organizations));
79+
final OrganizationListWindow organizationListWindow = new OrganizationListWindow(organizations);
80+
frame.setContentPane(organizationListWindow);
7881
frame.pack();
7982
frame.setLocationRelativeTo(null);
8083
frame.setVisible(true);
84+
button.setMinimumSize(new Dimension(organizationListWindow.getWidth(), button.getHeight()));
85+
button.setMaximumSize(new Dimension(organizationListWindow.getWidth(), button.getHeight()));
86+
organizationListWindow.addComponentListener(new ComponentListener() {
87+
@Override
88+
public void componentResized(ComponentEvent event) {
89+
button.setMinimumSize(new Dimension(organizationListWindow.getWidth(), button.getHeight()));
90+
button.setMaximumSize(new Dimension(organizationListWindow.getWidth(), button.getHeight()));
91+
}
92+
93+
@Override
94+
public void componentMoved(ComponentEvent e) {
95+
}
96+
97+
@Override
98+
public void componentShown(ComponentEvent e) {
99+
}
100+
101+
@Override
102+
public void componentHidden(ComponentEvent e) {
103+
}
104+
});
81105
}
82106

83107
public static boolean isWindowVisible() {

0 commit comments

Comments
 (0)