-1

I have a problem about opening another class because i have a JComboBox that will contain the choices of class that i need to often so here is my code please check it .

package Patola;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class imagebut extends JFrame {
    // ImageIcon we = new ImageIcon(getClass().getResource("ban.png"));
    // ImageIcon wer = new ImageIcon(getClass().getResource("ba.png"));

    public static void main(String args[]) {
        imagebut w = new imagebut();
        w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        w.setSize(300, 300);
        w.setVisible(true);


    }
    String[] cent = {"Conversion", "Adv.Calculator"};
    JComboBox box = new JComboBox(cent);
    JButton bi = new JButton("Patola");

    public imagebut() {
        /* JButton converter = new JButton("Convertion");
        JButton advancecalc = new JButton("Adv.Calculator");
        JButton calc = new JButton("Calculator");
        JButton Multiplication = new JButton("Multiplication");
        JButton Looping = new JButton("Looping");
        JButton Calendar = new JButton("Calendar");
        JButton Remarks = new JButton("Remarks");
        JButton Average = new JButton("Average");
        JButton Magicsq = new JButton("Magic Square");*/


        //      JLabel background = new JLabel(new ImageIcon(getClass().getResource("gif.gif")));   
        JPanel pan = new JPanel();


        box.setBounds(10, 10, 100, 25);
        bi.setBounds(40, 40, 80, 30);
        getContentPane().add(pan);
        //getContentPane().add(background);
        pan.add(box);
        pan.add(bi);
        /*
        background.add(box);
        background.add(bi);
         */


        /* background.add(converter);
        background.add(calc);
        background.add(advancecalc);
        background.add(Magicsq);
        background.add(Remarks);
        background.add(Calendar);
        background.add(Average);
        background.add(Looping);
        background.add(Multiplication);*/


        box.addActionListener(new ActionListener() 
        {

            public void actionPerformed(ActionEvent e) 
            {

                String s = (String) box.getSelectedItem();
                switch(s) 
                {
                        case "Convertion": new converter(); break;
                }
            }
        });



    }
}

So if I choose converter then click the button patola I needed to open this frame this is the another class I need to open .

