Question

I am new to Java and am trying to make a GUI that acts exactly like the command window of MatLAB.

I am trying to have the line that is currently being typed in the JTextArea sent to console instead of the whole JTextArea. My current strategy is using a caretlistener and keylistener. For now I am using "matlab's response" as a placeholder response, which should be the line underneath the line when I pressed enter.

Here is my code:

public class MatlabGui extends JPanel implements KeyListener {

    protected JTextArea myTextArea;;

    public MatlabGui() {
        super(new GridBagLayout());

        myTextArea = new JTextArea(50, 75);
        myTextArea.setEditable(true);
        JScrollPane myScrollPane = new JScrollPane(myTextArea);

        GridBagConstraints myCons = new GridBagConstraints();
        myCons.gridwidth = GridBagConstraints.REMAINDER;

        myCons.fill = GridBagConstraints.BOTH;
        myCons.weightx = 1;
        myCons.weighty = 1;
        add(myScrollPane, myCons);

        myTextArea.addKeyListener(this);

    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("Matlab");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new MatlabGui());
        frame.pack();
        frame.setVisible(true);

    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

@Override
public void keyPressed(KeyEvent evt) {
    // TODO Auto-generated method stub
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        try {
            myTextArea.addCaretListener(CaretListener);
            int caretpos = myTextArea.getCaretPosition();
            int start = 0;
            int end = 0;

            start = myTextArea.getLineStartOffset(caretpos);
            end = myTextArea.getLineEndOffset(caretpos);

            System.out.println(myTextArea.getText(start, end));

        } catch (BadLocationException ex) {
            System.out.println(ex.getMessage());
        }
        myTextArea.append("\n" + ">>>" + " " + "matlab's response");
                }

}    
    public String getString() {
        return myTextArea.getText();
    }


    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub
    }

}

All help is very much appreciated. Cheers

Était-ce utile?

La solution 2

I figured it out:

@Override
public void keyPressed(KeyEvent evt) {
    // TODO Auto-generated method stub
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        try {
            int end = myTextArea.getCaretPosition();
            int row = myTextArea.getLineOfOffset(end);
            int column = end - myTextArea.getLineStartOffset(row);
            int begin = end - column;
            int length = end - begin;

            System.out.println(myTextArea.getText(begin, length));

        } catch (BadLocationException ex) {
            System.out.println(ex.getMessage());
        }
        myTextArea.append("\n" + ">>>" + " " + "matlab's response");
    }

}

Autres conseils

For your other problem:

Your condition if (evt.getKeyCode() == KeyEvent.VK_ENTER) end before myTextArea.append("\n" + ">>>" + " " + "matlab's response"); so when a key is pressed, myTestArea.append is always called.

You should do:

 if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        try {
            // Hint
            // offset=myTextArea.getLineOfOffset(myTextArea.getCaretPosition());
            int start = 0;
            int end = 0;
            start = myTextArea.getLineStartOffset(start);
            end = myTextArea.getLineEndOffset(end);

            System.out.println(myTextArea.getText(start, end));

        } catch (BadLocationException ex) {
            System.out.println(ex.getMessage());
        }

        String text = myTextArea.getText();
        System.out.println(text);

        myTextArea.append("\n" + ">>>" + " " + "matlab's response");
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top