Jung, graph with gui/jframe as node - java

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.

Related

How can I update JPanel continuously?

I've got a slight problem, I'm writing a gps tracking app to track several objects at once. The data comes in over a serial interface, this is coming in fine from what I can tell. The issue is that I need to continually update the JPanel where the map is created and displayed.
public JPanel mapDisplay(){
JPanel mapPanel = new JPanel();
mapPanel.setSize(560, 540);
Coordinate start = new Coordinate (-34.9286, 138.6);
trackMap.addMapMarker(new MapMarkerDot(1Lat, 1Lon));
trackMap.setDisplayPosition(start,8);
System.out.println(1Lat);
mapPanel.add(trackMap);
mapPanel.setVisible(true);
return mapPanel;
}
This is what I have and it's happy to display the point once but won't update. If I print out the 1Lat variable in the serial method it continually prints, however it only does it once here.
A lot of the answers I've found refer to setting markers by arrays, however that won't work in this case as the objects I'm tracking could be anywhere.
Any help would be greatly appreciated :)
Is it possible to use a worker thread and not use an ArrayList? I would run the risk of missing data if I do.
Not necessarily. In a SwingWorker, your implementation of the doInBackground() method can publish() results as they become available. Note in particular that "Results from multiple invocations of publish() are often accumulated for a single invocation of process()." In your process(), simply loop through the List<Coordinate>, update the route and repaint() the map.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.SwingWorker;
import org.openstreetmap.gui.jmapviewer.Coordinate;
import org.openstreetmap.gui.jmapviewer.JMapViewer;
import org.openstreetmap.gui.jmapviewer.MapPolygonImpl;
/**
* #see http://stackoverflow.com/a/37193636/230513
*/
public class MapWorkerTest {
private final List<Coordinate> route = new ArrayList<>();
private void display() {
JFrame f = new JFrame("MapWorker");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMapViewer map = new JMapViewer() {
#Override
public Dimension getPreferredSize() {
return new Dimension(640, 480);
}
#Override
public String getToolTipText(MouseEvent e) {
Coordinate c = (Coordinate) getPosition(e.getX(), e.getY());
return c.getLat() + " " + c.getLon();
}
};
map.setToolTipText("");
Coordinate start = new Coordinate(-34.9286, 138.6);
route.add(start);
MapPolygonImpl poly = new MapPolygonImpl(route);
poly.setColor(Color.blue);
map.addMapPolygon(poly);
map.setDisplayPosition(start, 10);
f.add(map);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
new MapWorker(map, start).execute();
}
private class MapWorker extends SwingWorker<Void, Coordinate> {
private final JMapViewer map;
private Coordinate last;
public MapWorker(JMapViewer map, Coordinate start) {
this.map = map;
this.last = start;
}
#Override
protected Void doInBackground() throws Exception {
while (!isCancelled()) {
last = new Coordinate(last.getLat() + 0.0025, last.getLon() + 0.01);
publish(last);
Thread.sleep(1000);
}
return null;
}
#Override
protected void process(List<Coordinate> chunks) {
for (Coordinate c : chunks) {
route.add(c);
}
map.repaint();
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new MapWorkerTest()::display);
}
}
Multiple route management left as a exercise.

Get XY position of caret position in JTextArea

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 ;)

Java word game with GUI. What is wrong with my code?

I created a simple Java GUI word game.
It starts of with a head, and the user tries to guess the word. If it is right, the system prints out correct. If it is wrong, it will print out "wrong" and draw a body. The top text box is the hidden word (not really hidden) and the bottom is where you insert your guess.
What is wrong with this program is, the body is not painted after the user guess the wrong word.
The first class:
package hangman;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class NewClass extends JPanel {
int lineA, lineB, lineC, LineD;
int guess;
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.ORANGE);
//head
g.drawOval(110, 10, 25, 25);
g.drawLine(lineA, lineB, lineC, LineD);// (ideal) 125, 40, 120, 100
}
public void newPaint(int a, int b, int c, int d) {
lineA = a;
lineB = b;
lineC = c;
LineD = d;
super.revalidate();
}
}
The second class:
package hangman;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class NewClass1 extends JFrame {
private JTextField answerBox, hiddenAnswer;
NewClass nc = new NewClass();
public NewClass1() {
hiddenAnswer = new JTextField();
hiddenAnswer.setText("hat");// this is the word for the hangman
answerBox = new JTextField("put you answer in here");
answerBox.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
if (event.getActionCommand().equals(hiddenAnswer.getText())) {
System.out.println("you got it right");
} else {
System.out.println("sorry you got it wrong");
nc.newPaint(125, 120, 40, 100);
}
}
});
add(BorderLayout.NORTH, hiddenAnswer);
add(BorderLayout.SOUTH, answerBox);
}
}
entry point
NewClass1 ncc = new NewClass1();
NewClass nc = new NewClass();
ncc.add(BorderLayout.CENTER,nc);
ncc.setVisible(true);
ncc.setSize(300,300);
ncc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Use your newPaint API in the event, instead of directly setting the fields of NewClass.
if (event.getActionCommand().equals(hiddenAnswer.getText()))
{
System.out.println("you got it right");
}
else
{
System.out.println("sorry you got it wrong");
nc.newPaint(125, 120, 40, 100);
}
Edit: In the NewClass1, replace the repaint() with revalidate(). Here's the reference.
Edit2: In your case, it is best that you don't create the NewClass panel outside of NewClass1. In the NewClass1 constructor you can do this:
nc = new NewClass();
add(BorderLayout.CENTER, nc);
before you add the hiddenAnswer, and then you can remove the NewClass from the entry point.
The thing is, you weren't working on the instance of NewClass that you've created in the entry point, you were working with a random new NewClass.

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

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