package Patola;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class converter extends JFrame implements ActionListener
{
        Container con=getContentPane();

        JTextField input=new JTextField("");
        JButton BMI=new JButton("B.M.I");
        JButton dollars=new JButton("Peso to Dollars");
        JButton euro=new JButton("Peso to Euros");
        JButton yen=new JButton("Peso to Yen");
        JButton c=new JButton("Celcius to Farenheit");
        JButton f=new JButton("Farenheit to Celcius");
        JButton m=new JButton("Miles to Kilometers");
        JButton km=new JButton("Kilometers to Miles");
                JTextField inch = new JTextField(15);
                JButton ok = new JButton("Okay");
                JLabel lab = new JLabel("Enter height to inches");
                JFrame fr = new JFrame();

                double a,b,d;



    public converter()
        {
    super("Super Converter");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    con.setLayout(new GridLayout(0,1));
    con.add(input);
    con.add(BMI);
    con.add(dollars);
    con.add(euro);
    con.add(yen);
    con.add(c);
    con.add(f);
    con.add(m);
    con.add(km);
    BMI.addActionListener(this);
    dollars.addActionListener(this);
    yen.addActionListener(this);
    euro.addActionListener(this);
    c.addActionListener(this);
    f.addActionListener(this);
    m.addActionListener(this);
    km.addActionListener(this);
        ok.addActionListener(this);
    setContentPane(con);

            fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                        fr.setVisible(false);
                                        fr.setSize(200,150);
                                        JPanel p = new JPanel();
                                        p.setLayout(new FlowLayout());


                                        p.add(lab);
                                        p.add(inch);
                                        p.add(ok);
                                        fr.add(p);


            }
            public void actionPerformed(ActionEvent e)
                        {

                            {
                                try
                                {
                if(e.getSource()==BMI)
                                {
                    fr.setLocation(500,250);
                                        fr.setVisible(true);

                                }

                                 if(e.getSource() == ok)
                                       {

                                            a=Double.parseDouble(inch.getText());
                                            b=Double.parseDouble(input.getText());
                                            d=b/a;
                                            JOptionPane.showMessageDialog(fr,"BMI: "+d);
                                            input.setText("");
                                            inch.setText("");
                                            fr.setVisible(false);
                                       }
                    else if(e.getSource()== dollars)
                                        {
                        double a,b,d;
                        a=44.63;
                        b=Double.parseDouble(input.getText());
                        d=b/a;
                        JOptionPane.showMessageDialog(null,"$"+d);
                        input.setText("");
                                        }
                        else if(e.getSource()== yen)
                                                {
                        double a,b,d;
                        a=2.28;
                        b=Double.parseDouble(input.getText());
                        d=b*a;
                        JOptionPane.showMessageDialog(null,"�"+d);
                        input.setText("");
                                                }
                                else if(e.getSource()== euro)
                                                                {
                        double a,b,d;
                        a=0.016;
                        b=Double.parseDouble(input.getText());
                        d=b*a;
                        JOptionPane.showMessageDialog(null,"�"+d);
                        input.setText("");
                                                                }
                            else if(e.getSource()== c)
                                                        {
                        double a,b,d,f;
                        a=1.8;
                        f=32.0;
                        b=Double.parseDouble(input.getText());
                        d=((b*a)+f);
                        JOptionPane.showMessageDialog(null,d+"�F");
                        input.setText("");
                                                        }
                                                        else if(e.getSource()== f){
                        double a,b,d,g;
                        a=0.555555555555556;
                        g=32.0;
                        b=Double.parseDouble(input.getText());
                        d=(b-g)*a;
                        JOptionPane.showMessageDialog(null,d+"ºC");
                        input.setText("");}
                        else if(e.getSource()== m)
                                                {
                        double a,b,d,f;
                        a=1.60934;
                        b=Double.parseDouble(input.getText());
                        d=(b*a);
                        JOptionPane.showMessageDialog(null,d+"Kilometers");
                        input.setText("");
                                                }
                        else if(e.getSource()== km)
                                                {

                        double a,b,d,f;
                        a=0.621371;
                                                b=Double.parseDouble(input.getText());
                        d=(b*a);
                        JOptionPane.showMessageDialog(null,d+"miles");
                        input.setText("");

                                                }
                                             }
                                catch(NumberFormatException qwe)
                                {
                                    JOptionPane.showMessageDialog(null,"Exception Thread No value !");
                                }
                    }

                        }



            public static void main (String [] args)
                        {
                converter aFrame=new converter();
                aFrame.setSize(250,350);
                                aFrame.setLocation(500,250);
                                aFrame.pack();
                                aFrame.setVisible(true);


            }
}
shampoo
  • 49
  • 3
  • 10
  • 3
    Don't just dump your code. Analyze it, debug it, post only the relevant parts. – Sotirios Delimanolis Mar 09 '14 at 02:16
  • the problem is on the first part sir . the second class i post is already Okay . but in the first part if you look at my ActionEvent then i Open the new converter(); that handle the second class but if i click it nothing will appear . – shampoo Mar 09 '14 at 02:19

1 Answers1

