Java JTextArea动态列和行编号 [英] Java JTextArea dynamic column and row numbering

查看:112
本文介绍了Java JTextArea动态列和行编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个快速的谷歌,我似乎无法找到一个很好的解决方案,主要是因为我不知道如何描述它。



基本上,我需要在 JTextArea 中显示任意数量的十六进制字符,并且我希望它们的间隔均匀,并且具有字符的位置显示在字符的顶部和左侧。



这是我希望实现的一个例子,这是十六进制查看器WinHex。





我一直在尝试将字节数组转换为字符串,然后对其进行文本包装,但是我得到了一些奇怪的结果。任何意见,如何实现类似的东西,将不胜感激。



我考虑的另一个选择是使用JTable,但我想知道如果这是复杂的略有关系。也许。

谢谢

解决方案

一个非常简单的 AbstractTableModel 的实现。这只花了我15分钟的时间来撰写(以回应过分复杂的问题)。

  import java.awt.BorderLayout; 
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

public class HexText extends JFrame {
public static void main(String ... args){
final HexText window = new HexText();

EventQueue.invokeLater(new Runnable(){
@Override
public void run(){
window.setVisible(true);
}
});
}

private static class HexTableModel extends AbstractTableModel {
List< Integer> data = new ArrayList<>();

@Override
public int getRowCount(){
return data.size();
}

@Override
public int getColumnCount(){
return 9;

$ b @Override
public Object getValueAt(int rowIndex,int columnIndex){
if(columnIndex == 0){
return Integer.toHexString (rowIndex<< 5);
} else {
int row = data.get(rowIndex);
int theByte = 0xFF& (row>>(columnIndex * 2));
字符串输出= Integer.toHexString(theByte);
if(output.length()== 1)
output =0+ output;
返回输出;
}
}

public void addRow(int rowElement){
data.add(rowElement);

fireTableRowsInserted(data.size() - 1,data.size() - 1);


$ b $ public HexText(){
JPanel contentPane = new JPanel(new BorderLayout());

HexTableModel theModel = new HexTableModel();

JTable theTable = new JTable(theModel);

Random r = new Random(); (int i = 0; i <20; i ++){
theModel.addRow(r.nextInt());


}

contentPane.add(theTable,BorderLayout.CENTER);
this.add(theTable);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.pack();
}
}


I've had a quick Google around, and I can't seem to find a good solution to this, mostly because I'm not sure how to describe it.

Essentially, I need to display an arbitrary amount of hex characters in a JTextArea, and I'd like to have them spaced evenly, and have the positions of the characters shown at the top and left of the characters.

This is an example of what I'd like to achieve, this is the hex viewer WinHex.

I've been playing around with converting a byte array to a String, and then text-wrapping it, but I've had some odd results. Any advice on how to achieve something similar to this would be appreciated.

Another option I've considered is using a JTable, but I'm wondering if that's over complicating the matter slightly. Maybe.

Thanks

解决方案

This should get you started, using a very simple implementation of AbstractTableModel. This only took me 15 minutes to write (in response to "overcomplicating the issue").

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

public class HexText extends JFrame {
  public static void main(String... args) {
    final HexText window = new HexText();

    EventQueue.invokeLater(new Runnable() {
      @Override
      public void run() {
        window.setVisible(true);
      }
    });
  }

  private static class HexTableModel extends AbstractTableModel {
    List<Integer> data = new ArrayList<>();

    @Override
    public int getRowCount() {
      return data.size();
    }

    @Override
    public int getColumnCount() {
      return 9;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
      if (columnIndex == 0) {
        return Integer.toHexString(rowIndex << 5);
      } else {
        int row = data.get(rowIndex);
        int theByte = 0xFF & (row >> (columnIndex * 2));
        String output = Integer.toHexString(theByte);
        if (output.length() == 1)
          output = "0" + output;
        return output;
      }
    }

    public void addRow(int rowElement) {
      data.add(rowElement);

      fireTableRowsInserted(data.size() - 1, data.size() - 1);
    }
  }

  public HexText() {
    JPanel contentPane = new JPanel(new BorderLayout());

    HexTableModel theModel = new HexTableModel();

    JTable theTable = new JTable(theModel);

    Random r = new Random();

    for (int i = 0; i < 20; i++) {
      theModel.addRow(r.nextInt());
    }

    contentPane.add(theTable, BorderLayout.CENTER);
    this.add(theTable);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.pack();
  }
}

这篇关于Java JTextArea动态列和行编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