How to check if the tab selected is changed in java - java

As far as i have seen the event:
(1) private void jTabbedPane1StateChanged(javax.swing.event.ChangeEvent evt) {}
Checks whether a new tab is added or an exiting tab is deleted or not.
On googling , i found this code:
(2) ChangeListener changeListener = new ChangeListener() {
public void stateChanged(ChangeEvent changeEvent) {
// my code
}
};
jTabbedPane1.addChangeListener(changeListener);
I guess since it uses stateChanged event , it should do what the same a my first code.
By t way even after using both the codes i could not get the required resuts(ie An event that could be invoked when user changes the tab).
Can anyone suggest me a good event [i am using netbeans GUI environment] for effective action. (I dont want any mouseEvents)
Edit:
I want the following code to be excecuted if the tab changes:
String send3=( jTabbedPane1.getSelectedComponent().getComponentAt(0,0)).getName();
The above code dynamically gets the name of jTextarea (in the current tab) which is created dynamically in the jTabbedPanel.

I just checked my own source code where addChangeListener() works fine. The event is fired whenever the tab is changed by the user or programatically. In stateChanged() itself, the now selected tab is determined by
JTabbedPane p = (JTabbedPane)e.getSource();
int idx = p.getSelectedIndex();

Related

Java (JFace Application Window) Setting external label text

I am looking to figure out how to set the text of a label on an external Application Window.
What I have:
I have two windows so far. The first one is the main application window that will appear when the user starts the program. The second window is another separate window that I have created specifically to display a custom error window.
The problem: I seem to be unable to call the label that I have created on the error window and set the text to something custom. Why? I want to be able to reuse this window many times! This window is aimed for things like error handling when there is invalid input or if the application cannot read/save to a file.
I was going to post screen shots but you need 10 rep for that. It would have explained everything better.
Here is the code for the label on the Error_dialog window:
Label Error_label = new Label(container, SWT.NONE);
Error_label.setBounds(10, 10, 348, 13);
Error_label.setText("Label I actively want to change!");
Here is the condition I would like to fire off when it is met:
if(AvailableSpaces == 10){
//Set the label text HERE and then open the window!
showError.open();
}
I have included this at the top of the class as well:
Error_dialog showError = new Error_dialog();
Just save the label as a field in your dialog class and add a 'setter' method. Something like:
public class ErrorDialog extends Dialog
{
private Label errorLabel;
... other code
public void setText(String text)
{
if (errorLabel != null && !errorLabel.isDisposed()) {
errorLabel.setText(text);
}
}
You will need to use your dialog like this:
ErrorDialog dialog = new ErrorDialog(shell);
dialog.create(); // Creates the controls
dialog.setText("Error message");
dialog.open();
Note: you should stick to the rules for Java variable names - they always start with lower case.
Further learn to use Layouts. Using setBounds will cause problems if the user is using different fonts.

JFace TableViewer - Deselected rows reselect themselves automatically, with bonus asynchronous weirdness

I'm writing an eclipse RCP program that has a single-select JFace TableViewer in the GUI. Single select TableViewers don't have a default way of deselecting the selection, so I figured I'd add one by adding my own listener. The idea is to clear the selection if the user clicks on the already-selected row.
myTableViewer.addPostSelectionChangedListener(new ISelectionChangedListener(){
private boolean inLoop = false;
private StructuredSelection previousSelection = StructuredSelection.EMPTY;
#Override
public void selectionChanged(SelectionChangedEvent event) {
//Needed because setting the selection causes the listener to be called again.
if (inLoop) {
inLoop = false;
return;
}
StructuredSelection newSelection = (StructuredSelection) event.getSelection();
if (newSelection.equals(previousSelection)) {
newSelection = StructuredSelection.EMPTY;
inLoop = true;
myTableViewer.setSelection(StructuredSelection.EMPTY);
}
previousSelection = newSelection;
}
});
This unfortunately doesn't work. The selection is very briefly cleared, but about .2 seconds later the TableViewer automatically reselects the row that I just barely deselected!
It gets better. I debugged the JFace code a little to try and solve the issue. The TableViewer delegates its selection code to a StructuredViewer. If I put a breakpoint in StructuredViewer.setSelection(...), then all of a sudden my code works.
Apparently the PostSelectionChangedListener is poorly named and fires before the selection code is done running. My listener then races the StructuredViewer code, ends up changing the value too early, and then immediately gets overwritten.
Is there any way to avoid this? I'd really like to be able to deselect rows in the table viewer without having to add a clunky deselection button or keybinding. So far I've tried calling myTableViewer.getTable().setSelection(...), as well as packing the setSelection() call into a Display.asyncExec(...), but with no luck as yet. Any tips would be greatly appreciated!

Understanding button/mouse listener in Netbeans GUI generated code

can any tell me what this part of code is doing?
jButton1.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mouseClicked(java.awt.event.MouseEvent evt)
{
jButton1MouseClicked(evt);
}
});
why are there methods in the parameter for the method addMouseListener? can some one explain in details? im using netbeans and this is code generated.
It's an anonymous MouseAdapter, meaning it calls a new class instance without a variable/assignment. The code itself is calling a new custom event handling method jButton1MouseClicked(), which is what netbeans generates for you so that you can add in your own handling for the code.
Yes, don't add a mouse listener to a button. Well, I use this method to get the position of X and Y mouse click in my GUI using NetBeans.
Right-click the swing container: Choose Events: choose Mouse: choose MouseClicked. Add the following piece of code; (of course my text fields have the variable names tfMouseX and tfMouseY).
tfMouseX.setText(evt.getX() + "");
tfMouseY.setText(evt.getY() + "");

