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
}
}
Related
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();
}
}
});
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
I am creating a football draw app. I currently have 9 text areas which hold 6 different teams. I have attached a MouseListener to each text area. When you click on the text area, you see a new window with each team seperated into a group format.
I have an issue trying to get the text from the text areas. I could achieve this by adding a MouseListener to each individual text area but this violates the Don't Repeat Yourself (DRY) principle as far as I am aware.
I have included my code below:
gui.getTable1().addMouseListener(new tableListener());
gui.getTable2().addMouseListener(new tableListener());
gui.getTable3().addMouseListener(new tableListener());
gui.getTable4().addMouseListener(new tableListener());
gui.getTable5().addMouseListener(new tableListener());
gui.getTable6().addMouseListener(new tableListener());
gui.getTable7().addMouseListener(new tableListener());
gui.getTable8().addMouseListener(new tableListener());
gui.getTable9().addMouseListener(new tableListener());
public static class TableListener implements MouseListener {
#Override
public void mouseClicked(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
}
#Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
//get text from text area and pass to new GUI
}
#Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
I would like to use the TableListener private class for all my text areas instead of 9 different MouseListeners. I think this can be done in a single line but I can't think how. Can someone please help?
Attach just one instace of listener to all the textareas and use e.getSource() to get event source textarea.
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.
Say If i have two classes, in each class is a different JFrame, e.g JFrame A and JFrame B ( in seperate classes).
Now from the constructor of JFrame A I may push a button with an actionlistener attached, which will instantiate the other class, thus creating JFrame B. The problem is when JFrame B is created, both the JFrames are visible. If i close JFrame B, then JFrame A closes as well. How can i make it so only JFrame B closes?
Thanks
edit DISPOSE_ON_CLOSE does not work for me, it closes all the jframes.
some sample code:
public class classone {
public classone() {
JFrame a = new JFrame("this is A");
classtwo newFrame = new classtwo();
}
}
public class classtwo {
public classtwo() {
Jframe b = new JFrame("this is B");
b.setDefaultCloseOperation(b.DISPOSE_ON_EXIT);
}
}
please ignore any syntax errors, just for demonstration.
For the JFrame B, set the default close operation to "dispose" as shown below :
frameB.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Then closing the child windows won't shut down your entire application.
HTH ! ;-)
Do you have DISPOSE_ON_CLOSE on one frame and EXIT_ON_CLOSE on the other? If so then that would explain why your program is exiting prematurely. Ensure that all frames are set to DISPOSE_ON_CLOSE.
I got question now. Just when you create an instance of a Window tell how this object live, Review this code
...
new JFrame(){
#Override
public synchronized void addWindowListener(WindowListener l) {
// You may ask here also add windowClosing method and look at my previous post
super.addWindowListener(l);
}
}.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
....
Just DO_NOTHING_ON_CLOSE and addWindowListener in WindowClosing method show a JOptionPane.showConfirmDia and if result return no(1) then return; else system.exit(0);
its all
I see my first StackOverFlow post ,What a shame! I'm editting my post.here you are;
Until now , I realize Depending developing software approachs Swing getting older. I'm missing a technology like Microsofts XAML.
soyatec inc. has some deals using XAML with java you may have a look but "In my opinion" not successfull work.Anyway...
JFrame frame=new JFrame();
frame.addWindowListener(new WindowListener() {
#Override
public void windowClosing(WindowEvent e) {
int result= JOptionPane.showConfirmDialog(JOptionPane.getRootFrame() //or your parent swing element
, "Sure ?");
switch (result) {
case 1:
break;
default:
System.exit(0);
break;
}
}
#Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
#Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
#Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
#Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
#Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
#Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
}
);