Results 1 to 20 of 25
Thread: travelling between frames
- 06-27-2010, 09:30 PM #1
travelling between frames
I am traversing b/w frames, say, Frame1, Frame2, Frame3.
I click the button on Frame1 to move to Frame2 and so on......
and return back to the previous frames upon pressing "esc" key.
Frame1::
Java Code:public class Frame1 extends javax.swing.JFrame { [COLOR="Red"] protected static Frame1 f1 = new Frame1(); protected static Frame2 f2;[/COLOR] public Frame1() { initComponents(); } private void initComponents() { jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButton1.setText("jButton1"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(101, 101, 101) .addComponent(jButton1) .addContainerGap(226, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(130, 130, 130) .addComponent(jButton1) .addContainerGap(147, Short.MAX_VALUE)) ); pack(); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { [COLOR="red"] f2 = new Frame2(); f2.setBounds(100, 100, 500, 500); f2.setVisible(true); setVisible(false);[/COLOR] } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { f1.setVisible(true); } }); } private javax.swing.JButton jButton1; }
Frame2::
Java Code:import java.awt.event.*; import java.awt.*; public class Frame2 extends javax.swing.JFrame { protected static Frame3 f3; [COLOR="Blue"] KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager(); [/COLOR] public Frame2() { initComponents(); manager.addKeyEventDispatcher(new KeyEventDispatcher() { @Override public boolean dispatchKeyEvent(KeyEvent e) { if (e.getID() == KeyEvent.KEY_PRESSED) { if (e.getKeyCode() == 27) { [COLOR="red"] Frame1.f1.setVisible(true); setVisible(false);[/COLOR] } } return false; } }); } private void initComponents() { jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButton1.setText("jButton1"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(124, 124, 124) .addComponent(jButton1) .addContainerGap(203, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(104, 104, 104) .addComponent(jButton1) .addContainerGap(173, Short.MAX_VALUE)) ); pack(); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { [COLOR="red"] f3 = new Frame3(); f3.setBounds(200, 200, 500, 500); f3.setVisible(true); setVisible(false);[/COLOR] } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Frame2().setVisible(true); } }); } private javax.swing.JButton jButton1; }
Frame3::
Java Code:import java.awt.event.*; import java.awt.*; public class Frame3 extends javax.swing.JFrame { [COLOR="blue"] KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManag[/COLOR]er(); public Frame3() { initComponents(); manager.addKeyEventDispatcher(new KeyEventDispatcher() { @Override public boolean dispatchKeyEvent(KeyEvent e) { if (e.getID() == KeyEvent.KEY_PRESSED) { if (e.getKeyCode() == 27) { [COLOR="red"] Frame1.f2.setVisible(true); setVisible(false); [/COLOR] } } return false; } }); } private void initComponents() { setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 400, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 300, Short.MAX_VALUE) ); pack(); } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Frame3().setVisible(true); } }); } }
the problem is that when i traverse from the Frame3 to Frame2.. both the frames ie., Frame1 and Frame2 come to visibility..
I guess there is some problem with the design... But I can't figure it out..
please help:confused:Last edited by Stephen Douglas; 06-28-2010 at 12:07 AM.
The Quieter you become the more you are able to hear !
- 06-27-2010, 09:48 PM #2
What should happen?problem is that when i traverse from the Frame3 to Frame2.. both the frames ie., Frame1 and Frame2 come to visibility..
Try debugging your code by overriding the setVisible() method in each frame and adding to it a trace to show who is setting it visible:
Java Code:@Override public void setVisible(boolean b) { super.setVisible(b); if(b) { try{throw new Exception("Who called");}catch(Exception x) {x.printStackTrace();}; } }Last edited by Norm; 06-27-2010 at 09:55 PM.
- 06-27-2010, 09:54 PM #3
I mean the order of traversal should be from Frame3 back to Frame2 and then from Frame2 to Frame1..
The Quieter you become the more you are able to hear !
- 06-27-2010, 10:02 PM #4
How do you control what the order of traversal should be in your code?
Does your code save the state so that it knows when to go to which frame?
Do some debugging by adding System.out.println() to the code to show where and when things are happening.Last edited by Norm; 06-27-2010 at 10:13 PM.
- 06-27-2010, 10:14 PM #5
Yeah.. In the above code when the Frame2 is entered from the Frame1...
The trick is that "set the Frame1 to visibility = false and when need to return back to this frame set its visibility = true"
I have highlighted in red.The Quieter you become the more you are able to hear !
- 06-27-2010, 10:34 PM #6
Have you done the debugging things I mentioned yet?
Look at the output and see what happens when!!!
- 06-27-2010, 10:42 PM #7
the visibility of Frame1 when traversing from Frame3 to Frame2 is not justified.. So I placed the code specified by you in code for Frame1.
The results are::
when traversing from frame3 to frame2
java.lang.Exception: Who called
at Frame1.setVisible(Frame1.java:77)
at Frame2$1.dispatchKeyEvent(Frame2.java:32)
I can't make anything out of it.. I am new bie to Java So please tolerate me.The Quieter you become the more you are able to hear !
- 06-27-2010, 11:15 PM #8
You're trying to write a complicated program and are having trouble. You need to understand how to debug. First step is to understand the control flow within the program. You can help understand by using println()s and stack traces.
You need to put the code in all three frames. Make the output from each unique so you can tell which is frame is creating it. Look at the what trace output is generated when you press the esc key in frame3.
- 06-27-2010, 11:34 PM #9
I guess I figured out... But I need a bit of your help..
The problem was that When I was traversing from Frame2 to Frame3.. I was setting the visibility of Frame2 = false... that means the Frame2 was present but not visible.. So when I tried to return from Frame3 by pressing "Esc" ...... Frame2 also responded along with the Frame3.
Here I need a bit of your help.. Can you explain me how to make the Frame2 not to respond against "Esc" when it is set visibility = false. and only respond when visibility = true for it.?The Quieter you become the more you are able to hear !
- 06-27-2010, 11:38 PM #10
You have called manager.addKeyEventDispatcher( more than once. Each call
So how many dispatchers are on the list?Adds a KeyEventDispatcher to this KeyboardFocusManager's dispatcher chain
Look at the API doc. There is also a way to remove dispatchers.
- 06-28-2010, 12:10 AM #11
But as I specify in Blue in the above codes...
I have defined manager in every code separetely.. so how could list on dispatchers could be added to a single manager..
I'm sori I didn;t get it. please elaborate.thnxThe Quieter you become the more you are able to hear !
- 06-28-2010, 12:14 AM #12
Did you see the proof from the debug trace's? There you'll see that there were two calls made: one to each frame.
There is only one JVM executing. The static method: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManage r();
Must return the same object. To verify, add code to println("manager="+ manager);
following both places you call the get... method.
- 06-28-2010, 12:23 AM #13
The output is this ::
manager:::java.awt.DefaultKeyboardFocusManager@a40 1c2
What does that mean???
How can I get around the problem?The Quieter you become the more you are able to hear !
- 06-28-2010, 12:52 AM #14
If both of the println()s show the same thing, it means that there is one KeyboardFocusManager used by both classes. If there is only one manager, then there is only one list of dispatchers. So when you type ESC both dispatchers are called. This is shown by the debug output I had you put in the code.
-
I have to admit to not liking your use of static variables here and am thinking that there have to be better solutions that don't use these. Myself, I'd try to avoid opening and closing windows as I find it distracting, and per my experience with most commercial programs out there, I don't find this to be a common behavior of GUI applications. Having said that, if you want to do this for academic reasons, consider other ways to get references to a parent window, for instance it can be done by passing a reference.
A quick and dirty example:
Java Code:import java.awt.Component; import java.awt.Dimension; import java.awt.Point; import java.awt.Window; import java.awt.event.*; import java.util.Random; import javax.swing.*; public class Frame1B { private static final int GUI_COUNT = 7; private static final Dimension GUI_SIZE = new Dimension(400, 300); private static final int DELTA = 50; private static void createAndShowUI() { Random random = new Random(); JFrame frame = new JFrame("Cascading GUI's"); CascadingGui[] guis = new CascadingGui[GUI_COUNT]; guis[0] = new CascadingGui(null); guis[0].setPreferredSize(GUI_SIZE); Window previousWindow = frame; for (int i = 0; i < guis.length - 1; i++) { guis[i + 1] = new CascadingGui(previousWindow); guis[i + 1].setPreferredSize(GUI_SIZE); final CascadingGui nextGui = guis[i + 1]; final JDialog dialog = new JDialog(previousWindow, "Cascading Dialog " + (i + 1)); dialog.getContentPane().add(guis[i+1]); dialog.pack(); dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); dialog.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { nextGui.escapeAction(); } }); // locate the dialog in a location near but randomly displaced from the super window dialog.setLocationRelativeTo(previousWindow); Point p = dialog.getLocation(); int x = p.x + 2*random.nextInt(DELTA) - DELTA; int y = p.y + 2*random.nextInt(DELTA) - DELTA; dialog.setLocation(x, y); JButton button = new JButton("New Window"); guis[i].add(button); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Window thisWin = SwingUtilities.getWindowAncestor((Component) e.getSource()); thisWin.setVisible(false); dialog.setVisible(true); } }); previousWindow = dialog; } frame.getContentPane().add(guis[0]); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } } @SuppressWarnings("serial") class CascadingGui extends JPanel { private static final String ESCAPE = "Escape"; private Window superWindow; public CascadingGui(Window superWindow) { this.superWindow = superWindow; int condition = JComponent.WHEN_IN_FOCUSED_WINDOW; InputMap inMap = getInputMap(condition); inMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), ESCAPE); getActionMap().put(ESCAPE, new AbstractAction() { public void actionPerformed(ActionEvent e) { escapeAction(); } }); } public void escapeAction() { if (superWindow != null) { Window thisWin = SwingUtilities.getWindowAncestor(this); thisWin.setVisible(false); superWindow.setVisible(true); } } }Last edited by Fubarable; 06-28-2010 at 01:55 AM. Reason: correction
- 06-28-2010, 09:01 AM #16
This code seems to be a highly sophisticated & obviously above my level of comprehension..:DA quick and dirty example:
Can you please help me understand the role of "CascadingGui" in the above code.
thnx.:cool:The Quieter you become the more you are able to hear !
- 06-28-2010, 12:41 PM #17
What's the "role" you ask about?help me understand the role of "CascadingGui"
Do you understand what is happening in the code that you originally posted?
Did you add the debugging output I described and look at the output?
It shows that there are 2 listeners on the queue and that they are both called when the ESC key is pressed.
As Furbarable says, your code has some very poor usages in it.
You really need a new design.
-
In my code, CascadigGui is a JPanel-derived class that is added to the contentPane of a JFrame or JDialog. The CascadigGui constructor allows passage of a Window (JFrame or JDialog for instance) reference which is the parent window that launches the new GUI (the one that holds this CascadingGui JPanel on its contentPane). I give CascadigGui some key binding code (the code that uses InputMap and ActionMap objects) that has it respond to a press of the escape button. If the button is pressed, and if superWindow is not null (it's null if we're currently in the very first JFrame), then CascadigGui will tell its own Window (JDialog or JFrame) that is holding it to become invisible, and tells the parent Window to become visible.
But the details aren't important. The key is that the child JDialog has a reference to the parent Window (JFrame or JDialog) that launches it, and uses this reference to make the parent window visible if escape has been pushed. This way you can avoid your static components. How you precisely implement the details is not as important as understanding the concept.
edit: and actually, I may not even need to pass references of the owning JFrame or Dialog to the child dialog as the Window class has a getOwner() method which may do the same thing. I'll have to try this to see if it works.Last edited by Fubarable; 06-28-2010 at 01:15 PM.
-
Indeed, it does work with getOwner:
Java Code:import java.awt.Component; import java.awt.Dimension; import java.awt.Point; import java.awt.Window; import java.awt.event.*; import java.util.Random; import javax.swing.*; public class Frame1B2 { private static final int GUI_COUNT = 7; private static final Dimension GUI_SIZE = new Dimension( 400, 300); private static final int DELTA = 50; private static void createAndShowUI() { Random random = new Random(); JFrame frame = new JFrame("Cascading GUI's Version 2"); CascadingGui2[] guis = new CascadingGui2[GUI_COUNT]; // first JPanel is placed into the main JFrame guis[0] = new CascadingGui2(); guis[0].setPreferredSize(GUI_SIZE); Window previousWindow = frame; // make child CascadingGui2 JPanels and place in JDialogs for (int i = 0; i < guis.length - 1; i++) { guis[i + 1] = new CascadingGui2(); guis[i + 1].setPreferredSize(GUI_SIZE); final CascadingGui2 nextGui = guis[i + 1]; final JDialog dialog = new JDialog(previousWindow, "Cascading Dialog " + (i + 1)); dialog.getContentPane().add(guis[i + 1]); dialog.pack(); dialog .setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); dialog.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { nextGui.escapeAction(); } }); // locate the dialog in a location near but randomly displaced from the // super window dialog.setLocationRelativeTo(previousWindow); Point p = dialog.getLocation(); int x = p.x + 2 * random.nextInt(DELTA) - DELTA; int y = p.y + 2 * random.nextInt(DELTA) - DELTA; dialog.setLocation(x, y); JButton button = new JButton("New Window"); guis[i].add(button); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Window thisWin = SwingUtilities .getWindowAncestor((Component) e.getSource()); thisWin.setVisible(false); dialog.setVisible(true); } }); previousWindow = dialog; } frame.getContentPane().add(guis[0]); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } } /** * CascadingGui2.java extends JPanel * This JPanel should be either a contentPane or added to * the contentPane of a Window such as a JDialog or JFrame * @author Pete * */ @SuppressWarnings("serial") class CascadingGui2 extends JPanel { private static final String ESCAPE = "Escape"; public CascadingGui2() { // key binding to trap the escape key int condition = JComponent.WHEN_IN_FOCUSED_WINDOW; InputMap inMap = getInputMap(condition); inMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), ESCAPE); getActionMap().put(ESCAPE, new AbstractAction() { public void actionPerformed(ActionEvent e) { escapeAction(); // do this action if escape pressed } }); } public void escapeAction() { // get the Window that is holding this JPanel Window thisWin = SwingUtilities.getWindowAncestor(this); // get the Window's owner, if it exists Window owner = thisWin.getOwner(); if (owner != null) { // if owner exists thisWin.setVisible(false); // make current window invisible owner.setVisible(true); // make owner window visible } } }
- 07-04-2010, 02:34 PM #20
it would be difficult for me to implement the better alternative as suggested by Fubarable in this very project.. so i would prefer to go by very stupid trick of me...to traverse between frames
in the codes that i posted there is also a reference problem.. when i traverse from Frame1 to Frame2 or from Frame2 to Frame3.. i create an object for the frame i am going into but when i resturn from frame3 to Frame2 or from FRame2 to Frame1 i just set visibility of the frames to false..
so in particulars.. when i again traverse from Frame1 to Frame2 or Frame2 to Frame3 the objects for Frame2 and Frame3 are again created.. i.e., multiple objects are created with respect to the traversals..
so i was wondering if i could just destroy or something like that the object of Frames2 and Frame3 when i return to Frame1 simultanously.. eventually..
n e suggestions?The Quieter you become the more you are able to hear !
Similar Threads
-
navigation b/w frames
By imrankhan in forum AWT / SwingReplies: 3Last Post: 03-19-2012, 12:25 PM -
jsp frames
By vasug in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 04-16-2010, 05:00 PM -
Switching Frames
By jonnytabpni in forum New To JavaReplies: 1Last Post: 11-08-2009, 10:12 PM -
The Travelling Salesman Web Service
By dnaevolutions in forum Java SoftwareReplies: 0Last Post: 02-22-2009, 06:46 PM -
Help regarding Frames
By ramesh.8189 in forum AWT / SwingReplies: 14Last Post: 02-15-2009, 08:12 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks