JTabbedPane click event - java

I need to be able to process a click on the tabs in a JTabbedPane. I'm not using this to change tabs, and this isn't going to trigger on tab change. What I'm attempting to do is close the tab when it is right clicked. However, I'm not sure how I can access the tab to add a click event on it. Most of the questions related to clicking on JTabbedPanes suggest using a ChangeListener, but that won't work, since the tabs aren't going to be changed on right click.
Is there any way for me to add a click event to a JTabbedPane's tab?

Is there any way for me to add a click event to a JTabbedPane's tab?
Read the section from the Swing tutorial on How to Use TabbedPanes for a working example on how to close a tab with a mouse click.
Keep a link to the tutorial handy for Swing basics.

Sorry for late answer, but I found this very usefull for me and for avoid extra clicks detected by stateChanged (with this you can detect all you want in "click tab"):
myJTabbedPane.addMouseListener(new MouseListener()
{
#Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("Panel 1 click");
}
#Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
});
Finally, if you want to detect right click on tab you can see next tutorial (search getModifiers() in next page):
https://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html

Related

KeyListener on a disabled button

I have a series of buttons in my code. I have added key listeners to individual buttons to listen to keys, so that when user presses RIGHT, LEFT,UP DOWN, I can transfer focus to the next button.
Note: I know that TAB can be used
now everything works really great! but when the focus is at a disabled button. I am not able to listen to it.
Any suggestions, as to how I can go around the problem?
Please pardon me before hand for amateur coding style!
addEntry.addKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
#Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
#Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{calPeriod.setFocusable(true);
calPeriod.grabFocus();
}if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
getTime.setFocusable(true);
getTime.grabFocus();
}
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
genChart.setFocusable(true);
genChart.grabFocus();
}
}
});

JTabbedPane action performed

Im looking for way where an action can be performed while using a tabbed pane whenever new tab is opened.
Something like formwindowopenned
Im looking for way where an action can be performed while using a tabbed pane whenever new tab is opened.
I assume you mean when a user clicks on an existing tab to switch to that tab. If so, then you can add a ChangeListener to the tabbedPane and listen for the stateChanged event.
If you are talking about adding a new tab to the tabbed pane, then you would just manage that in your application logic.
All actions on tabs (this will get you extra actions, not only mouse clicks, for example, if you change tab by code using selected index, this code will be executed):
ChangeListener changeListener = new ChangeListener() {
public void stateChanged(ChangeEvent changeEvent) {
JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent.getSource();
int index = sourceTabbedPane.getSelectedIndex();
System.out.println("Tab changed to: " + sourceTabbedPane.getTitleAt(index));
}
};
myJTabbedPanel.addChangeListener(changeListener);
Only mouse clicks on tabs:
/**
* Detects clicks when user click tab inside tabbed pane
*/
private void addMouseEventToPanel(){
this.myJTabbedPanel.addMouseListener(new MouseListener()
{
#Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("You clicked on tab number "+this.myJTabbedPanel.getSelectedIndex());
}
#Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
});
}

Press a Key to Open a file in a Java program

I developed a Java program that open specific files when the user clicks on a Jlabel. The click event worked very well. I added a keypress event so that a user can press a key on the keyboard to open the file but it's not working.
Can anyone show me how to use a key event listener to open a file or file path in Java. Am new to Java an still learning. I need your help. Please
In order to make that to work you need to understand how is the KeyListener working.. the widget needs to have the focus gained in order to catch that event and IMHO set the focus on a JLabel makes no much sense... hence in this case you can just work with a keyListener assigned to the JFrame
so my suggestion would be to work on a keylistener on the JFrame and define the keys you need for the open the file there..
Example:
public class Test extends JFrame implements KeyListener{
.....
.....
#Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
#Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
#Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}

How to use getsource with listeners

I am making a tool which writes the data I give to a file. Where I need help is I have four JTextFields in which I want to apply the same mechanism to each one. That mechanism will be written independently from the frame code, and would be about checking the data input into the field and other stuff.
I wrote that mechanism to each field and it worked, and now I am thinking about shortening the code by isolating that mechanism. I know that I must use action listeners and document listeners, but I can't figure out how to make it work with <code>e.getsource()</code> method.
Here is the mechanism that I need to isolate:
txtNa_1 = new JTextField();
//the mechanism will be about setting the field text in the beginning
//to "N/A", when focused sets to "", and when
//focus is lost it preserve the index.
txtNa_1.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent arg0) {
if (txtNa_1.getText().equals("N/A")){
txtNa_1.setText("");
}}
public void focusLost(FocusEvent arg0) {
if (txtNa_1.getText().equals("")){
txtNa_1.setText("N/A");
}}});
txtNa_1.getDocument().addDocumentListener(new DocumentListener() {
public void removeUpdate(DocumentEvent arg0) {
// TODO Auto-generated method stub
txtNa_23 = txtNa_2.getText();
}
public void insertUpdate(DocumentEvent arg0) {
// TODO Auto-generated method stub
String txtNa_23 = txtNa_2.getText();
}
public void changedUpdate(DocumentEvent arg0) {
// TODO Auto-generated method stub
txtNa_23 = txtNa_2.getText();
}});
Simply put, can somebody explain how to cast a <code>getsource</code> along with what I am trying to make? Help appreciated.

GXT Window is there a onblur event

I am using GXT window to create a popup edit window for editing a form field. As I edit the text field, the form field also changes. But I want to capture when the user has clicked outside of the window. That is, I want to capture blur event on a window, is that possible?
Here is a sample of my code for the window:
final Window window = new Window();
window.setSize(450, 100);
window.setPlain(true);
window.setModal(true);
window.setBlinkModal(true);
window.setHeading("Edit Text");
window.setLayout(new FitLayout());
window.setIconStyle("icon-edit");
window.addWindowListener(new WindowListener() {
public void windowHide(WindowEvent wevent) {
//do something
}
});
Can anyone help? Thanks
This will work for GXT3. GXT2 seems to have the same window.addDomHandler api though so may work in that version as well.
I threw in the MouseOutHandler version too just in case you decide the user shouldn't have to click... Anyway choose your favorite!
window.addDomHandler(new BlurHandler() {
#Override public void onBlur(BlurEvent event) {
// TODO Auto-generated method stub
}
}, BlurEvent.getType());
window.addDomHandler(new MouseOutHandler() {
#Override public void onMouseOut(MouseOutEvent event) {
// TODO Auto-generated method stub
}
}, MouseOutEvent.getType());
By the way, when adding a DomHandler you don't need to sink the event.

Categories

Resources