Hallo zusammen, ich habe mich in letzter Zeit recht viel mit GUI-Programmierung beschäftigt, habe aber das Gefühl, dass mein Code nicht optimal ist. Ich habe jetzt zum Beispiel eine Klasse, abgeleitet von JPanel, die im Endeffekt eine Liste von Komponenten enthält. Das Ganze sieht so aus:
Darüber gibt es noch einen Button (der aber nicht zu der Klasse selbst gehört) mit dem man eine weitere Zeile hinzufügt. Der Mülleimer ist denke ich klar.
Jetzt wo das erwartete Ergebnis hoffentlich klar ist der Code:
mit der makegbc-Methode habe ich schon ein bisschen was ausgelagert, bin mir aber sicher, dass man den Code, vor allem die beiden Methoden "addLabelCombinationInputField" und "removeLabelCombinationInputField" noch optimieren kann, nur wie ist mir nicht bewusst. Außerdem würde ich diese Liste gerne scrollbar machen, sobald sie mehr als 6 Elemente beinhaltet, sodass das Fenster nicht immer größer wird.
Ich würde mich freuen, wenn ich da ein paar Hinweise bekommen könnte.
Darüber gibt es noch einen Button (der aber nicht zu der Klasse selbst gehört) mit dem man eine weitere Zeile hinzufügt. Der Mülleimer ist denke ich klar.
Jetzt wo das erwartete Ergebnis hoffentlich klar ist der Code:
Java:
public class LabelCombinationInput extends JPanel {
private static final long serialVersionUID = 4125516955764108160L;
private List<LabelCombinationInputField> labelCombinationInputFields;
private GridBagLayout layout;
private GridBagConstraints gbc;
private JDialog parent;
private JPanel container;
private GridBagLayout containerLayout;
public LabelCombinationInput(JDialog parent) {
this.parent = parent;
labelCombinationInputFields = new ArrayList<>();
LabelCombinationInputField defaultComb = new LabelCombinationInputField(this);
defaultComb.setDefault("default");
labelCombinationInputFields.add(defaultComb);
layout = new GridBagLayout();
setLayout(layout);
container = new JPanel();
containerLayout = new GridBagLayout();
container.setLayout(containerLayout);
// Load default
for (LabelCombinationInputField input : labelCombinationInputFields) {
gbc = makegbc(0, container.getComponentCount(), 1, 1, GridBagConstraints.CENTER, new Insets(10, 0, 0, 0),
10, 0, GridBagConstraints.HORIZONTAL);
containerLayout.addLayoutComponent(input, gbc);
container.add(input);
}
gbc = makegbc(0, getComponentCount(), 1, 1, GridBagConstraints.PAGE_START, new Insets(10, 0, 0, 0), 10, 0,
GridBagConstraints.HORIZONTAL);
layout.addLayoutComponent(container, gbc);
add(container);
setVisible(true);
}
public void addLabelCombinationInputField() {
LabelCombinationInputField newInput = new LabelCombinationInputField(this);
labelCombinationInputFields.add(newInput);
gbc = makegbc(0, container.getComponentCount(), 1, 1, GridBagConstraints.CENTER, new Insets(10, 0, 0, 0), 10, 0,
GridBagConstraints.HORIZONTAL);
containerLayout.addLayoutComponent(newInput, gbc);
container.add(newInput);
parent.pack();
container.revalidate();
validate();
}
public void removeLabelCombinationInputField(LabelCombinationInputField field) {
try {
containerLayout.removeLayoutComponent(field);
container.remove(field);
validate();
parent.pack();
} catch (Exception e) {
throw new NullPointerException("Cannot remove Component.");
}
}
}
mit der makegbc-Methode habe ich schon ein bisschen was ausgelagert, bin mir aber sicher, dass man den Code, vor allem die beiden Methoden "addLabelCombinationInputField" und "removeLabelCombinationInputField" noch optimieren kann, nur wie ist mir nicht bewusst. Außerdem würde ich diese Liste gerne scrollbar machen, sobald sie mehr als 6 Elemente beinhaltet, sodass das Fenster nicht immer größer wird.
Ich würde mich freuen, wenn ich da ein paar Hinweise bekommen könnte.