DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Exploring Exciting New Features in Java 17 With Examples
  • Generics in Java and Their Implementation
  • How To Build a Command-Line Text Editor With Java (Part 3)
  • How To Build a Command-Line Text Editor With Java (Part 1)

Trending

  • How to Build Local LLM RAG Apps With Ollama, DeepSeek-R1, and SingleStore
  • Build Your First AI Model in Python: A Beginner's Guide (1 of 3)
  • How to Format Articles for DZone
  • Ensuring Configuration Consistency Across Global Data Centers
  1. DZone
  2. Coding
  3. Java
  4. How To Build a Command-Line Text Editor With Java (Part 2)

How To Build a Command-Line Text Editor With Java (Part 2)

Want to know how text editors implement cursor movements and scrolling? Learn how in this video tutorial.

By 
Marco Behler user avatar
Marco Behler
DZone Core CORE ·
Sep. 20, 22 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
5.4K Views

Join the DZone community and get the full member experience.

Join For Free

Let's continue building our Java-based, command-line text editor that we started here.

This is Part 2 of the "How To Build a Command-Line Text Editor With Java" series. We will cover the following: 

  • How to detect "complex" key presses like arrow up, arrow down, and friends
  • How to move the cursor around on the screen 
  • How to load and display text files, as well as a first, vertical scrolling implementation 

You're going to be in for a ride! 

What’s in the Video

Before we can tackle cursor movements in our command-line text editor, we'll have to figure out how to detect complex keypresses, like Arrow-Up, Arrow-Down, Page-Up, etc. Why is this complex? Because pressing these keys doesn't, in fact, just send one key to your program, but three or four different ones. We'll first need to learn what these key combinations are that are being sent.

Having learned that, we'll need to figure out how to map those combinations of keys to just one single keypress. So, whenever our program is sent a combination like "esc]5~," it knows that it is sent the HOME_KEY, not four individual keys.  Making this change is possible using just a couple of lines of code.

Being able to detect keypresses, we can now move on to cursor movements. First, we'll need to remember our cursor's X and Y coordinates. Moving the cursor itself, is just simple arithmetic, increasing or decreasing the X and Y coordinates as needed, and putting some boundaries in place, so the cursor cannot move off the screen. Finally, we don't really move the cursor, we just draw it in the right place, but slightly change the ANSI escape codes for cursor placements, which we learned about in the last episode.

With cursor movements in place, we can finally think about loading files. In fact, we'll load all the text files we want to display entirely into memory for now - an area we can optimize later on. It's also important to turn our text file into a list of strings, as that will make it very simple to display its content later on. Once we're able to load files, it's also time to do some much-needed refactorings, extracting a couple of methods and renaming variables so they make sense.

It's time to think about vertical scrolling! We'll work through a bit of scrolling theory, namely that we'll need to get a new variable involved, an offset. The offset specifies the line of your file, that should be displayed at the top of the screen. And scrolling "merely" means to correctly change the offset, and then make sure that all cursor movement methods and screen drawing methods take the offset into account, not just the raw screen width and height.

Even though the scrolling implementation is just a couple of lines of code, it is somewhat hard to understand, and I'll leave it to you as an exercise to really get comfortable with it. This leaves us with what's next in the series: making sure that the page up and page down keys work, proper macOS and windows support for our editor and plenty of other things that you would expect from a text editor!

See you in the next episode.


IT Implementation Text editor Text file Build (game engine) Command (computing) Java (programming language) Memory (storage engine) SENT (protocol) Strings

Published at DZone with permission of Marco Behler. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Exploring Exciting New Features in Java 17 With Examples
  • Generics in Java and Their Implementation
  • How To Build a Command-Line Text Editor With Java (Part 3)
  • How To Build a Command-Line Text Editor With Java (Part 1)

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!