Get XY position of caret position in JTextArea - java

Actually, i have already ask this question in here. But, i'm making mistake. I haven't already get the solution.
First, at the question before, i can get Rectangle with
Rectangle rectangle = textArea.modelToView( textArea.getCaretPostion() );
I'm also get X and Y position.
I'm creating a editor that can add new Text Area each i press Enter key. XY position with code above always give same return in every Text Area. Look my code.
import java.awt.Container;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;
public class forquestion extends JFrame {
Container textAreaBox;
LinkedList<JTextComponent> textArea;
int nameTA;
public forquestion() {
int nameTA = 0;
textArea = new LinkedList<>();
textAreaBox = Box.createVerticalBox();
textAreaBox.add(Box.createVerticalGlue());
addLine();
this.add(textAreaBox);
this.setVisible(true);
}
public static void main(String[] args) {
forquestion app = new forquestion();
app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
public void addLine () {
JTextComponent temp_ta = createTextComponent();
textArea.add(temp_ta);
textAreaBox.add(textArea.getLast());
textAreaBox.add(Box.createVerticalGlue());
}
protected JTextComponent createTextComponent() {
JTextArea ta = new JTextArea("test");
/*if (count%2==0)
ta.setForeground(Color.red);
else
ta.setForeground(Color.GREEN);*/
ta.setFont(new Font("Courier New",Font.PLAIN,16));
ta.setLineWrap(true);
ta.setWrapStyleWord(true);
ta.setName(Integer.toString(nameTA));
nameTA+=1;
basicKey("ENTER", enter, ta);
ta.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent ev) {
try {
taMousePressed(ev);
} catch (BadLocationException ex) {
Logger.getLogger(forquestion.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
return ta;
}
public void basicKey(String s, Action a, JTextArea ta) {
ta.getInputMap().put(KeyStroke.getKeyStroke(s), s);
ta.getActionMap().put(s, a);
}
Action enter = new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
addLine();
}
};
private void taMousePressed(java.awt.event.MouseEvent ev) throws BadLocationException {
int now_focus = Integer.parseInt(ev.getComponent().getName());
int _caret;
_caret = textArea.get(now_focus).getCaretPosition();
Rectangle rectangle = textArea.get(now_focus).modelToView(_caret);
double x = rectangle.getX();
//int xc = textArea.get(now_focus).getLocation().x;
double y = rectangle.getY();
//int yc = textArea.get(now_focus).getLocation().y;
//double h = rectangle.getHeight();
//double w = rectangle.getWidth();
System.out.println(x);
System.out.println(y);
//System.out.println(xc);
//System.out.println(yc);
//System.out.println(h);
//System.out.println(w);
System.out.println("");
}
}
My code will print XY position each time you press a Text Area. But, the display always same in every text area. (Try to make many Text Area and give some text) Btw, it just simple code. You need change the window frame size for update the new text area after you press enter key..hahaha.
So, my question is: How can i get the XY position of caret (text cursor) in any Text Area. I want to display JPopmenu there. :)
I hope this question clear for you. Thx before.

The Rectangle reported back is relative to the text area, where it's 0x0 position is the top, left corner of the component.
If you use something like...
popup.show(textArea.get(now_focus), rectangle.x, rectangle.y + rectangle.height);
Where popup is a JPopupMenu, it will make the required translations to the screen itself.
Now. Having said that. Personally, I would prefer to use the popup API support provided by Swing. This is going to mean needing to create a custom component that extends from JTextArea to achieve it...
public class MyPopupTextArea extends JTextArea {
/*...*/
public Point getPopupLocation(MouseEvent evt) {
Rectangle rectangle = textArea.get(now_focus).modelToView(_caret);
Point p = rectangle.getLoction();
p.y += rectangle.height;
return p;
}
}
Then, based on your needs, you can use setComponentPopup to provide a shared instance of the JPopupMenu or, if required, create a custom JPopupMenu for each instance of the custom editor and use setComponentPopup as you see fit...no messing about with mouse listeners ;)

Related

New JFrame is opening in disabled state when opened on a button click

