1

Sorry about the vague title, it's hard to explain what I mean without an extensive description.

I have a CPU simulation program that I have built, and within the GUI of this program I have a JTextArea that is populated with strings of text that look like the following

0x01 : $00 (0) [BRK]

These lines of text represent the values held inside RAM memory of size 256 bytes. The JTextArea contains 256 of these lines, each one displaying what is being held in that particular memory address. It looks something like this

0x01 : $00 (0) [BRK]
0x02 : $00 (0) [BRK]
0x03 : $00 (0) [BRK]  <-- PC
0x04 : $00 (0) [BRK]
0x05 : $00 (0) [BRK]
...
0xFF : $00 (0) [BRK]

Also present is the PC arrow that displays the current position of the program counter as the memory is stepped through. With each step of the program, the PC is incremented by two. As such, if the PC is pointing at 0x03, and the program is stepped, the PC will then be incremented to 0x05.

My problem, is that my JTextArea can only display 12 of these strings at one time due to its size, this means that after 6 steps the PC arrow will disappear off the bottom of the TextArea, and the user will have to manually scroll down to see it again.

Is there a way that I can make it so that as the PC arrow descends down the list of memory addresses, the JTextArea scrolls with it so that the PC arrow is always in the center of the TextArea no matter how far down the TextArea it is?

I have tried using the following code to achieve this, but it doesn't seem to cause any difference at all.

        int caretVal = 0;
        int pc = 0;

        try
        {
            for(int i = 0; i < steps; i++)
            {
                step();
                pc = mos6502.getCpu().getPc();
            }

            if(pc > 5)
                caretVal = pc + 11;

            gui.refreshGui();
            gui.setMMCaret(caretVal);
        }
        catch(...)
        {
            etc...
        }

I am assuming that this method doesn't actually do what I believe it to. But I'm not too sure.

Edit: Also, I assume this is relevant. After every step in the program, a gui.refreshGui() method is called. This method causes the JTextAreas to be updated with new information, this causes the JTextArea's position to shoot to the bottom, as it's being filled with new information.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I would (highly) recommend using a `JTable` over a `JTextArea` for this purpose – MadProgrammer Mar 08 '18 at 00:51
  • @MadProgrammer I'll have a look into it, what's the difference between the two exactly? –  Mar 08 '18 at 00:53
  • [Maybe like this](https://stackoverflow.com/questions/8791118/moving-jtextarea-scroll-to-caret) or [this](https://stackoverflow.com/questions/13437865/java-scroll-to-specific-text-inside-jtextarea/13438455#13438455) – MadProgrammer Mar 08 '18 at 00:54
  • `JTable` is intended to display structured data (in column/row format), `JTextArea` displays text – MadProgrammer Mar 08 '18 at 00:54
  • @MadProgrammer I've just looked up the documentation and tutorials. I understand why you suggest to use a JTable, however for the sake of simplicity and neatness, I like using the JTextArea and would rather stay with that option. The JTable looks rather messy compared to the strings I can place inside the JTextArea imo. –  Mar 08 '18 at 00:55
  • Sure, until you hit variable width font issues ;) – MadProgrammer Mar 08 '18 at 00:57
  • @MadProgrammer I'm using a fixed-width font. And I have programmed the updateGui() method to take into account the various widths of the parameters that will populate each string, therefore the `[BRK]` section of the string always appears in exactly the same position in every string, regardless of string contents. –  Mar 08 '18 at 01:00
  • The choice is your own, but I think you'd find it easier to manage a `JTable` then a `JTextArea`, in particular the "PC", as it could be an independent element, without needing to try and remove it from the text and re-add it in and all the possible mutation conditions you'll probably need to take into account. Even a `JList` would be a better choice - IMHO – MadProgrammer Mar 08 '18 at 01:11
  • @MadProgrammer I appreciate your input, and I'll happily take it into account the next time I'm in a similar situation. However this is less to do with my choice of Swing component and more to do with getting the arrow to follow along. –  Mar 08 '18 at 01:21
  • See the two links I linked to earlier – MadProgrammer Mar 08 '18 at 01:35
  • For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Mar 08 '18 at 06:03

1 Answers1

1

Check out the Text Utilities. This class contains static methods that you can use on text components.

I would think you would be interested in the centerLineInScrollPane(...) method. It will attempt to center the line with the caret in the center.

It uses the setViewPostion(...) method of the JViewport to position the line containing the Caret in the center of the viewport.

camickr
  • 321,443
  • 19
  • 166
  • 288