How to limit ModifyListener for user interaction only

I have a textbox with attached ModifyListener.
In implemented modifyText(ModifyEvent e) I execute desired functionality.
The problem with that, that this event is triggered on every text change.
I don't want it to trigger if text was altered programmaticly (by setting text via code).
I want it to trigger only when user changes the code (I can't use keylistener because it will be triggered also when user click on arrow buttons and etc, it also won't detect if user copy&paste text)
You could unregister your ModifyListener before calling setText(..) and reregister it afterwards.
How about textBox.addKeyListener(...) and textBox.addMouseListener(...) instead of ModifyListener?
You can try using Focusout listener.... then you will get the value which user has entered only once.
Text text;
text.addListener(SWT.FocusOut, new Listener() {
#Override
public void handleEvent(Event arg0) {
//Your code here.....
}
});

SWT, Maintain default tab ordering when adding Key Listner

I've been creating a custom TabFolder extension that adds a key listener to allow quick tab switching using an ALT + # hotkey.
By adding the KeyAdapter to my TabFolder, the event handler works properly only when you have a tab header selected (in which case the ALT + ARROW_LEFT/ARROW_RIGHT also work.). I need this hot key to be active when any Widget with-in the TabFolder is active; however, it shouldn't be active if the selection is in a different tab folder or widget outside of a tab folder.
In an attempt to solve this, I wrote a simple recursive function to apply the key listener to all of the children of the tab folder:
public void applyQuickSwitchKeyBindings() {
removeKeyListener(ka);
addKeyListener(ka);
for(Control c: getChildren())
applyQuickSwitchKeyBindingsToChildren(c);
}
private void applyQuickSwitchKeyBindingsToChildren(Control c) {
if(c==null) return;
if(c instanceof Composite) {
Control[] controls = ((Composite)c).getChildren();
for(Control c2: controls)
applyQuickSwitchKeyBindingsToChildren(c2);
if(controls.length < 1) {
c.removeKeyListener(ka);
c.addKeyListener(ka);
}
}
}
Then i call the applyQuickSwitchKeyBindings() after I add the controls to each TabItem in the tab group.
The good news was that the quick switch hot key (ALT + #) worked great!
The bad news was that the original TAB ordering based on z-index is now gone. When you hit the SWT.TAB key you lose focus on your current text box and don't gain focus on anything else...
Questions:
1.) Can each control only have one KeyListener?
2.) Why is the original TAB traversal not working anymore?
Thanks in advance!
to 1) I'm pretty sure that more than one KeyListener is allowed.
to 2) I'm not sure, that depends on what you're doing in your KeyAdapter. Maybe you can post that too?
I just the tab order is broken somehow, you can reset ( or change ) it with a call to setTabList( Control[] ).
setTablList( new Control[] {
control1,
control2,
control3,
....
} );
So after more time learning and developing with SWT i've discovered my problem. When you add a listener it is applied to the widget/control you call the addXXXListener function on. So if that control is not active the listeners will not be fired.
The solution seems to be SWT's global Filter mechanism which allows you to add global application(Display) scope listeners.
Display.getCurrent().addFilter(SWT.keyPress, new KeyPressListener());
Pardon the incorrectness of this line, but if you google it you'll see what i mean.
I have also read to use this sparingly.

Categories