I am stuck with a very unusual situation. I have a class "ScreenSizeSelector" which has a method 'getSelectedScreenSize'. The method's work is to create a UI, user drags the UI and method return back size of window.
Now I am calling the method of class in following ways:
A simple class (non GUI)
On the button click from a JFrame
In the first case, it is working perfectly fine (i.e. size selector window opens, user drags it, resize it and it is giving back window coordinates) but in second case, window opens but in disabled mode, user is not able to perform any operation on the window, not even able to close the window.
Here is the code I am using
ScreenSizeSelector class :
package screenrecorder;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
class ScreenSizeSelector {
private JFrame sizeSelectorWindow;
private JButton btnOk;
private Border emptyBorder;
private Rectangle screenArea = null;
private static Object lock = new Object();
public Rectangle getSelectedScreenSize(){
screenSizeSelectorUI();
Thread t = new Thread() {
public void run() {
synchronized(lock) {
while (sizeSelectorWindow.isVisible())
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t.start();
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
return screenArea;
}
public void screenSizeSelectorUI() {
emptyBorder = BorderFactory.createEmptyBorder();
sizeSelectorWindow = new JFrame("Select screen area");
btnOk = new JButton("Start");
sizeSelectorWindow.setUndecorated(true);
sizeSelectorWindow.getRootPane().setWindowDecorationStyle(3);
sizeSelectorWindow.setBackground( new Color(0, 0, 0, 0) );
sizeSelectorWindow.setSize(400,400);
sizeSelectorWindow.addWindowListener(new WindowEventHandler());
sizeSelectorWindow.setAlwaysOnTop(true);
sizeSelectorWindow.setLocationRelativeTo(null);
btnOk.setToolTipText("Click this button after deciding the screen area");
btnOk.addActionListener(new ButtonEventHandler());
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
buttonPanel.setBackground(new Color(0,0,0,0));
buttonPanel.add(btnOk);
sizeSelectorWindow.add(buttonPanel,BorderLayout.SOUTH);
sizeSelectorWindow.setVisible(true);
sizeSelectorWindow.setEnabled(true);
}
class ButtonEventHandler implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
int x = (int)(sizeSelectorWindow.getBounds().getX());
int y = (int) (sizeSelectorWindow.getBounds().getY());
int width = sizeSelectorWindow.getWidth();
int height = sizeSelectorWindow.getHeight();
screenArea = new Rectangle(x,y,width,height);
sizeSelectorWindow.dispatchEvent(new WindowEvent(sizeSelectorWindow, WindowEvent.WINDOW_CLOSING));
}
}
class WindowEventHandler implements WindowListener{
#Override
public void windowOpened(WindowEvent e) {
}
#Override
public void windowClosing(WindowEvent e) {
synchronized (lock) {
sizeSelectorWindow.setVisible(false);
lock.notify();
}
}
#Override
public void windowClosed(WindowEvent e) {
}
#Override
public void windowIconified(WindowEvent e) {
sizeSelectorWindow.setState(JFrame.NORMAL);
Toolkit.getDefaultToolkit().beep();
}
#Override
public void windowDeiconified(WindowEvent e) {}
#Override
public void windowActivated(WindowEvent e) {}
#Override
public void windowDeactivated(WindowEvent e) {}
}
}
Test1 class :
package screenrecorder;
import java.awt.Rectangle;
public class Test1{
public static void main(String[] args){
System.out.println(new ScreenSizeSelector().getSelectedScreenSize());
}
}
Test2 class :
package screenrecorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Test2 extends JFrame{
public Test2(){
JButton btn = new JButton("Click ME");
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println(new ScreenSizeSelector().getSelectedScreenSize());
}
});
getContentPane().add(btn);
setSize(100,100);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new Test2();
}
}
Any help is appreciated.
when you click the button, the action listener waits for the getSelectedScreenSize() function to return. and the getSelectedScreenSize() function is waiting for the second window created by screenSelectorUI() to be invisible. screenSelectorUI() does create a second window, but you set the color like this:
sizeSelectorWindow.setBackground( new Color(0, 0, 0, 0) );
if you look at the color constructor javadocs:
public Color(int r,
int g,
int b,
int a)
Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0 - 255).
Parameters:
r - the red component
g - the green component
b - the blue component
a - the alpha component
you set the alpha value to 0, making it completely invisible. (alpha value is transparency) also, this second window is undecorated and does not exit on close, so you don't even know it's there at all.
what I don't get is how test1 worked at all.
side note: when I try test 1 on mac it only shows the button and all I can do is click it. the button will disappear, but the application will still be running.
This is basically a total guess, but a lot of the swing components make requests to the operating system, not commands. sort of like saying, "hey can I please be resized to 400, 400?" the OS doesn't technically have to do what you say. and I was reading How does Java handle multithreading? which says that multithreading really depends on the OS. I have a feeling it just messes up somewhere when screenSelectorUI() is called by itself, but somehow gets it right when it's inside the thread of some button.

