Last active
August 29, 2015 14:17
-
-
Save apertureless/e15e1b7d46631507bef4 to your computer and use it in GitHub Desktop.
GUI Klausur WS 14/15 Lernhilfen & nützliche Code snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Methode für das Beenden der Anwendung mit Bestätigungsdialog | |
*/ | |
private void closePromt() { | |
int close; | |
close = JOptionPane.showInternalConfirmDialog(desktopPane, "Programm Beenden?", "Anwendung beenden.", JOptionPane.YES_NO_OPTION); | |
if (close == 0) { | |
System.exit(0); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static final String ERLAUBTE_ZEICHEN ="0123456789,."; | |
/** | |
* Klasse gibt alle gültigen Eingabezeichen JTPreisEingabe Feld vor. | |
*/ | |
public class EingabeDokument extends PlainDocument { | |
/** | |
* | |
* @param offs | |
* @param s | |
* @param as | |
* @throws BadLocationException | |
*/ | |
@Override | |
public void insertString(int offs, String s, javax.swing.text.AttributeSet as) throws BadLocationException { | |
for (int i = 0; i < s.length(); i++) { | |
if (!ERLAUBTE_ZEICHEN.contains("" + s.charAt(i)) ){ | |
Toolkit.getDefaultToolkit().beep(); | |
return; | |
} | |
} | |
if (!istButtonFlasche) { | |
jTPreisAusgabe.setText(""); | |
} | |
super.insertString(offs, s, null); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EingabeCheck extends InputVerifier { | |
private static final String REGEX_KOMMA = "(\\d*,?\\d{0,2})|(\\d{0,3}(\\.\\d{3})*,?\\d{0,2})"; | |
private static final String FEHLER_TITEL = "Keine gültige Zahl - Verify"; | |
private static final String FEHLER_MSG = "Bitte geben Sie eine Zahl im deutschen Format ein. (12,34 oder 1.234,56) "; | |
@Override | |
public boolean verify(JComponent input) { | |
return ((JTextField) input).getText().matches(REGEX_KOMMA); | |
} | |
@Override | |
public boolean shouldYieldFocus(JComponent input) { | |
boolean istGueltig = verify(input); | |
if (!istGueltig) { | |
JOptionPane.showMessageDialog(null, FEHLER_MSG, FEHLER_TITEL, JOptionPane.WARNING_MESSAGE); | |
} | |
return istGueltig; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void SetSpinnerValues() { | |
int value = (int)jahrgang + 1; | |
int min = value; | |
int max = value + MAX_LAGERDAUER; | |
int step = 1; | |
SpinnerNumberModel lagerModel = new SpinnerNumberModel(value, min, max, step); | |
JSpinner.NumberEditor ed = new JSpinner.NumberEditor(jSLagerdauer); | |
ed.getFormat().setGroupingUsed(false); | |
//jSLagerdauer.setEditor(ed); | |
jSLagerdauer.setModel(lagerModel); | |
jSLagerdauer.setEditor(new JSpinner.NumberEditor(jSLagerdauer,"####")); | |
jSLagerdauer.requestFocusInWindow(); | |
lagerdauer = value - (int)jahrgang; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private boolean isFormularChanged2() { | |
boolean isChanged = false; | |
for (Component c : WeinDatenPanel.getComponents()){ | |
if (!isChanged && c instanceof JTextField) { | |
isChanged = !((JTextField) c).getText().isEmpty(); | |
} else if (!isChanged && c instanceof JComboBox) { | |
isChanged = isComboBoxChanged; | |
} | |
} | |
return isChanged; | |
} | |
private boolean isFormularChanged() { | |
boolean isChanged = false; | |
for (Component c : WeinDatenPanel.getComponents()){ | |
if (!isChanged && c instanceof JComboBox) { | |
isChanged = isComboBoxChanged; | |
} | |
} | |
if(!isChanged) { | |
isChanged = !jTextFieldName.getText().isEmpty(); | |
} | |
if(!isChanged) { | |
isChanged = !jFTJahrgang.getText().isEmpty(); | |
} | |
if(!isChanged) { | |
isChanged = !jTPreisEingabe.getText().isEmpty(); | |
} | |
if(!isChanged) { | |
isChanged = !jTPreisAusgabe.getText().isEmpty(); | |
} | |
if(!isChanged && bnv.verify(jFTextfieldBestellnummer)){ | |
isChanged = true; | |
} | |
return isChanged; | |
} | |
private void closeFormular() { | |
if (isFormularChanged()) { | |
int abbrechen = JOptionPane.showInternalConfirmDialog(jDesktopPane1, ABORT_MSG, | |
ABORT_TITEL, JOptionPane.YES_NO_OPTION); | |
if (abbrechen == 0) { | |
WeinAufnehmenFrame.setVisible(false); | |
isChangeForm = false; | |
resetFormular(); | |
resetColors(); | |
jBFirstItem.setVisible(false); | |
jBPrevItem.setVisible(false); | |
jBNextItem.setVisible(false); | |
jBLastItem.setVisible(false); | |
jFTextfieldBestellnummer.setEditable(true); | |
} | |
} else { | |
WeinAufnehmenFrame.setVisible(false); | |
isChangeForm = false; | |
resetFormular(); | |
resetColors(); | |
jBFirstItem.setVisible(false); | |
jBPrevItem.setVisible(false); | |
jBNextItem.setVisible(false); | |
jBLastItem.setVisible(false); | |
jFTextfieldBestellnummer.setEditable(true); | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MaskFormatter mf = null; | |
NumberFormat nf = new DecimalFormat("0000"); | |
// Maskformatter anlegen für das Bestellummer-Feld | |
// Im Format 01-ABCD-0001 | |
try { | |
mf = new MaskFormatter("##-UUU-" + nf.format(laufnummer + 1)); | |
mf.setPlaceholderCharacter('_'); | |
DefaultFormatterFactory dff = new DefaultFormatterFactory(mf); | |
jFTextfieldBestellnummer.setFormatterFactory(dff); | |
} catch (ParseException ex) { | |
Logger.getLogger(WeinVerwaltung.class.getName()).log(Level.SEVERE, null, ex); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Setzt die Fokusreihenfolge fest. | |
* | |
* @return ArrayList<JComponent> - Fokusreihenfolge | |
*/ | |
private ArrayList<JComponent> getOrder() { | |
ArrayList<JComponent> order = new ArrayList<>(); | |
order.add(jBFirstItem); | |
order.add(jBPrevItem); | |
order.add(jBNextItem); | |
order.add(jBLastItem); | |
order.add(jFTextfieldBestellnummer); | |
order.add(jTextFieldName); | |
order.add(jFTJahrgang); | |
order.add(jSLagerdauer); | |
order.add(jComboBoxFarbe); | |
order.add(jComboBoxLand); | |
order.add(jComboBoxRegion); | |
order.add(jComboBoxAlkoholgehalt); | |
order.add(jCFlaschengroesse); | |
order.add(jTPreisEingabe); | |
order.add(jBUmrechnenUp); | |
order.add(jTPreisAusgabe); | |
order.add(jBDown); | |
order.add(jButtonSpeichern); | |
order.add(jButtonAbbrechen); | |
return order; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BestellnummerVerifier extends InputVerifier{ | |
private final String REGEX = "[0-9]{2}-[A-Z]{3}-[0-9]{4}"; | |
@Override | |
public boolean verify(JComponent component) { | |
return ((JTextField) component).getText().matches(REGEX); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment