Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ***
- // InputHandler
- // ***
- public class InputHandler {
- private int MAX_INPUT_LEN = 30;
- private int MIN_INPUT_LEN = 3; // Koristi se za Oznaku drzave i tako tih slicnih stvari...
- private int MAX_LOGIN_ATEMPTS = 5;
- public char[] invalidChars = new char[] {'(', ')', '{', '}', '[', ']', '!', '?', '@', '/', '*', '.', ';', '-', '+', ',', '|', '^', '&', '%', '$', '_'};
- private boolean ok = true; // Kontrola unosa
- public InputHandler() {
- }
- public void setOk(boolean ok) {
- this.ok = ok;
- System.out.println(this.ok + " u setteru InputHandler-a");
- }
- public boolean isOk() {
- return ok;
- }
- // ***
- // MyKeyListener
- // ***
- import gui.handlers.InputHandler;
- public class MyKeyListener implements KeyListener {
- // U ovoj klasi se setuje prom. ok na false iz InputHandler klase
- // i koristi se kod JTextField-a u klasi PanelDetailDrzave da provjeri unos
- @Override
- public void keyTyped(KeyEvent e) {
- // TODO Auto-generated method stub
- if (e.getSource() instanceof JTextField) {
- JTextField txtf = (JTextField)e.getSource();
- char c = e.getKeyChar();
- InputHandler ih = new InputHandler();
- boolean ok = true;
- for (int i = 0; i < ih.invalidChars.length; i++) {
- if (c == ih.invalidChars[i]) {
- System.out.println("DETEKTOVAN NEDOZVOLJEN ZNAK!");
- txtf.setBackground(Color.RED);
- ih.setOk(false); // OVDE SE SETUJE NA FALSE IZ INPUTHANDLER-a
- System.out.println(ih.isOk() + " u MyKeyListeneru");
- }
- // else
- // txtf.setBackground(Color.WHITE);
- }
- }
- }
- // ...
- // ***
- // PanelDetailDrzave
- // ***
- import gui.actions.MyKeyListener;
- import gui.handlers.InputHandler;
- public class PanelDetailDrzave extends GPanelDetail {
- private JTextField txtNazivDrzave;
- InputHandler ih = new InputHandler();
- public PanelDetailDrzave(StanjeDijaloga mode) {
- // ...
- KeyListener kl = new MyKeyListener();
- txtNazivDrzave.addKeyListener(kl);
- // ...
- @Override
- public boolean proveriIspravnostPodataka() {
- System.out.println(ih.isOk() + " u proveriIspravnostPodataka()");
- if (!ih.isOk()) {
- ok = false;
- txtNazivDrzave.setBackground(Color.RED);
- System.out.println("jebe mater");
- }
- // ...
- /* IZLAZ KONZOLE:
- DETEKTOVAN NEDOZVOLJEN ZNAK!
- false u setteru InputHandler-a
- false u MyKeyListeneru
- true u proveriIspravnostPodataka() -- OVDE JE GRESKA!
- */
Advertisement
Add Comment
Please, Sign In to add comment