I was wondering how to add a focus gained event listener.
At the moment I have a Mouse Event which is being added to my
JTextareas
//=======================================================
// mouse drag event
//=======================================================
public static class genDrag extends MouseMotionAdapter {
JTextArea textarea;
// receive textarea as argument
public genDrag(JTextArea argTextarea) {
textarea = argTextarea;
}
// add drag functionality to argument
public void mouseDragged(MouseEvent E) {
Point p = SwingUtilities.convertPoint(textarea, E.getPoint(), gc_gui.cv_content);
textarea.setBounds((p.x - 40), (p.y - 15), 100, 30);
}
}
which I can then call using
//=======================================================
// apply mouse event
//=======================================================
JTextArea textarea = new JTextArea();
textarea.setBounds(50, 50, 100, 30);
textarea.addMouseMotionListener(new genDrag(textarea));
this works fine but I have been unable to reproduce the same
functionality for a focusGained event
//=======================================================
// mouse focus event
//=======================================================
public static class genFocus extends EventListener {
JTextArea textarea;
public genFocus() {
textarea = argTextarea;
}
public void focusGained(FocusEvent E) {
System.out.println("Focus Triggered");
}
}
The above doesn't seem happy at all
UPDATING CODE
static gui classGui;
public static void main(String[] args) {
classGui = new gui();
classGui.textarea.addMouseMotionListener(
new genDrag(classGui.textarea)
);
classGui.textarea.addFocusListener(
new genFocus(this)
);
classGui.frame.setVisible(true);
public static class gui {
JFrame frame;
JPanel panel;
JTextArea textarea;
public gui() {
frame = new JFrame();
// configure JFrame here
panel = new JPanel();
// configure JPanel here
textarea = new JTextArea();
textarea.setBounds(50, 50, 100, 30);
frame.add(textarea);
}
}
public static class genDrag extends MouseMotionAdapter {
JTextArea textarea;
public genDrag(JTextArea argTextarea) {
textarea = argTextarea;
}
public void mouseDragged(MouseEvent E) {
Point p = SwingUtilities.convertPoint(textarea, E.getPoint(), gc_gui.cv_content);
textarea.setBounds((p.x - 40), (p.y - 15), 100, 30);
}
}
public static class genFocus implements FocusListener {
JTextArea textarea;
public genFocus(JTextArea argTextarea) {
textarea = argTextarea;
}
public void focusGained(FocusEvent E) {
System.out.println("Focus gained");
}
public void focusLost(FocusEvent E) {
System.out.println("Focus lost");
}
}
}
To handle focus events, your handler needs to implement the FocusListener interface instead of EventListener.
Note that you need to add this handler via the addFocusListener. I don't think you did this, because if you had done this, you would have gotten a compiler error indicating what was wrong.
Use of the #Override annotation helps finding such errors. Put it above every method you think should override a parent method. If such a method does not actually override another method, the compiler will throw an error. This way you get informed of the mistake instead of your program failing silently.
you should add a event-listener to the control JTextArea then only it will be able to handle any event request.
JTextField textarea= new JTextField("Value");
textarea.addFocusListener(new genFocus(textarea)); //this peice of code will add an listener to you textarea Object of JTextField.
Your Mouse Listener will work because you have added a mouse event listener to your JTextArea.
textarea.addMouseMotionListener(new genDrag(textarea));//code to add MouseMotionListener.
but there is no FocusEvent is registerd with your JTextArea.
Thanks.
I think this is exactly what you need...
Just a hint: your class genFocus (prefer to follow code conventions: GenFocus) should implement FocusListener.
Related
I'm building a UI in Java. I want to create new components, like a JLabel, using a button. So every time I click a button it creates a new JLabel and places them in a specific JPanel.
Then, I want to be able to do some things with the labels based on how the user clicks on them.
With a left mouse press I want them to be able to drag the labels around the screen.
With a right mouse click I want to be open a new window where certain data can be entered, tied to the label (which might involve dynamically creating variables).
I've been toying around with some code I've Googled around for. I can get a button to create new labels in a panel, but when I try to get them to drag, I can only get one label at a time to appear, and after a second button press, moving the label isn't smooth, it jumps around.
I haven't even tried to implement any of the right mouse click things yet. If anyone can point me in the right direction, I'd appreciate it.
public class Testing {
JFrame frame;
//Launch the application.
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
Testing window = new Testing();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
//Create the application.
public Testing() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
JPanel area;
JButton btnCreate;
JLabel dragLabel;
frame = new JFrame();
frame.setBounds(100, 100, 511, 542);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setVisible(true);
area = new JPanel();
area.setBounds(10, 11, 477, 404);
frame.getContentPane().add(area);
area.setLayout(new BorderLayout());
btnCreate = new JButton("Create Label");
dragLabel = new JLabel("Drag Me");
btnCreate.setBounds(10, 425, 477, 67);
frame.getContentPane().add(btnCreate);
btnCreate.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
area.add(dragLabel);
area.revalidate();
DragListener drag = new DragListener();
dragLabel.addMouseListener(drag);
dragLabel.addMouseMotionListener(drag);
}
});
}
}
class DragListener extends MouseInputAdapter
{
Point location;
MouseEvent pressed;
public void mousePressed(MouseEvent me) {
pressed = me;
}
public void mouseDragged(MouseEvent me)
{
if(SwingUtilities.isLeftMouseButton(me)){
Component component = me.getComponent();
location = component.getLocation(location);
int x = location.x - pressed.getX() + me.getX();
int y = location.y - pressed.getY() + me.getY();
component.setLocation(x, y);
}
}
}
EDIT - I'm fairly certain the primary issue is in how the JLabel itself is being added to the panel. Every time the button is being pushed it's adding the same label over again, and this is gumming up the works.
Unfortunately, I'm not sure how to deal with that. I've done a bit more digging, and since dynamic variables aren't possible, I'm going to have to use an array or a map or some sort. With that, it appears I can declare arrays of components. Would something like that be necessary for my purposes?
Really odd stuff in your code. I don't want to go everything, and I'm not an expert by any stretch of the imagination, but I tried to remove redundant or contradictory stuff. I suspect a part of what you did was just copy pasting bits without really fitting them into the code.
Anyway, you needed to create the label inside the listener, so that it creates a new one everytime you click. Otherwise you only ever create one label and just reuse the same everytime.
I implemented a dialog on right click to enter the label name, don't know what you wanted to do but at least it detects right clicks.
Also in general it's easier to use layout managers instead of hardcoding everything. Here you had a borderlayout but were ignoring it.
class Main {
//Launch the application.
public static void main(String[] args) {
DrageableLabel window = new DrageableLabel();
}
}
public class DrageableLabel {
public DrageableLabel() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
JFrame frame = new JFrame();
Container area = frame.getContentPane();
area.setLayout(new BorderLayout());
JButton btnCreate = new JButton("Create Label");
btnCreate.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
/*
This is where you create your new window
for now I've added a dialog that takes a string parameter and creates a label with that string
I moved the method code to create a new drageable label outside the actionlistener to make it less confusing and reuseable
Either build w-e you want directly in here
or call a method that does it (which I prefer)
*/
String string = JOptionPane.showInputDialog(frame, "Enter your message", "Messages", JOptionPane.CANCEL_OPTION);
addDrageableLabel(string, area);
} else if (SwingUtilities.isLeftMouseButton(e)) {
addDrageableLabel("Drag me", area);
}
}
});
area.add(btnCreate, BorderLayout.SOUTH);
frame.setBounds(100, 100, 511, 542);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
// This is the method that creates and adds a drageable label
public void addDrageableLabel(String labelName, Container container) {
JLabel dragLabel = new JLabel(labelName);
container.add(dragLabel, BorderLayout.CENTER);
container.validate();
DragListener drag = new DragListener();
dragLabel.addMouseListener(drag);
dragLabel.addMouseMotionListener(drag);
}
}
class DragListener extends MouseInputAdapter {
Point location;
MouseEvent pressed;
#Override
public void mousePressed(MouseEvent me) {
pressed = me;
}
#Override
public void mouseDragged(MouseEvent me) {
Component component = me.getComponent();
location = component.getLocation(location);
int x = location.x - pressed.getX() + me.getX();
int y = location.y - pressed.getY() + me.getY();
component.setLocation(x, y);
}
}
Does JPanel have a doClick() equivalent? I need to call it where the panel is created? I tried calling that and it suggested i casted the panel to an abstract button but then it was impossible to do so.
No, JPanel does not have a doClick() but you can always write your own. Here is an example.
private void initPanel() {
JPanel panel = new JPanel();
panel.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
doClick();
}
});
doClick();
}
private void doClick() {
// handle the click
}
I have Java Swing application ToolTipMouseTest
The critical line is label.setToolTipText("label" + i);. Once it is commented out very click on a label produces 2 mousePressed in console. With this line enabled click on labels would produce nothing.
Is this expected behaviour or a bug? My goal is to show tooltips without disabling MouseListener from working.
Almost SSCCE, but without imports:
public class ToolTipMouseTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new ToolTipMouseTest();
}
});
}
public ToolTipMouseTest() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JLayeredPane lpane = new JLayeredPane() {
#Override
public Dimension getPreferredSize() {
return new Dimension(600,400);
}
};
MouseAdapter1 mouseAdapter1 = new MouseAdapter1();
lpane.addMouseListener(mouseAdapter1);
frame.add(lpane);
JPanel panel1 = new JPanel();
panel1.setSize(new Dimension(600, 400));
panel1.setOpaque(false);
lpane.add(panel1, JLayeredPane.PALETTE_LAYER);
JPanel panel2 = new JPanel();
for (int i = 0; i < 5; i++) {
JLabel label = new JLabel("Label " + i);
panel2.add(label);
label.setToolTipText("label" + i); //HERE!!
}
JScrollPane spane = new JScrollPane(panel2) {
private static final long serialVersionUID = 1L;
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 200);
}
};
MouseAdapter2 mouseAdapter2 = new MouseAdapter2();
spane.addMouseListener(mouseAdapter2);
panel1.add(spane);
frame.pack();
frame.setVisible(true);
}
private class MouseAdapter1 extends MouseAdapter {
#Override
public void mousePressed (MouseEvent me) {
System.out.println("1 mousePressed");
}
}
private class MouseAdapter2 extends MouseAdapter {
#Override
public void mousePressed (MouseEvent me) {
System.out.println("2 mousePressed");
}
}
}
It is working as intended. Let me explain why.
When a component has no listener, the mouse events will be propagated.
When any component has at least one MouseListener set on it - it will consume any mouse enter/exit/click/press/release events from going down in the components hierarchy.
It's the same for any listener such as MouseMotionListener with
mouse dragged/moved.
When you are adding a tooltip to a component (JLabel in your case), the component automatically receive a new MouseListener and a MouseMotionListener from ToolTipManager. The registerComponent method from ToolTipManager class do this (it's invoked by setToolTipText) :
public void registerComponent(JComponent component) {
component.removeMouseListener(this);
component.addMouseListener(this);
component.removeMouseMotionListener(moveBeforeEnterListener);
component.addMouseMotionListener(moveBeforeEnterListener);
component.removeKeyListener(accessibilityKeyListener);
component.addKeyListener(accessibilityKeyListener);
}
In your case - JLabels are consuming mouse events and mouse motion events, and as such prevents from propagating the events to the JLayeredPane because ToolTipManager listener added itself when the tooltip is set (setToolTipText) on the component.
In order to work around this, register a listener that will pass events down. You can add that listener to every component with a tooltip that should pass mouse events down (e.g to a JLayeredPane, a JScrollPane, etc).
Here is a small example of how that could be done:
var destinationComponent = // the JLayeredPane, JScrollPane, etc with mouse listeners
componentWithToolTip.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent event) {
destinationComponent.dispatchEvent(
SwingUtilities.convertMouseEvent(
event.getComponent(), // the component with the tooltip
event,
destinationComponent
)
);
}
// implements other mouse* handlers as required.
});
In that setup componentWithToolTip will have 2 listeners, the one from the ToolTipManager and the propagating one. When componentWithToolTip all its listeners will be triggered, and the propagating listener will dispatch to the declared destination component destinationComponent. So that destinationComponent listeners receive the mouse events as well.
I would like to use setAlwaysOnTop(boolean) in java.
I want to setAlwaysOnTop() when I click on a JButton and this JButton has its own actionListener
My Problem is I don't know how to set the JFrame on top at this situation, because it's not inside the constructor nor there is a method getFrame()
I tried creating a method inside the constructor but it does not work :S.
UPDATE:
private class optionAction implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource() == onTop) //onTop is a menuItem when I click it it should make the frame Always on top.
frame.setAlwaysOnTop(true); //This does not work of course just to demonstrat you what I want to do
}
}
The following code lines show you how it can be done with a direct implementation of ActionListener() assigned to a button declared inside the constructor. (You can also do this anywhere else in your class.)
class MyFrame extends JFrame {
public MyFrame() {
// ...
JButton button = new JButton("PRESS");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
setAlwaysOnTop(true);
// Alternatively use MyFrame.this.setAlwaysOnTop(true);
}
});
add(button);
// ...
}
}
An idea might be to pass a reference to your JFrame to the constructor of your implementation of the ActionListener.
Maybe something like this:
class MyActionListener implements ActionListener {
private JFrame jFrame;
public MyActionListener(JFrame jFrame) {
this.jFrame = jframe;
}
public void onClick(Event event) {
jFrame.setAlwaysOnTop(true);
}
}
create a boolean called ontop
boolean ontop = false;
jbutton.addActionListener(new ActionListener()) {
public void actionPerformed(ActionEvent e){
if (ontop) {
frame.setAlwaysOnTop(false);
ontop = false;
}
else {frame.setAlwaysOnTop(true); ontop = true}
});
The correct working code for this question is below:
private class optionAction implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource() == onTop) //onTop is a menuItem
setAlwaysOnTop(true); //This does not work of course just to demonstrate you what I want to do
}
}
The reason this is does not work because I was setting the setAlwaysOnTop on a JFrame object, which it doesn't exists in that class.
To set the setALwaysOnTop on a JFrame you have to remove the frame. and just add `setAlwaysOnTop()
Hey guys, I have a problem with a code that I've been writing.
I have a JFrame that contains two buttons. Each of these buttons has an action. The problem I'm having is with a JButton called "btnDone" that's supposed to get back to a previous screen. If I I keep pushing the button repeatedly, eventually the "btnDone" would stop doing the logic it's supposed to do. My code is as follows:
For the frame:
public class ItemLocatorPnl extends JPnl
{
private static final long serialVersionUID = 1L;
private Pnl pnl;
private JButton btnDone;
private JButton btnRefreshData;
public void setPnl(Pnl pnl) {
this.pnl = pnl;
}
public ItemLocatorPnl(Pnl pnl)
{
super();
this.pnl=pnl;
initialize();
}
private void initialize()
{
this.setSize(300, 200);
JPanel jContentPane = new JPanel();
jContentPane.setLayout(new MigLayout());
// (1) Remove window frame
setUndecorated(true);
// (3) Set background to white
jContentPane.setBackground(Color.white);
// (5) Add components to the JPnl's contentPane
POSLoggers.initLog.writeDebug("ItemLocator: Adding icon");
jContentPane.add(wmIconLabel, "align left");
POSLoggers.initLog.writeDebug("ItemLocator: Adding global controls");
jContentPane.add(createUpperPanel(), "align right, wrap");
POSLoggers.initLog.writeDebug("ItemLocator: Adding main panel");
jContentPane.add(pnl,"width 100%,height 100%, span 3");
// (6) Attach the content pane to the JPnl
this.setContentPane(jContentPane);
}
private JPanel createUpperPanel()
{
JPanel upperPanel=new JPanel();
MigLayout mig = new MigLayout("align right", "", "");
upperPanel.setLayout(mig);
upperPanel.setBackground(Color.WHITE);
// Create the Done button
btnDone= GraphicalUtilities.getPOSButton("<html><center>Done</center></html>");
btnDone.addActionListener(new ButtonListener());
// Create the Refresh Data button
btnRefreshData = GraphicalUtilities.getPOSButton("<html><center>Refresh<br>Data</center></html>");
btnRefreshData.addActionListener(new ButtonListener());
//Addiing buttons to the Panel
upperPanel.add(btnRefreshData, "width 100:170:200, height 100!");
upperPanel.add(btnDone, "width 100:170:200, height 100!");
return upperPanel;
}
public class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == btnRefreshData) {
Actual.refreshData();
} else if (e.getSource() == btnDone) {
Actual.backToMainScreen();
}
}
catch (Exception ex)
{
}
}
}
}
This is the method that the btnDone button calls upon clicking:
public static void backToMainScreen()
{
frame.setVisible(false);
frame.dispose();
}
This is the code that displays the JFrame:
public static void displayItemLocatorFrame()
{
pnl = new Pnl();
frame = new Frame(pnl);
frame.setVisible(true);
pnl.getSearchCriteria().requestFocus();
}
Please note that the "frame" object is static, and all of my methods are static, and they exist in a static class called Actual.
So in short, I just want to make sure that no matter how many times a user clicks on the button, and no matter how fast the clicks were, the frame should act normally.
Any suggestions? (I tried synchronizing my methods with no luck..)
I would generally prefer to use an Action for what you're trying to do.
So your code might look like this:
btnDone = new JButton(new CloseFrameAction());
...
private class CloseFrameAction extends AbstractAction
{
public CloseFrameAction()
{
super("Done");
}
public void actionPerformed(ActionEvent e)
{
frame.dispose();
setEnabled(false);
}
}
Notice the setEnabled(false) line - this should disable the button and prevent the user clicking on it again. Obviously I don't know what your exact requirements are but this is the general approach I would take.
The problem was with using a static panel that was instantiated with the click of the button each time. Removing "static" has finally fixed my problem! Thanks everyone for the help.