Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7555129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:31:21+00:00 2026-05-30T11:31:21+00:00

All I want to do, If I’m pressing ‘{‘ this key in JtextArea.automatically ‘}’

  • 0

All I want to do, If I’m pressing ‘{‘ this key in JtextArea.automatically ‘}’ this will be print also.

 if(evt.KEY_PRESSED == '{')

            System.out.print("}");

is this is okay??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T11:31:23+00:00Added an answer on May 30, 2026 at 11:31 am

    for listening changes into JTextComponent is there DocumentListener, if you have to need control over inputed Char, sings, whitespace chars or word(s) you have to implements DocumentFilter

    notice for Chars reservated by programing language(s) you have to use double escapes,

    \\( instead of (

    or

    \\{ instead of {

    otherwise you get

    Exception in thread "AWT-EventQueue-0" java.util.regex.PatternSyntaxException: 
    Illegal repetition
    

    for example

    import java.awt.*;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.text.*;
    
    public class TextAreaTest extends JFrame {
    
        private static final long serialVersionUID = 1L;
        private JTextArea textArea;
    
        public TextAreaTest() {
            textArea = new JTextArea();
            textArea.setPreferredSize(new Dimension(60, 32));
            textArea.setOpaque(true);
            textArea.setLineWrap(true);
            textArea.setWrapStyleWord(true);
            ((AbstractDocument) textArea.getDocument()).setDocumentFilter(new DocumentFilter() {
    
                @Override
                public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
                    string = string.replaceAll("\\{", "\\{}");
                    super.insertString(fb, offset, string, attr);
                }
    
                @Override
                public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
                    text = text.replaceAll("\\{", "\\{}");
                    //TODO must do something here
                    super.replace(fb, offset, length, text, attrs);
                }
            });
    
            textArea.getDocument().addDocumentListener(new DocumentListener() {
    
                @Override
                public void changedUpdate(DocumentEvent e) {
                    update(e);
                }
    
                @Override
                public void insertUpdate(DocumentEvent e) {
                    update(e);
                }
    
                @Override
                public void removeUpdate(DocumentEvent e) {
                    update(e);
                }
    
                private void update(DocumentEvent e) {
                    List<String> lines = getLines(textArea);
                    String lastLine = lines.get(lines.size() - 1);
                    int tabbedTextWidth = Utilities.getTabbedTextWidth(new Segment(
                            lastLine.toCharArray(), 0, lastLine.length()), textArea.getFontMetrics(textArea.getFont()), 0, null, 0);
                    int lineHeight = getLineHeight(textArea);
                    if (lines.size() * lineHeight > textArea.getHeight() || tabbedTextWidth > textArea.getWidth()) {
                        System.out.println("Too big! Should refuse the update!");
                    }
                }
            });
    
            getContentPane().add(textArea);
        }
    
        private static List<String> getLines(JTextArea textArea) {
            int lineHeight = getLineHeight(textArea);
            List<String> list = new ArrayList<String>();
            for (int num = 0;; num++) {
                int i = textArea.viewToModel(new Point(0, num * lineHeight));
                int j = textArea.viewToModel(new Point(0, (num + 1) * lineHeight));
                if (i == 0 && j == 0) {
                    continue;
                }
                if (textArea.getDocument().getLength() == i && i == j) {
                    break;
                }
                String s = removeTrailingNewLine(textArea.getText().substring(i, j));
                list.add(s);
                //System.out.println(i + " " + j + " = " + s);
            }
            return list;
        }
    
        private static int getLineHeight(JTextArea textArea) {
            return textArea.getFontMetrics(textArea.getFont()).getHeight();
        }
    
        private static String removeTrailingNewLine(String s) {
            if (s.endsWith("\n")) {
                return s.substring(0, s.length() - 1);
            } else {
                return s;
            }
        }
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    TextAreaTest test = new TextAreaTest();
                    test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    test.pack();
                    test.setVisible(true);
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All i want is, a data will pass into this php file: check.php and
All I want is to do a POST like this: You can post a
All I want is a dictionary which tells me which key it couldn't find,
All I want is to save my user(player) highscores, and this information to persist
all I want to remove the duplicate value from this Array Array ( [0]
all I want is a date like this: 01/06/1973 as an NSDate. I assumed
all i want to do is this: Public Property TabsCollection()() as String()() Get Return
All I want is to update an ListViewItem's text whithout seeing any flickering. This
All I want to do is have a JOptionPane inputDialog with a JTextArea instead
all i want to create the header file for database like this. it contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.