Jung, graph with gui/jframe as node

I am playing with the JUNG library and I want to create a graph, whose nodes are some gui(jframe presumably) items.
I want each of the nodes to have a few buttons, one text field, menu, etc.
And my question is: is this possible?
If yes, I have two other questions:
1. How should I approach it(I am new to Java and I am not familiar with the best practices )
2. What interface should I use(I am looking at .visualisation.decorators.* currenlty).
Thank you in advance.
OK, eventually I succeeded creating a graph of JPanels and JFrames. I will continue with JPanels. And here is the result:
But now I face some other problems:
The frames can be moved, but only if they are picked at the upper left corner. How can I make the whole JPanel area pickable?
I need the JPanels to be accessible, e.g. I should be able to press the button, write some text in a textbox(this is not implemented yet) and probably resize the JPanel. Currently the JPanels are "inactive/no focus" for some reason. I tried the enable method, no success. Tried some "focus" methods, again failed.
Any suggestions on the above two questions?
Here is how my code looks like:
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.geom.Point2D;
import javax.swing.BorderFactory;
import javax.swing.CellRendererPane;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import edu.uci.ics.jung.algorithms.layout.CircleLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.graph.DirectedSparseGraph;
import edu.uci.ics.jung.visualization.RenderContext;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.control.ModalGraphMouse;
import edu.uci.ics.jung.visualization.picking.PickedState;
import edu.uci.ics.jung.visualization.renderers.Renderer;
import edu.uci.ics.jung.visualization.transform.shape.GraphicsDecorator;
public class GraphPanel extends Container
{
static final long serialVersionUID = 420001L;
DirectedSparseGraph<Number, Number> graph = null;
VisualizationViewer<Number, Number> vv = null;
PickedState<Number> pickedState = null;
public GraphPanel(Number[][] nodes_list)
{
try
{
graph = new DirectedSparseGraph<Number, Number>();
construct_graph(nodes_list);
vv = new VisualizationViewer<Number, Number>
(new CircleLayout<Number, Number>(graph), new Dimension(400, 400));
vv.getRenderer().setVertexRenderer(new MyRenderer());
// The vertex pick listener
pickedState = vv.getPickedVertexState();
pickedState.addItemListener(new ItemListener()
{
#Override
public void itemStateChanged(ItemEvent e)
{
Object subject = e.getItem();
if (subject instanceof Number)
{
Number vertex = (Number) subject;
if (pickedState.isPicked(vertex))
{
System.out.println("Vertex " + vertex + " is now selected");
}
else
{
System.out.println("Vertex " + vertex + " no longer selected");
}
}
}
});
// The following code adds capability for mouse picking of
// vertices/edges. Vertices can even be moved!
final DefaultModalGraphMouse<Number, Number> graphMouse = new DefaultModalGraphMouse<Number, Number>();
vv.setGraphMouse(graphMouse);
graphMouse.setMode(ModalGraphMouse.Mode.PICKING);
}
catch (Exception e)
{
System.err.println("Failed to construct graph!\n");
System.err.println("Caught Exception: " + e.getMessage());
}
}
/*Attach the graph panel/container to a specified frame*/
public void attach_to_frame(JFrame frame)
{
frame.setContentPane(vv);
}
/*This one should be reimplemented*/
private void construct_graph(Number[][] nodes_list)
{
int i = 0;
/*add the nodes*/
for(i=0; i<nodes_list.length; i++)
{
graph.addVertex(i);
graph.addEdge(nodes_list[i][0], nodes_list[i][1], nodes_list[i][2]);
}
}
/*re-implement the render functionality to work with internal frames(JInternalFrame)*/
static class MyRenderer extends JPanel implements Renderer.Vertex<Number, Number>
{
static final long serialVersionUID = 420000L;
#Override
public void paintVertex(RenderContext<Number, Number> rc,
Layout<Number, Number> layout, Number vertex)
{
try
{
GraphicsDecorator graphicsContext = rc.getGraphicsContext();
Point2D center = layout.transform(vertex);
Dimension size = new Dimension(100, 80);
System.out.printf("Vertex[%d] X = %d Y = %d: Running paintVertex()\n", vertex, (int)center.getX(), (int)center.getY());
JPanel sv = new JPanel();
sv.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
sv.setBackground(Color.GREEN);
sv.setPreferredSize(size);
sv.add(new JButton("Button1"));
//OK
graphicsContext.draw(sv, rc.getRendererPane(), (int)center.getX(),
(int)center.getY(), size.width, size.height, true);
}
catch (Exception e)
{
System.err.println("Failed to render images!\n");
System.err.println("Caught Exception: " + e.getMessage());
}
}
}
public static void main(String[] args)
{
/*Create the window*/
JFrame frame = new JFrame("BLABLA");
Number[][] list = {{0, 1, 3}, {1, 3, 1}, {2, 2, 3}, {3, 2, 0}};
GraphPanel g = new GraphPanel(list);
g.attach_to_frame(frame);
frame.getContentPane().setPreferredSize(new Dimension(640, 480));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}/*2*/
Yes it is possible (I think). I would recommend you change your VertexLabeler to one that you implement yourself. Something in the lines of...
VisualizationImageServer<V, E> vv = new ...;
vv.getRenderContext().setVertexLabelRenderer(new MyVertexRenderer());
class MyVertexRenderer extends JFrame implements VertexLabelRenderer {}
But I'm not sure how it is going to work in terms of positioning it later. You might need to change the VertexShapeTransofrmer aswell with vv.getRenderContext().setVertexShapeTransformer().
I must say, I've spent a lot of time trying to get Jung to render a graph nicely like graphviz with very little success.

Change JList item background color on hover

I'm trying to change the background color of a JList cell when it is being hovered over, but I'm not sure how to do it. Here is what I currently have:
package cats.youtube.gui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.util.LinkedList;
import javax.swing.AbstractListModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import cats.youtube.search.YoutubeSearchResult;
public class SearchResultsList extends JPanel{
private class Renderer extends DefaultListCellRenderer{
public Component getListCellRendererComponent(JList list, Object value, int index, boolean selected, boolean focused){
final JTextArea area = new JTextArea(model.get(index).toString());
area.setBorder(new EmptyBorder(5, 0, 5, 0));
area.setForeground(selected || focused ? Color.WHITE : Color.BLACK);
area.setBackground(selected || focused ? Color.RED : Color.WHITE);
return area;
}
}
public class Model extends AbstractListModel<String>{
private LinkedList<YoutubeSearchResult> results;
private Object lock;
private Model(){
results = new LinkedList<YoutubeSearchResult>();
lock = new Object();
}
public int getSize(){
return results.size();
}
public String getElementAt(final int i){
return results.get(i).toString();
}
public YoutubeSearchResult get(final int i){
return results.get(i);
}
public void add(final YoutubeSearchResult r){
synchronized(lock){
results.add(r);
fireContentsChanged(this, 0, getSize());
try{
lock.wait(500L);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
public void remove(final YoutubeSearchResult r){
results.remove(r);
fireContentsChanged(this, 0, getSize());
}
public void removeAll(){
results.clear();
fireContentsChanged(this, 0, getSize());
}
}
private JList<String> list;
private JScrollPane scroll;
private Model model;
private Renderer renderer;
public SearchResultsList(){
super(new BorderLayout());
list = new JList<String>(){
public void processMouseMotionEvent(final MouseEvent e){
super.processMouseMotionEvent(e);
final int i = locationToIndex(e.getPoint());
if(i > -1){
final Rectangle bounds = getCellBounds(i, i+1);
if(bounds.contains(e.getPoint())){
// <--------- here is line 95
}
}
}
};
list.setModel(model = new Model());
list.setCellRenderer(renderer = new Renderer());
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
add(scroll = new JScrollPane(list), BorderLayout.CENTER);
}
public Model getModel(){
return model;
}
}
The problem is on line 95; I don't know what to put there. I tried multiple approaches, one being calling the getListCellRendererComponent method through my renderer and changing the background like that, but that didn't work.
The hovering portion does work (it does get the correct indexes) I just don't know what to put on line 95. If anyone could help me, it would be very much appreciated.
I, personally, would use a MouseMotionListener over overriding the processMouseMotionEvent, but that's just me.
You need some way to tell the renderer which rows are "highlighted", the two immediate ways I can think of achieving this is to ...
Create a custom JList which has methods to set/get the highlighted row. You would then need to cast to this implementation and interrogate the appropriate method, taking action as required.
Provide a method within the list data that mark the row as highlighted or not. This would allow you to interrogate the data directly.
The advantage of the first approach is that it isolates the responsibility to the view, where it really belongs. It does have the disadvantage of meaning you need to create a custom JList. It might be easier to use the getClientProperty and putClientProperty methods instead, this would mean you wouldn't need a custom implementation nor cast the list in the renderer, but is has the disadvantage of not being obvious to other developers.
The second approach mixes display and data information together, not something I would encourage as you really want to keep this kind of stuff separated ;)
Here's how I did it (solution here: http://objectmix.com/java/73071-highlight-itemin-jlist-mouseenter-mouse-over.html, second message):
private int mHoveredJListIndex = -1;
...
mList.addMouseMotionListener(new MouseAdapter() {
public void mouseMoved(MouseEvent me) {
Point p = new Point(me.getX(),me.getY());
int index = mList.locationToIndex(p);
if (index != mHoveredJListIndex) {
mHoveredJListIndex = index;
mList.repaint();
}
}
});
And in your renderer:
public class CellRenderer extends JComponent implements ListCellRenderer
{
#Override
public Component getListCellRendererComponent(JList aList, Object aValue, int aIndex, boolean aIsSelected, boolean aCellHasFocus)
{
Color backgroundColor = mHoveredJListIndex == aIndex ? Color.gray : Color.white;
JPanel pane = new JPanel(new BorderLayout()); // add contents here
pane.setBackground(backgroundColor);
return pane;
}
}
AFAIK good RolloverSupportTest / Hightlighter is implemented
Substance L&F has hightlighter
RolloverSupportTest by #aephyr

How to change the mouse pointer to finger pointer in swing?

In html when we create a hyperlink and point over it ,then it automatically changes to a finger pointer.
So I was wondering can we achieve the same in java swings.
Suppose I have a label on clicking which a new form pops-up.But I want that when the user points over the label it should change to finger pointer,showing that something will pop-up if its clicked.In this way we can differentiate that label with normal labels on the form i guess :).
But how to do something like this?
You can set cursor of JLabel to Cursor.HAND_CURSOR using below code :
JLabel label = new JLabel("https://stackoverflow.com");
label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
go to the properties of the button or the label and scroll down you will find a column of cursor you can change to hand or whatever you want
As said you'd want to call the setCursor() method on the JLabel and set it to Cursor.Hand_CURSOR to further this you can also underline the text to make it an HTML look alike link if you want :):
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URI;
import javax.swing.JLabel;
/**
*
* #author ludovicianul
*/
public class URLLabel extends JLabel {
private String url;
public URLLabel() {
this("","");
}
public URLLabel(String label, String url) {
super(label);
this.url = url;
setForeground(Color.BLUE.darker());
setCursor(
new Cursor(Cursor.HAND_CURSOR));
addMouseListener(
new URLOpenAdapter());
}
public void setURL(String url) {
this.url = url;
}
//this is used to underline the text
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.blue);
Insets insets = getInsets();
int left = insets.left;
if (getIcon() != null) {
left += getIcon().getIconWidth() + getIconTextGap();
}
g.drawLine(left, getHeight() - 1 - insets.bottom, (int) getPreferredSize().getWidth()
- insets.right, getHeight() - 1 - insets.bottom);
}
private class URLOpenAdapter extends MouseAdapter {
#Override
public void mouseClicked(MouseEvent e) {
if (Desktop.isDesktopSupported()) {
try {
Desktop.getDesktop().browse(new URI(url));
} catch (Throwable t) {
//
}
}
}
}
}
Reference:
CREATING A URL JLABEL IN SWING
Hossein Mobasher
answer is good and mine answer is very late but i just want to add up you can also use it like this below.
JLabel label = new JLabel("http://stackoverflow.com");
label.setCursor(Cursor.HAND);
It will also work i have tried it in Java 1.8
ButtonName.SetCursor(new Cursor(12));
This one works definitely!