1
  1. This if(e.getSource().equals("Convertion")) is not going to work. e.getSource() returns the object source of a the object firing the ActionEvent. Comparing that to a String will get you nowhere.

  2. Sorry, I just had to point this out because it was bugging me. But what is "Convertion"? Is that even a word. Maybe you mean "Conversion"

  3. Just add the ActionListener to your JComboBox instead

    box.addActionListener(new ActionListener(){
        public void actionPerfomed(ActionEvent e) {
            String s = (String)box.getSelectedItem();
            switch(s) {
                case "Convertion": new conveter(); break;
                case "Adv.Calculator": ...
            }
        }
    });
    
  4. In your converter and probably all your other frame classes, setVisible(true) after adding all components.

  5. Why even use multiple frames. See The Use of Multiple JFrames, Good/Bad Practice?. You might find a better, cleaner solution.

  6. Swing apps should be run on the Event Dispatch Thread. You could do so by wrapping the inside of your main in a SwingUtilities.invokeLater.... See more details at Initial Threads

  7. If you don't want to add the ActionListener to the JComboBox and the JButton instead, just put the switch inside the actionPerformed. There's not need to check the source of the ActionEvent becuase you are only registering that listener with one button.

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • switch(s) the netbeans said string in switch are not supported in -source 1.6 . what does it mean ? – shampoo Mar 09 '14 at 02:53
  • It's pretty obvious what it means. It said it loud and clear. Either change your compiler to 1.7 or just use an `if` statement instead. – Paul Samsotha Mar 09 '14 at 02:54
  • 1.7 compiler sir ? what is it sorry i am new about this . – shampoo Mar 09 '14 at 02:55
  • Right click on your `project --> Project Properties`. Then in `Sources` set `Source/Binary` Format to `JDK 7`. – Paul Samsotha Mar 09 '14 at 02:58
  • about this sir . i correctly do your number 3 option . but nothing appear i cant open my converter class – shampoo Mar 09 '14 at 03:01
  • The frame should open when you select the item from the combobox. If you are having problems with switch statements, just use `if` statements. – Paul Samsotha Mar 09 '14 at 03:04
  • in your pc sir it is runnning ? – shampoo Mar 09 '14 at 03:05
  • I didn't test it. Also take out the `main` method in `converter`. You don't need it or anything in it. – Paul Samsotha Mar 09 '14 at 03:05
  • i will take it out ? how sir ? – shampoo Mar 09 '14 at 03:07
  • Highlight all of it with your mouse. Then press delete on your keyboard – Paul Samsotha Mar 09 '14 at 03:07
  • i did already sir . i removed my main method in the converter but still not opening the converter – shampoo Mar 09 '14 at 03:10
  • You chnaged the name in the combo box to conversion, so you need to change the swtch also. Also you need to set the frame visible(true), it's currently false. – Paul Samsotha Mar 09 '14 at 03:12
  • what frame sir i need to set true . the frame in converter ? i am right ? – shampoo Mar 09 '14 at 03:15
  • Look, how many frames do you have? Out of the two, which one has setVisible(false) – Paul Samsotha Mar 09 '14 at 03:16
  • the converter sir . i mean sir how can i set it true just only setVisible(true); it didnt work sir . i dont have a main method to named my JFrame sir . i am confusing hahaha . – shampoo Mar 09 '14 at 03:19
  • Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError at javax.swing.text.StyleContext$SmallAttributeSet.containsAttributes(StyleContext.java:973) at javax.swing.text.StyleContext$SmallAttributeSet.equals(StyleContext.java:852) at java.util.WeakHashMap.eq(WeakHashMap.java:343) at java.util.WeakHashMap.get(WeakHashMap.java:468) at java.util.Collections$SynchronizedMap.get(Collections.java:2037) – shampoo Mar 09 '14 at 03:27
  • It works for me. You need to change "Contertion" in your switch to "Conversion" Then in the `converter` change `setVisible(false)`to `true`. Put the setVisible _after_ you finish adding all the components. – Paul Samsotha Mar 09 '14 at 03:29
  • **Don't** create a `new Converter()` in your `converter` constructor. That is probably why you are getting `stackoverflow` – Paul Samsotha Mar 09 '14 at 03:31
  • i see now i get It the only problem to me is the "Conversion" thanks sir godbless :) – shampoo Mar 09 '14 at 03:32
  • Also your `converter` is _already_ a `JFrame`. You don't need to create another `JFrame` – Paul Samsotha Mar 09 '14 at 03:32