JTextPane and undo manager style change - java

My situation: I have a JTextPane with its own syntax highlighting. I have it set so that when the user stops typing, it updates the style in the text using the setCharacterAttributes() method.
My Issue: When these updates to the style are not performed, the undo manager works as expected. But when I do use it, the undo manager counts those style changes as actual undo-able actions! Meaning hitting Ctrl+z (I have it bound to undo when pressed) it just un-colors the last character i typed. Rather than actually removing/undoing it.
How would I get it so undo-ing and redo-ing only affects text changes and not style/font changes in my StyledDocument?
Thank you.

It sounds like you need to make use of addEdit or the Significant attribute as explained by the UndoManager:
The UndoManager makes use of isSignificant to determine how many edits
should be undone or redone. The UndoManager will undo or redo all
insignificant edits (isSignificant returns false) between the
current edit and the last or next significant edit. addEdit and
replaceEdit can be used to treat multiple edits as a single edit,
returning false from isSignificant allows for treating can be used to
have many smaller edits undone or redone at once. Similar
functionality can also be done using the addEdit method.
Sources:
https://docs.oracle.com/javase/8/docs/api/javax/swing/undo/UndoableEdit.html

Related

Getting the selectionLayer.getSelectedCells() as empty but not always

When I select a cell and start typing any value it edits the cell.
But when I am sorting few columns(2 to 3) by default using this code below,
natTable.doCommand(new SortColumnCommand(sortHeaderLayer, i, true, sortColumn.getSortDirection()));
and then select cell and start typing for first time or maybe first few times it edits then it won't.
When I debug the code I found that I was getting the selected Cells as empty after few successful editing, and that happens randomly like sometimes after 1 or 2 or 4 .. successful editing, then the selected Cell becomes empty. The cell is selected and is clearly visible but still we get empty in the code.
ActivateEditorAction.run()
The above code is used for the Action for the cell editing on selection. In the else block of the synchronized block when we are checking for isSelectionAndTypedKeyValidForEditing(), there the selected cells is coming as empty, not always but once it starts coming as empty then it keeps on coming as empty all the time even though selection is visible on the table.
Code for the function isSelectionAndTypedKeyValidForEditing()
There are certain events that cause a selection clearing. By default sorting is causing such an event.
If you are using GlazedLists you should also be aware that there are multiple events that are fired in case of sorting. The GlazedListsEventLayer tries to squash those events to a single event, but for huge lists it can happen that still multiple events are fired if the time frame in which GlazedLists is firing events is longer than the squashing time.
Maybe you are hitting a concurrency issue somewhere. There is not enough information about your NatTable setup and too much custom code that overrides the default configuration.
Apart from this information it seems to be a quite specific issue that depends on your configuration and your custom code. And that seems to be much more complicated than to be solved easily via Stackoverflow. IMHO you need professional support in this area. Some project members offer sponsored support in that area.

Elegant solution for cyclical UI updates in JAVA

I was trying to create a new UI component for the user to specify an integer value in two ways: using either a scrollbar, or a textfield. I wanted this to be a single UI component from which I can call something like integerField.getValue() and have it return the current integer value represented by the component.
My problem is, a classic cyclical update situation: when the underlying integer value is changed using the scrollbar, the textfield value needs to be updated, which triggers another event that causes the scrollbar to update - cycle complete.
In other words, scrollbar.setValue()->component.setValue()->textField.setText()->component.valueChanged()->component.setValue()->scrollbar.setValue()
Now, I can prevent this by having the first item in this chain setting a boolean flag and the other items in the component checking the flag before responding to a value change event. However, I'm not convinced that's an elegant solution.
Anyone have any better ideas?
Thanks!
A technical solution to the problem would be to read your text field and the last update time then your slider and the last update time. On the firing of the event, you would read the most recently updated value and if the values match stop updating and reacting.
SpinSlider, seen here and here, may be a good choice. It combines a JSpinner and a JSlider so that each component's ChangeListener listens to the other's ChangeListener.

