So my questions is pretty basic. I have a code of 7-8 lines which does the exact same thing, gets the text of 4 Text Panes and updates them to a 5th Text Pane, now thing is whenever any change is made to one of those 4 Text Panes, this 5th text pane will be updated, so I have to use that same 7-8 lines of code on all the ActionPerformed methods of those 4 Text Panes. Is there any way to make a method somewhere which has those 7-8 lines and just call it in all those 4 Text Panes.
I just read you're using the NetBeans graphical editor and it auto generated those methods for you.
The easiest solution would be to create a method yourself somewhere all the other methods can access and then just call that in all the ActionPerformed()s.
// use the same parameters as currently given to your ActionPerformed
public void ActionPerformed1(Event param) {
onTextPaneUpdate(param);
}
public void ActionPerformed2(Event param) {
onTextPaneUpdate(param);
}
public void ActionPerformed3(Event param) {
onTextPaneUpdate(param);
}
// use the same parameters as currently given to your ActionPerformed
public void onTextPaneUpdate(Event param) {
// your code for all methods
}
If the ActionPerformeds are in different classes / files, you can just create your own class e.g. MyTextPaneUpdateListener and declare this onTextPaneUpdate method static, this way you could call it from everywhere without having any object to call it on. (MyTextPaneUpdateListener.onTextPaneUpdate(params...))
In case this doesn't help you please provide example code that you're currently using as the answer heavily depends on your current implementation.
Related
I defined a paste method in a controller class, based on the solution given here: How to copy/paste table cells in a TableView. Everything went well, except for one detail: some cells where data is pasted have events that should be triggered but are not.
For example:
public class MyController {
private TableColumn<MyBean, String> valueColumn;
...
valueColumn.setOnEditCommit(e -> doSomeStuff(e));
private void doSomeStuff(CellEditEvent<MyBean, String> event) {
...
}
In this example, after user hits ENTER, the doSomeStuff method is called, which is expected behavior.
The problem with the paste method I implemented is that it does not affect the cell, only its content (its ObservableValue). This means of course that after data is pasted, no event is triggered.
My question : is there a way to trigger the same event, or a similar one that will call my doSomeStuff method after pasting data?
Table View doesn't work as you think. If you want to have a fully customizable structure, use a grid pane. It is hard to create one, but after you make it look like a table, you have many more options to customize.
I did that I a recent project where I was needed to insert a table inside a cell. It was much more easier with a gridpane and textfields.
Different screen readers (VoiceOver, NVDA, Narrator) ignore my code on different locations of the UI. The method getName(AccessibleEvent e) gets called and supplies the correct result, yet it sometimes is ignored by either VoiceOver, NVDA or Narrator. This behaviour seems so random.
The following case is working with VoiceOver, yet NVDA completely ignores it.
Accessible accessible = getControlForAccesible().getAccessible();
accessible.addAccessibleListener(new AccessibleAdapter() {
#Override
public void getName(AccessibleEvent e) {
e.result = getTextForScreenReader();
}
});
This code is inside a subclass of Composite which acts as button in my case. So there is no subview inside this button. I draw the text with the drawText-method of GC. It also attached a tooltip to the composite which is ignored as well.
No matter which of the methods of the AccessibleAdapter i use, still the same result, whereas on some other parts of my code, those different methods supply different results.
Does anyone had similar problems?
For example, building a GUI in the Swing library, you often need to build a few parts within a JPanel.
eg.
public CustomPanel extends JPanel {
public CustomPanel() {
super();
// Build the slider.
{
...
}
// Build the combo boxes.
{
...
}
}
}
It seems the following are my options:
Use the {} the way I am currently using them (basically for indentation of similar code).
Create custom methods for each component eg. public JSlider createCustomSlider().
Create custom classes for each custom component (this seems a bit overkill if I have a number of one off components).
Is there a downside to how I indent my code with {}?
EDIT:
Also, as an additional benefit, the braces currently serve to scope my variables. That means I'm always certain that I don't accidentally use them further down the line.
I'd use methods. Having brackets doesn't add anything to the documentation that you don't already have in your comments, having well named methods is always a good idea.
Downside of your method -- you'll be the only one who understands why you did it that way.
I am having trouble understanding the undo/redo functions using UndoManager, and integrating it with the MVC model.
I am not sure where to put the various methods(in model, view or control)
and I am still not sure how to use the undo manager.
My control class implements UndoableEditListener
It creates:
private UndoManager manager = new UndoManager();
and in:
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Undo")) {
try {
manager.undo();
} catch (CannotUndoException ex) {
ex.printStackTrace();
}
}
}
I understand up to here, but the rest I am not sure what to do. I know I will have to add more in the model and view class, but not sure where.
DO I have to have the following classes?
public class UndoAction extends AbstractAction {}
public void undoableEditHappened(UndoableEditEvent e) {
I am simply placing an integer number in a textfield, and then I want to be able to undo this.I set the number in the textfield in the view class.I want to acheive this the simplest way possible, no fancy coding! This is a minor part of my assg but I just cant get it working!!
==========================================================
Here is a more detailed description of my code, maybe it will help:
I have a model, view and control package.
Contol has:
ButtonGUIControl.java, which implements both
ActionListener and
UndoableEditListener.
final UndoManager manager = new UndoManager();
In the actionPerformed method, it calls
if (e.getActionCommand().equals("Undo")){
try {
manager.undo();
}
and in:
public void undoableEditHappened(UndoableEditEvent evt) {
manager.addEdit(evt.getEdit());
}
In the View:
Grid.java , which extends JTextField will add the following, wherever it needs to display a number on the GUI:(model is simply an instance of my Model class)
getDocument().addUndoableEditListener(new ButtonGUIControl(model));
Could it be because the UndoManager is being created in a different package? I really have no idea how to debug this anymore!!
I could post my entire code if that helps. I guess Im not sure how to integrate this with my mvc model structure.
Take a step back for a second. The whole idea here is that a user will use your app and will make a series of changes to something. A text editor is a good example. You can insert characters and lines, delete them again, replace text with other text, scroll the text, etc. In order to support this with MVC you have a model that holds state and a View that displays it.
Your first instinct might be to have the view directly access the model, and then refresh the view every time the user makes a change, but it's very hard to undo those changes with that implementation. Instead, you encode every kind of change the user can make in classes that are able to perform that change and can later undo that change.
For example, an action that inserts text would be implemented by a class that knows the character offset of the insertion point and the string of characters that is to be inserted. The perform action would insert the string at the offset and the undo action would remove the right number of characters after that insertion point. You'd have a different class that would handle deletion, another to handle scrolling etc.
Every time the user takes some action, the view would construct one of these UndoableEdit classes and would tell the instance to run itself (redo()). Once executed, you put that UndoableEdit at the end of a list of UndoableEdit instances that represent all of the actions the user has taken so far. This list makes it very easy to support any sequence of undo requests, redo requests and actual edit actions (resulting in more UndoableEdit's being put on the list).
So back to your question. If your app needs to support undo and redo, then you'll need to implement an UndoManager which simply manages the list of UndoableEdit's and performs undo and redo as necessary. You also have to implement a whole bunch of UndoableEdits, one for each kind of thing your user will do against the UI. As for a listener, I can't see that you really need to do that one.
If you need only simple undo/redo, you can use UndoManager as it is, you don't need to subclass or customize it in any way.
JTextField (more specifically its model, the Document) has some built-in support for undo, which means you don't need to write UndoableEdit implementations either, the UndoableEdit objects will be automagically created for you (actually AbstractDocument.DefaultDocumentEvent implements UndoableEdit).
Full simple working example is here
I started to look into using GWT in combination with UiBuilder. I'm a bit puzzled about how you can use the #UiHandler(..) directive to make simple event handle code as written down in the GWT documentation:
#UiHandler("button")
void handleClick(ClickEvent e) {
Window.alert("Hello, AJAX");
}
In this case the method handleClick is used.
How do you know for each GWT widget what methods can be created with #UiHandler? For some you can also create a doClose() method.
But what can you use with, for instance, a ListBox to get an event an item is selected? Where in the documentation can I see this?
The parameter you pass to the #UiHandler annotation is the name of the appropriate field you want to assign that *Handler. So, in this case you are assigning a ClickHandler to a Button button (actually, we just know the field's name).
As for how this exactly works - it's part of GWT magic :) My guess is that, just like any other UiBinder related code (I think there was a presentation on Google IO, that showed the code that UiBinder generates), at compilation time the compiler figures out what goes where. In this example: we have a Button button, and we have a #UiHandler annotated method that has a ClickEvent parameter -> that must mean it's a ClickHandler (notice that the method's name doesn't matter). So let's add some code at compile time (in the constructor, probably) that adds that handler to the button. If you are interested in a more comprehensive answer - check out the source :D
But what can you use with, for
instance, a ListBox to get an event
an item is selected? Where in the
documentation can I see this?
In the GWT API reference. In this case, you are probably looking for ListBox.addChangeHandler. But you usually won't find #UiHandler related code there - that's because it would be redundant - you always construct the #UiHandler methods the same way:
You check the *Handler that you want to add, say ChangeHandler
It has a void onChange(ChangeEvent event) - so, your method needs a ChangeEvent parameter and should look like this:
#UiHandler("listBox")
void whateverName(ChangeEvent event) {
// ...
}
Probably your problem is in your onModuleLoad method:
public void onModuleLoad()
{
HelloWorld helloWorld = new HelloWorld("BOTAO");
// Using this way #UiHandler will not work
//Document.get().getBody().appendChild(helloWorld.getElement());
// correct way
RootPanel.get().add(helloWorld);
}