0

I've added a "Terminal" that extends JInternalFrame into my JDesktopPane. terminal constructor is like

public Terminal(Executable exec)

where Executable is an interface that contains the abstract method elab, and I store that instance into a variable. so when I press "OK", I call exec.elab(String arg) method. Into my JDesktopPane i've created a Terminal(new BasicCommands()). After that creation, with a command, i want to change the instance of my Terminal with terminal.setExec(new AdvancedCommands()) where AdvancedCommands and BasicCommands are class that implements Executable interface and contains their own elab method. So when I do that I have no warnings or other things, but when i want to call terminal.setExec(new AdvancedCommands()) to change my elab method, nothing change... How I can do it? I tried with refreshing terminal (revalidate and repaint) but nothing...

How i add keyListener in Terminal class:

inputArea.addKeyListener(new KeyAdapter() {
        @Override
        public void keyReleased(KeyEvent arg0) {
            if (arg0.getKeyCode()==KeyEvent.VK_ENTER){
                if(!inputArea.getText().isEmpty()){
                    scrollPane.getVerticalScrollBar().setValue(scrollPane.getVerticalScrollBar().getMaximum());
                    exec.elab(new Str(inputArea.getText())); //I want to change this method with ter.setExec(some class that implements Executable);
                    clearInputArea();
                }
            }
        }
    });

1 Answers1

0

I found a solution that works, but I don't know if there are others better:

public void refresh(){
    revalidate();
    repaint();
    inputArea.removeKeyListener(ad);
    ad = new KeyAdapter() {
        @Override
        public void keyReleased(KeyEvent arg0) {
            if (arg0.getKeyCode()==KeyEvent.VK_ENTER){
                if(!inputArea.getText().isEmpty()){
                    scrollPane.getVerticalScrollBar().setValue(scrollPane.getVerticalScrollBar().getMaximum());
                    exec.elab(new Str(inputArea.getText()));
                    clearInputArea();
                }
            }
        }
    };
    inputArea.addKeyListener(ad);
}

I've create this method where I remove my keylistener and add that again.