Looking for an efficient Java Swing based console

I'm looking for a highly efficient Swing Java component which I can just plug into my application UI. I already tried using the classes within Swing such as JTextArea with no avail; they simply aren't high-performance enough and have any crippling drawbacks. Additionally, it'd be nice if it had standard console features such as scroll-lock, clear console, colours, and so on.
Edit: Forgot to say, this console will have a lot of debug information streaming into it, and it needs to be fully scrollable.
Cheers,
Chris
I fail to see what is wrong with using a JTextPane. It supports attributes which you can specify as each piece of text is added to the console. Clearing it is also obviously a no brainer. When added to a scroll pane it also supports scrolling.
You can add scroll locking by using Smart Scrolling.
Plus, it removes text too early and
No idea what that means as text is never removed unless you specifically remove it from the document.
doesn't allow the user to scroll while
input is being entered (afaik). The
effect is that you just see text
flashing while the number of rows
remains the same.
By default the text scrolls automatically as text is append to the document assuming the code is executed on the EDT. This scrolling can be controlled the the example provided in the link above.
Edit:
but I'd still like a library solution
I don't know of any
auto-colourise text coming from
different streams
The Message Console might give you some ideas.
(i.e., detect [error] prefix on a
line) and colourise lines based on
this)
This is easily done by adding a DocumentFilter to the Document of the text pane. You can add attributes as text is inserted into the Document.
Be sure that you read about the Event Dispatching Thread (EDT) in swing!
BTW: a simple search 'java swing console' will give you a lots of hints OR you could use/adapt the beanshell textfield which is a jtextfield too ...

How do I fire an action when the user leaves a JTextBox?

I've got a JTextField, and I'd like the system to do some processing on what the user typed whenever the user leaves the text field. The ActionListener that you can add to just the JTextField only fires when the user presses enter, however.
I'd like the processing routine to run whenever the user leaves the text box by any means - tabs, clicks out of it, presses enter, etc. (The processing in question is to save the text the user typed to the appropriate data object, nothing fancy.)
My google-fu has failed on this one: I'm confident that it's possible, I just can't see how.
Add a FocusListener.
It's worth noting that this is a relatively low-level listener. On JComboBox it wont work unless you find the text field (and perhaps button) that the particular PL&F inserts. Swing is a bit odd that way (amongst many other ways).
Although for my money, non-cosmetic changes that happen when focus leaves a field give poor user experience. Much better to do any relevant changes on every change with a listener on the text field's document.
If you want to edit the text as it is typed then you should use an DocumentFilter.
If you want to validate the text as a complete entity then you can use an InputVerifier.

How can I dynamically alter the Java LAF UIDefaults?

I'm working on cusomizing a Swing application by dynamically altering the UIDefaults. The end goal is to alter many of them (colors, fonts, sizes, borders, etc) and save the result on a per user basis. While it may give the application some non-standard looks, the client asketh and the client shall receive.
The only problem I'm running into is that GUI will only update ONCE. The first time I change a ui property everthing is great, subsequent changes don't affect anything.
// called from the EDT
// uiKeyName points to some ColorUIResource
UIManager.getDefaults().put(uiKeyName, <<color from color picker>>);
SwingUtilties.updateComponentTreeUI(rootWindow);
It works once, but never again. Ideas?
When you put the new color (for example) in the table, is it a Color object, or a ColorUIResource? As I understand it, the new value will only be taken up if the current value is null or an instance of UIResource. Thus, if you insert a plain old Color object, any subsequent changes you make will be ignored.
Also, as the linked doc page suggests, this may only help with the font and foreground/background colors; when it comes to changing things like borders and margins, you may be out of luck. This is not what Swing's Pluggable LookAndFeels were designed for. Maybe you could interest the client in a Synth-based LAF?

Categories

Resources