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

  • Home
  • SEARCH
  • 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 8799817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:25:27+00:00 2026-06-14T00:25:27+00:00

I am trying to develop a hex editor in which the panel for editor

  • 0

I am trying to develop a hex editor in which the panel for editor is provided with key listeners and the input through keyboard is converted into corresponding input. The problem however is regarding the saving the coverted input.only one key stroke value is converted at a time as for now. I would like to use this converted input() in a string as a bunch and use it for other purposes. I tried to save converted text in a byte array but it is returning the first value I typed irrespective of what the input afterwards is.

package gui.hex;

import javax.swing.*;

import cryptool.Test;

import java.awt.*;
import java.awt.event.*;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;

public class JHexEditorASCII extends JComponent implements MouseListener,
        KeyListener {

    private static final long serialVersionUID = 5636121664420538046L;
    private JHexEditor he;
    private static CharsetEncoder encoder = Charset.forName("UTF-8")
            .newEncoder();
    public static String modifiedText;
    public static byte temp[];

    public static byte[] getTemp() {
        return temp;
    }

    public void setTemp(byte[] temp) {
        this.temp = temp;
    }

    protected JHexEditorASCII(JHexEditor he) {
        this.he = he;
        addMouseListener(this);
        addKeyListener(this);
        addFocusListener(he);
    }

    public Dimension getPreferredSize() {
        debug("getPreferredSize()");
        return getMinimumSize();
    }

    public Dimension getMinimumSize() {
        debug("getMinimumSize()");

        Dimension d = new Dimension();
        FontMetrics fn = getFontMetrics(JHexEditor.font);
        int h = fn.getHeight();
        int nl = he.getLines();
        d.setSize((fn.stringWidth(" ") + 1) * (16) + (he.border * 2) + 1, h
                * nl + (he.border * 2) + 1);
        return d;
    }

    public void paint(Graphics g) {
        debug("paint(" + g + ")");
        debug("cursor=" + he.cursor + " buff.length=" + he.buff.length);
        Dimension d = getMinimumSize();

        // sets the color to the background.
        g.setColor(Color.orange);
        g.fillRect(0, 0, d.width, d.height);
        g.setColor(Color.black);

        g.setFont(JHexEditor.font);

        // ascii data
        int ini = he.getInitial() * 16;
        int fin = ini + (he.getLines() * 16);
        if (fin > he.buff.length)
            fin = he.buff.length;

        int x = 0;
        int y = 0;
        for (int n = ini; n < fin; n++) {
            if (n == he.cursor) {

                // sets color for the focus cursor it used to be in blue color.
                g.setColor(Color.red);
                if (hasFocus())
                    he.background(g, x, y, 1);
                else
                    he.table(g, x, y, 1);
                if (hasFocus())
                    g.setColor(Color.white);
                else
                    g.setColor(Color.black);
            } else {
                g.setColor(Color.black);
            }

            // builds a character object
            String s;

            if (encoder.isLegalReplacement(new byte[] { he.buff[n] }))
                s = "" + new Character((char) he.buff[n]);
            else
                s = ".";

            // compares the input supplied
            // if ((he.buff[n] < 20) || (he.buff[n] > 126))

            // converts the input entered into string
            // s = ".";
            // displaying of the text goes here.

            // displays the input

            System.out.println("the typed in string is:" + s);

            temp = s.getBytes();
            setTemp(temp);

            setModifiedText(s);

            he.printString(g, s, (x++), y);

            if (x == 16) {
                x = 0;
                y++;
            }
        }

    }

    private void debug(String s) {
        if (he.DEBUG)
            System.out.println("JHexEditorASCII ==> " + s);
    }

    public void dispaly(Graphics g) {
        int x = 0;
        int y = 0;

        he.printString(g, Test.getK(), (x++), y);
        if (x == 16) {
            x = 0;
            y++;
        }
    }

    // calculate the position of the mouse
    public int calculateMousePosition(int x, int y) {
        FontMetrics fn = getFontMetrics(JHexEditor.font);
        x = x / (fn.stringWidth(" ") + 1);
        y = y / fn.getHeight();
        debug("x=" + x + " ,y=" + y);
        return x + ((y + he.getInitial()) * 16);
    }

    // mouselistener
    public void mouseClicked(MouseEvent e) {
        if (e.getClickCount() == 2) {
            // double clicked
            he.setNewTextListener.actionPerformed(new ActionEvent(this, 0,
                    "NEWASCII"));
        }

        debug("mouseClicked(" + e + ")");
        he.cursor = calculateMousePosition(e.getX(), e.getY());
        this.requestFocus();
        he.repaint();
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    // KeyListener
    public void keyTyped(KeyEvent e) {
        debug("keyTyped(" + e + ")");

        // only add content, if buffer is large enough
        if (he.buff.length > he.cursor) {
            he.buff[he.cursor] = (byte) e.getKeyChar();

            if (he.cursor != (he.buff.length - 1))
                he.cursor++;
            // change this
            // System.out.println( he.buff.toString());

            he.repaint();
        }
    }

    public void keyPressed(KeyEvent e) {
        debug("keyPressed(" + e + ")");
        he.keyPressed(e);
    }

    public void keyReleased(KeyEvent e) {
        debug("keyReleased(" + e + ")");
    }

    public boolean isFocusTraversable() {
        return true;
    }

    public static String getModifiedText() {
        return modifiedText;
    }

    public void setModifiedText(String modifiedText) {
        this.modifiedText = modifiedText;
    }
}
  • 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-06-14T00:25:29+00:00Added an answer on June 14, 2026 at 12:25 am
    • develop a hex editor (encoder.isLegalReplacement...)

    • use JTextField with DocumentFilter, or easiest could be JFormatterTextField instead of paint chars by using paint() to JComponent

    • use paintComponent (instead of paint()) for Swing JComponents

    • add DocumentListener to the JTextField with DocumentFilter or JFormatterTextField

    • interesting way could be to built / prepare / create a ArrayList of hex () and to use AutoComplete JTextField

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to develop my first ASP.NET MVC web app and have run into
I am trying to develop an application for my localhost on which I can
I was trying to develop a video management software for which I was evaluating
I am trying to develop the XMPP Gateway which can send/receive from standard XMPP
Trying to develop a text editor, I've got two textboxes, and a button below
I am trying to develop an Android based application, which can play video from
I am trying to develop an application which will record voice and plot a
I'm trying to develop a responsive navigation menu which dynamically creates a More.. menu
I'm trying to develop an app which first checks if the device is connected
Im trying to develop a small site which retrieves a results from a database

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.