Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Im trying to open a new GUI window by clicking on a button

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Im trying to open a new GUI window by clicking on a button

    Hi, im trying to open a new GUI window by clicking on a button. But for now what I manage to do is only get a black window. Here is my code.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class TesteWallet extends eWallet implements ActionListener{
     
    	  eWallet SetAmt = new eWallet();
    	  public String calculated = "Amount is $";
    	  private JButton jbtSetMaxAmt = new JButton("Set");
    	  private JButton jbtSetExpenditures = new JButton("Set");
    	  private JButton jbtExpenditures = new JButton("Detail View");
    	  public JButton jbtSetDetailExp = new JButton("Set");
    	  private JLabel jlabelCurrentAmt = new JLabel("Current Available Amount");
    	  private JLabel jlabelMaxAmt = new JLabel("Maximum Amount $");
    	  private JLabel jlabelExpenditures = new JLabel("Expenditures $");
    	  public JLabel jlabelFood = new JLabel("Food $");
    	  public JLabel jlabelClothing = new JLabel("Clothing $");
    	  public JLabel jlabelTransport = new JLabel("Transport $");
    	  public JLabel jlabelSavings = new JLabel("Savings $");
    	  private JTextField jtextCalculatedAmt = new JTextField(calculated);
    	  private JTextField jtextMaxAmt = new JTextField("");
    	  private JTextField jtextExpenditures = new JTextField("");
    	  public JTextField jtextFood = new JTextField("");
    	  public JTextField jtextClothing = new JTextField("");
    	  public JTextField jtextTransport = new JTextField("");
    	  public JTextField jtextSavings = new JTextField("");
    	  private JButton jbtCalculate = new JButton("Calculate");
    	  static int jopIcon = JOptionPane.QUESTION_MESSAGE;
     
    	  boolean bFlag = true; //state, true means no exception
    	  public double cAmt;
     
     
     
    	  public TesteWallet(){
     
    		  BorderLayout b = new BorderLayout();
    		  setLayout(b);
     
    		  JPanel p  = new JPanel();
    		  JPanel p1 = new JPanel();
    		  JPanel p2 = new JPanel();
    		  JPanel p3 = new JPanel();
    		  //JPanel p4 = new JPanel();
     
     
    		  p.setLayout(new GridLayout(3,1));
    		  p1.setLayout(new GridLayout(3,1));
    		  p2.setLayout(new GridLayout(4,1));
    		  p3.setLayout(new GridLayout(3,1));
    		  //p4.setLayout(new GridLayout(4,1));
     
     
    		  jbtSetMaxAmt.setBackground(Color.yellow);//change background color
    		  jbtSetMaxAmt.setForeground(Color.black);//change font color
    		  jbtSetExpenditures.setBackground(Color.black);//change background color
    		  jbtSetExpenditures.setForeground(Color.red);//change font color
    		  jbtCalculate.setBackground(Color.green);//change background color
    		  jbtCalculate.setForeground(Color.yellow);//change font color
     
     
     
     
    		  p1.add(jlabelMaxAmt);
    		  p1.add(jtextMaxAmt);
    		  p1.add(jbtSetMaxAmt);
     
    		  p2.add(jlabelExpenditures);
    		  p2.add(jtextExpenditures);
    		  p2.add(jbtSetExpenditures);
    		  p2.add(jbtExpenditures);
     
    		  p3.add(jlabelCurrentAmt);
    		  p3.add(jtextCalculatedAmt);
    		  p3.add(jbtCalculate);
     
    		  //p4.add(jlabelFood);
    		  //p4.add(jtextFood);
    		  //p4.add(jlabelClothing);
    		  //p4.add(jtextClothing);
    		  //p4.add(jlabelTransport);
    		  //p4.add(jtextTransport);
    		  //p4.add(jtextSavings);
    		  //p4.add(jbtSetDetailExp);
     
     
    		  p.add(p1);
    		  p.add(p2);
    		  p.add(p3);
     
     
    		  add(p, BorderLayout.CENTER);
     
     
     
    		  jbtSetMaxAmt.addActionListener(this);
    		  jbtSetExpenditures.addActionListener(this);
    		  jbtExpenditures.addActionListener(this);
    		  jbtCalculate.addActionListener(this);
     
     
     
    	  }	
     
    	  public void detailView(){
    		  JFrame nFrame = new JFrame("Expenditures");
    		  nFrame.setSize(500,300);
    		  nFrame.setLocationRelativeTo(null);
    		  nFrame.setVisible(true);
     
    		  BorderLayout c = new BorderLayout();
    		  setLayout(c);
     
     
    		  JPanel pM = new JPanel();
    		  JPanel p4 = new JPanel();
    		  JPanel p5 = new JPanel();
    		  JPanel p6 = new JPanel();
    		  JPanel p7 = new JPanel();
    		  JPanel p8 = new JPanel();
     
    		  pM.setLayout(new GridLayout(4,1));
    		  p4.setLayout(new GridLayout(2,1));
    		  p5.setLayout(new GridLayout(2,1));
    		  p6.setLayout(new GridLayout(2,1));
    		  p7.setLayout(new GridLayout(2,1));
    		  p8.setLayout(new GridLayout(1,1));
     
    		  p4.add(jlabelFood);
    		  p4.add(jtextFood);
    		  p5.add(jlabelClothing);
    		  p5.add(jtextClothing);
    		  p6.add(jlabelTransport);
    		  p6.add(jtextTransport);
    		  p7.add(jlabelSavings);
    		  p7.add(jtextSavings);
    		  p8.add(jbtSetDetailExp);
     
    		  pM.add(p5);
    		  pM.add(p6);
    		  pM.add(p7);
    		  pM.add(p8);
     
    		  add(pM, BorderLayout.CENTER);
     
    	  }
     
     
    	  public static void main(String[] args){
     
    		  TesteWallet frame = new TesteWallet();
    	      //JFrame frame=new JFrame();
    	    frame.setTitle("eWallet");
    	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	    frame.setSize(200,400);
    	    frame.setVisible( true );
     
     
     
     
     
    	    //SetAmt.setCurrentAmt(jtextCurrentAmt);
    	   // System.out.println("diaply window, but set is invisible");
    	  }
    	  /*public static void main(String[] args)
    	  {
    	    JFrame frame = new JFrame();
    	      //sJFrame frame=new JFrame();
    	    frame.setTitle("My Simple GUI - Drawing Shapes");
    	    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    	    frame.setSize(400.0,250);
    	    JFrame.setVisible(true);
    	  }*/
     
     
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		if (e.getSource()==jbtSetMaxAmt){
     
    			try
    			    {
    				 cAmt = Double.parseDouble(jtextMaxAmt.getText());//here, exception may come out
    				 if (cAmt < 0){
    						jtextMaxAmt.setText("");
    						JOptionPane.showMessageDialog(null, "Please Enter A Valid Amount.\n" +
    								"Eg Any Number Larger Than '0'.\n");									
    						}
    			     }catch(/*NumberFormat*/Exception nfe)
    			    {
    			    	 jtextMaxAmt.setText("");
    			    	 JOptionPane.showMessageDialog(null, "Please Enter A Valid Amount.\n" +
    						"Eg Any Number Larger Than '0'.\n");
    			    }
    			     SetAmt.setMaxAmt(cAmt);
     
     
    			}
    		else if (e.getSource()==jbtSetExpenditures){
     
    				try
    				    {
    					 cAmt = Double.parseDouble(jtextExpenditures.getText());//here, exception may come out
    					 if (cAmt < 0){
    						 jtextExpenditures.setText("");
    						JOptionPane.showMessageDialog(null, "Please Enter A Valid Amount.\n" +
    						"Eg Any Number Larger Than '0'.\n");
    						}
    				     }catch(/*NumberFormat*/Exception nfe)
    				    {
    				    	 jtextExpenditures.setText("");
    				    	 JOptionPane.showMessageDialog(null, "Please Enter A Valid Amount.\n" +
    							"Eg Any Number Larger Than '0'.\n");
    				    }
    				     SetAmt.setExpenditures(cAmt);
    				}
     
     
    		else if (e.getSource()==jbtExpenditures)
    		{
    			detailView();
    		}
     
    		else if (e.getSource()==jbtCalculate)
    		{	 
     
    			if(SetAmt.getMaxAmt() > 0 )
    			{
    			 		 if(SetAmt.getExpenditures() > 0)
    			 		 {			 						 	 
    			 			 cAmt = SetAmt.getMaxAmt() - SetAmt.getExpenditures();			 	 
    			 			 SetAmt.setCalculatedAmt(cAmt);
    			 			 jtextCalculatedAmt.setText("Amount is $" +cAmt);
    			 			 SetAmt.setMaxAmt(cAmt);
    			 			if(cAmt>0)
    					 	 { 
    					 		JOptionPane.showMessageDialog(null, "It Is Safe To Spend Your Money\n");
    					 	 }
    					 	 else
    					 	 {
    							JOptionPane.showMessageDialog(null, "WARNING!\n You Have Exceeded Your Spending Limit.\n");
    						 }
    			 		 }
    			 	 	 else
    			 	 	 {
    			 	 		 JOptionPane.showMessageDialog(null, "Please Enter Your Expenditures Amount.");
    			 	 	 }
    			 }
    			 else
    			 {
    			 		JOptionPane.showMessageDialog(null, "Please Enter Your Maximum Amount.");			 		
    			 }			 	 
     
     
    		}
     
     
     
     
     
     
     
    	}
    	}
    Last edited by amzwans; August 10th, 2011 at 11:33 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Im trying to open a new GUI window by clicking on a button

    Do you have a question associated with this? What do you expect to happen, with what components?

    For future reference, please post an SSCCE, and please use the code tags. I recommend editing your post and flanking the source code with [highlight=java]Your Code Here[/highlight]

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Im trying to open a new GUI window by clicking on a button

    What I am asking is how do I display the buttons and panel in the new window. I can't seem to display it even though I've added and delcared it in the method detailView()

Similar Threads

  1. [SOLVED] How to stop the same window from having more than one instance of it open at a time.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 9th, 2011, 05:47 PM
  2. Replies: 3
    Last Post: February 21st, 2011, 07:25 PM
  3. How to press button to open another window
    By vkokaram in forum AWT / Java Swing
    Replies: 1
    Last Post: July 18th, 2010, 03:51 PM
  4. unable to go to next page...while clicking submit button
    By javaking in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: April 29th, 2010, 02:55 AM
  5. How to upload a file by clicking a link instead of button?
    By raghuprasad in forum Java Theory & Questions
    Replies: 2
    Last Post: May 3rd, 2009, 05:21 AM

Tags for this Thread