Listen to mouse clicks on whole system (not on a JFrame etc.)

I want to write a little code which react to a mouse click. But it seems the only way is to listen to clicks on Java components. A direct listener to all clicks would be great.
Is there actually a possibility to implement this in Java?
Thanks in advance!
Update:
Found out, that it would need a hook via JNI with some C coding.
More information on
http://www.jotschi.de/?p=90
Best regards,
fnst
It's perfectly possible if you are willing to use a third party library - JNativeHook
It provides these functionality using JNI which is otherwise not possible in pure java apps.
Don´t think so, because of the sandbox you´re running in.
some food for thought: use Point in Java to detect where the click occurs. http://download.oracle.com/javase/1.4.2/docs/api/java/awt/Point.html
admittedly, ive only used it once before for detecting rows in a table something like, but it seems the closest to what ive found answering your qn.:
public void mouseClicked(MouseEvent e) {
JTable target = (JTable)e.getSource();
//get the coordinates of the mouse click
Point p = e.getPoint();
//get the row index that contains that coordinate
row= target.rowAtPoint(p);
}
im sorry if this isn't what you're looking for, but otherwise, clicking on components is the only way to go. whats the alternative- clicking on containers? well that just doesn't make any sense does it?
As far as I know, there is not easy way to accomplish what you want. But what you want could be done.. Well for modern way to accomplish this task, I suggest you examine java.awt.Dialog show method..
package mouseclickevent;
import java.awt.AWTEvent;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.MenuComponent;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.lang.reflect.Method;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class FrmEvent extends JFrame {
public FrmEvent(){
JPanel panel = new JPanel();
getContentPane().add(panel);
JButton btn = new JButton("Test");
panel.add(btn);
panel.add(new JTextField("Test"));
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
FrmEvent frm = new FrmEvent();
frm.setBounds(300,300, 200, 200);
frm.show();
}
});
}
private void consumeEvents() {
try {
if (Thread.currentThread().getClass().getName().endsWith(
"EventDispatchThread")) {
EventQueue eq = null;
eq = Toolkit.getDefaultToolkit().getSystemEventQueue();
if (eq == null) {
return;
}
while (isVisible() || isShowing()) {
AWTEvent event = eq.getNextEvent();
Object src = event.getSource();
Class kActiveEvent= Class.forName("java.awt.ActiveEvent");
if (kActiveEvent != null) {
if (kActiveEvent.isInstance(event)) {
Method m;
Class types[] = {};
Object args[] = {};
m = kActiveEvent.getMethod("dispatch", types);
if (m != null) {
m.invoke(event, args);
continue;
}
}
}
dispatchEvent(src, event);
}
}
}
catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
private void dispatchEvent(Object src, AWTEvent event) {
if (event.getID()== MouseEvent.MOUSE_CLICKED) {
System.out.println("mouseClicked");
}
if (src instanceof Component) {
( (Component) src).dispatchEvent(event);
} else if (src instanceof MenuComponent) {
( (MenuComponent) src).dispatchEvent(event);
}
}
public void show(){
super.show();
consumeEvents();
}
public static void main(String[] args) {
FrmEvent frm = new FrmEvent();
frm.setBounds(300,300, 200, 200);
frm.show();
}
}

Categories

Resources