yay this first time that I develop something larger ;)
My problem: main method display a frame with buttons where you choose what function/action you want to perform. Once you press a button I want the main window become invisible and windows with option for function to become visible.
So what I did is

public void actionPerformed(ActionEvent e)
{
	if(e.getSource() == btnFirst)
	{
		myApp.setVisible(false);  // pass control to selected function
		doSomething();
                myApp.setVisible(true);   // retrive control
	}						
}

however this get me following error

C:\MyApp.java:54: cannot find symbol
symbol  : method doSomething()
location: class MyApp
            doSomething();
            ^

with call doSomething() I want to triger this

public RenameImage()
{
        myFrame = new JFrame("Do Something");
    	myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	buildGUI();
	myFrame.setSize(470,470);
    	myFrame.setVisible(true);
}

I don't even what to google for :confused:

Recommended Answers

All 4 Replies

hmm, the blatantly obvious: check whether the method actually exists in the same class you're calling it from.

And of course rename RenameImage to renameImage.

Came to same conclusion when I went to bed(funny enought I always find solution to problem went I go to sleep). But thats fine as long there would be solution :lol:
Now this peace of code doesn't do exactly what I want to do

if(e.getSource() == btnFirst)
{
	myApp.setVisible(false);  // pass control to selected function
	doSomething();
         myApp.setVisible(true);   // retrive control
}

it hidde myApp for time which take to display second application window and then it will display it again. I'm missing sort of control mechanizm which check if second window still in use, if yes keep it hidden. I tried to use while loop and check boolean in second application, but that kicked my CPU activity to 100% and I have to force to shut-down.
So how I do I get around this?
Secondly, I use JFrame setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) to close my windows, but this kill both frames.
So how do I specify I want to close only second JFrame?

NEW UPDATE
Problem with killing both JFrames sorted, replaced JFrame.EXIT_ON_CLOSE with JFrame.DISPOSE_ON_CLOSE.
Now I add it WindowEvents so if windowDeactivated it will setVisible to false and windowActivated to setVisible to true. However I can't triger windowActivated when I close the second window.
What shall I do?

Finaly found solution to my problem :cheesy:
Bellow is example code from java forum(link here) which shows how this thing can be done

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class test extends JFrame implements ActionListener {
  JButton frame1Button, frame2Button;
  JFrame frame1, frame2;
 
  public test() {
    super("Frame 1");
    frame2 = new JFrame("Frame 2");
    frame2Button = new JButton("Hide");
    frame2Button.addActionListener(this);
    JPanel frame2ButtonPanel = new JPanel();
    frame2ButtonPanel.add(frame2Button);
    frame2.getContentPane().add(frame2ButtonPanel, "North");
    frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame2.setSize(300,200);
    frame2.setLocation(350,350);
    frame2.setVisible(true);
    frame1 = this;
    frame1Button = new JButton("Hide");
    frame1Button.addActionListener(this);
    JPanel frame1ButtonPanel = new JPanel();
    frame1ButtonPanel.add(frame1Button);
    getContentPane().add(frame1ButtonPanel, "North");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(300,200);
    setLocation(100,100);
    setVisible(true);
  }
 
  public void actionPerformed(ActionEvent e) {
    JButton button = (JButton)e.getSource();
    if(button == frame1Button) {
      frame1.setVisible(false);
      frame2.setVisible(true);
    }
    if(button == frame2Button) {
      frame1.setVisible(true);
      frame2.setVisible(false);
    }
  }
 
  public static void main(String[] args) {
    new test();
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.