Visualizzazione dei risultati da 1 a 4 su 4

Discussione: Shell con la jTextArea

  1. #1

    Shell con la jTextArea

    Ciao, qualcuno sa il modo di ottenere solo una porzione di testo da una jTextArea?
    Io devo implementare questa shell (per intenderci tipo la schell di linux)dove una volta che l'utente preme invio, devo prendere l'input e rielaborarlo.
    Il problema è che non so come prendere di volta in volta solo la porzione di codice che mi interessa.
    io ho implementato il metodo keyReleased e prendo l'input dell'utente attraverso il metodo getKeyChar(), purtroppo se premo backspce, questo non cancella l'ultimo carattere ma viene aggiunto il carattere backspace.
    qualcuno sa come prendere l'input ovviando a questo problema? getString non va bene perchè prende tutto l'input della jtextArea.

  2. #2
    Se a qualcuno può servire ho trovato una semi soluzione...ve la ripropongo, a qualcuno può servire, anche se non mi entusiasma.

    if (e.getKeyChar() == '\b'){
    int i=commandString.indexOf('\b');
    String str1=commandString.substring(0, i-1);
    String str2=commandString.substring(i+1, commandString.length());
    commandString=str1+str2;
    }

    La tengo come soluzione provvisoria

  3. #3
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    Avevo una bella giornata:

    codice:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    /**
     *
     * @author Andrea
     */
    public class InputParser extends JFrame {
        
        private class myTextArea extends JTextArea implements KeyListener {
            
            private String consoleInput;
            
            public void keyReleased (KeyEvent ke) {
                if (ke.getKeyCode() == ke.VK_ENTER) {
                    try {
                        String temp = this.getText(this.getLineStartOffset(this.getLineCount()-2),this.getLineEndOffset(this.getLineCount()-2));
                        System.out.println(temp);
                    }
                    catch (Exception e) {
                        e.printStackTrace();
                    }
                    this.append(consoleInput);
                    this.setCaretPosition(this.getText().length());
                }
            }
            
            public void keyPressed(KeyEvent ke) {
                
            }
            
            public void keyTyped (KeyEvent ke) {
             
            }
            
            public myTextArea(String consoleInput) {
                this.consoleInput = consoleInput;
                this.setText(consoleInput);
                this.setCaretPosition(this.getText().length());
                this.addKeyListener(this);
            }
        }
        
        /** Creates a new instance of InputParser */
        public InputParser() {
            super("test");
            myTextArea mta = new myTextArea("C:\\>");
            this.setSize(400,400);
            this.getContentPane().add(new JScrollPane(mta));
            this.setVisible(true);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        
        public static void main (String[] args) {
            InputParser ip = new InputParser();
        }
        
    }
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  4. #4
    Grazie!!!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.