Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java Code Issuses

  • 31-03-2005 2:22pm
    #1
    Registered Users, Registered Users 2 Posts: 1,991 ✭✭✭


    i am writing a prog for a college CA and i need to swicth from one screen to another, the prog is written in java and the screens are done in javax.swing and java.awt, i want the current windows to close and the corresponding one to appear when i click on the right button!??

    Anyone help? :(


Comments

  • Registered Users, Registered Users 2 Posts: 7,516 ✭✭✭BrokenArrows


    to close the current window you call dispose();

    to open a new window you do the following:

    McFadden.MainWindow.mainWindow MW = new McFadden.MainWindow.mainWindow();
    MW.show();

    In this example "McFadden.Mainwindow" is the package name and mainWindow is the class. If you are not using packages then you simply do

    mainWindow MW = new mainWindow();
    MW.show();


  • Registered Users, Registered Users 2 Posts: 1,991 ✭✭✭Ziycon


    Nice one, you saved my ass! :D


  • Registered Users, Registered Users 2 Posts: 7,516 ✭✭✭BrokenArrows


    no problem. i ask enough questions in here. its about time i started answering a few.


  • Registered Users, Registered Users 2 Posts: 1,991 ✭✭✭Ziycon


    this is the class that i want to close but when i click on the button it does nothing, any ideas?


    public void actionPerformed(ActionEvent e)
    {
    JButton clickedButton=(JButton)e.getSource();
    String buttonText=clickedButton.getText();

    if (buttonText.equals("Cancel"))
    {
    dispose();
    }
    .....etc


  • Registered Users, Registered Users 2 Posts: 261 ✭✭HaVoC


    have you added an ActionListener to the button cancel ?
    i.e. in your constructor for the frame:
    btnCancel.addActionListener(this);


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,991 ✭✭✭Ziycon


    Ye, i have the ActionListener implemented in the class and added to the cancel button! But i still have the problem!


  • Registered Users, Registered Users 2 Posts: 7,516 ✭✭✭BrokenArrows


    Is your public void actionPerformed(ActionEvent e) being used specifically for the cancel button.

    If so then there is no need to check if the buttontext = cancel

    Try this, this is just the way i have it set up in one of my programs. You should be able to easily modify it.

    jCancelButton.setText("Cancel");
    jCancelButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    jCancelButton_actionPerformed(e);
    }
    });

    this is the jCancelButton_actionPerformed(e); part

    private void jCancelButton_actionPerformed(ActionEvent e)
    {
    dispose():
    }

    This is a very effective and simple way to imlement this. If you want to open a new window after clicking the cancel button just ass the code shown in the previous post to the JCancelButton_actionPerformed(e); function. Hope this clarifys everything.


  • Registered Users, Registered Users 2 Posts: 261 ✭✭HaVoC


    no idea, try doing System.out.println()'s or step through it with your ide and see if actionPerformed is being called and if it is , is it going into your if statement.
    Instead of comparing the text ya might wanna try this:
    if (e.getSource() == btnCancel)
    {
    dispose();
    }


  • Registered Users, Registered Users 2 Posts: 1,991 ✭✭✭Ziycon


    neither of dem worked! everything is working but da dispose function the way i have it set up! there is more then one item in the actionPerformed method!

    stress :mad:


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Can we see the new code?


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,991 ✭✭✭Ziycon


    There any ideas?

    public void actionPerformed(ActionEvent e)
    {
    JButton clickedButton=(JButton)e.getSource();
    String buttonText=clickedButton.getText();

    if (buttonText.equals("Cancel"))
    {
    dispose();
    }

    else if (buttonText.equals("Submit"))
    {
    if (discDefault != true)
    {
    try
    {
    double inputDisc = Double.parseDouble(txtOther.getText());

    if ((inputDisc > 0) && (inputDisc < 100))
    {
    discount = inputDisc;
    System.out.println(discount);
    }

    else
    System.out.println("Improper Discount!!!");
    }

    catch (NumberFormatException nfe)
    {
    System.out.println("Invalid Discount Value entered!");
    }
    }

    else
    { System.out.println(discount); } //PRINTOUT DISCOUNT
    }
    }


    Also can anyone tell me what type of exception do i put into the brackets of the catch block for the below??
    *dbconn.connect(); is calling a method which logs into an oracle databse over a network.

    try
    {
    dbconn.connect();
    }
    catch()
    {
    JOptionPane.showMessageDialog(null, "An error occured will trying to connect to server", "Connection Error #01", JOptionPane.WARNING_MESSAGE);
    System.exit(1);
    }


  • Registered Users, Registered Users 2 Posts: 2,031 ✭✭✭lynchie


    Is it going into the actionPerformed method?

    Have you actually aded the ActionListener i.e. called cancelButton.addActionListener(this) to your code? (Assuming cancelButton is the name of your button)


  • Registered Users, Registered Users 2 Posts: 1,991 ✭✭✭Ziycon


    yes i have put in:

    cancel.addActionListener(this);


  • Registered Users, Registered Users 2 Posts: 4,188 ✭✭✭pH


    At the first line of the actionPerformed method put a System.out.println() and put another in the if statement. Run again with console showing and make sure that the action listener is indeed working (both prints are shown)

    If both prints get shown then the problem is with the dispose, otherwise it's with the ActionListener.


  • Closed Accounts Posts: 175 ✭✭bdiddy


    cancelListener = new ActionListener()
    {

    public void actionPerformed(ActionEvent e)
    {
    setVisible(false);
    dispose();
    }
    };
    cancelButton.addActionListener(cancelListener);


  • Registered Users, Registered Users 2 Posts: 7,516 ✭✭✭BrokenArrows


    that shouldnt make a difference bdiddy because if the frame is disposed it would automatically be taken off the screen.

    All that that would accomplish is that it would hide the GUI before disposing it.

    The only thing that seem to be wrong is that it does not seem to be going into the action listener.

    The only wav to prove this is to do what pH said place a few System.out.println's in the code to see exactly what part is being run.


  • Registered Users, Registered Users 2 Posts: 1,991 ✭✭✭Ziycon


    i'll try putting the s.o.p. lines in and come back to ya when i know what its running! tanx.


Advertisement