Learning swing

applet only displays after 3 runs

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 2130 Views - Last Post: 19 July 2010 - 09:15 AM Rate Topic: -----

#1 irmoathlete  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 98
  • Joined: 09-July 10

Learning swing

Posted 18 July 2010 - 09:56 PM

The applet only displays the added things after running a few times, usually 3


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

public class learnSwing extends JFrame
{
	 JMenuBar menu;
	 JLabel msg,displayText;
	 JButton calculateButton;
	 JButton displayString;
	 JPanel panel;
	 JTextField txtfield;
	 
	
	public static void main(String[]args)
	{
		//String input = JOptionPane.showInputDialog("Please enter your name: ");		
		//JOptionPane.showMessageDialog(null,"The name you entered is: "+input);
		new learnSwing();
	}

	public learnSwing()
	{
		//Sets the size of the Frame
		setSize(800,600);
		
		//Centers Frame on screen if null
		setLocationRelativeTo(null);	

		
		//Sets the title of the Frame
		setTitle("Learning Swing");
		
		//Sets the action to close when the user clicks the x
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        //Makes the Frame visible
        setVisible(true);
        
        msg = new JLabel("hey click that button");
        
        calculateButton = new JButton("Calculate");       
        calculateButton.addActionListener(new calculateButtonListener());
        
        txtfield = new JTextField(15);

        
        panel = new JPanel();
        panel.add(calculateButton);        
        
        add(panel);
        panel.add(msg);   
        panel.add(txtfield);
        
	}
	
	public class calculateButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			if(e.getSource()== calculateButton)
			{
				String tender = txtfield.getText();
				
				if(tender.length()==0)
				{
					JOptionPane.showMessageDialog(null,"Please enter the form of tender you wish to pay with: ");
				}
				else
				{
					JOptionPane.showMessageDialog(null,"Please enter amount given");
					String cashNeeded=txtfield.getText();
					double cashCost = Double.parseDouble(cashNeeded);
					double cashReceive = Double.parseDouble(txtfield.getText());
					JOptionPane.showMessageDialog(null,"You gave: "+cashReceive);
				}				
				txtfield.requestFocus();
			}
			calculateButton.setForeground(Color.BLUE);		
			msg.setForeground(Color.RED);
		}
		
	}
}




This post has been edited by irmoathlete: 18 July 2010 - 09:57 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Learning swing

#2 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Learning swing

Posted 18 July 2010 - 10:02 PM

Edit: didn't read close enough:

I saw the AWT imports and started to question your code. Swing is the newer version of GUI while AWT is outdated. You can remove the Component one

This post has been edited by Luckless: 18 July 2010 - 10:06 PM

Was This Post Helpful? 0
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Learning swing

Posted 18 July 2010 - 10:03 PM

So set it visible AFTER you've added the components:

	public learningSwing()
	{
		//Sets the size of the Frame
		setSize(800,600);

		//Centers Frame on screen if null
		setLocationRelativeTo(null);


		//Sets the title of the Frame
		setTitle("Learning Swing");

		//Sets the action to close when the user clicks the x
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


                msg = new JLabel("hey click that button");

                calculateButton = new JButton("Calculate");
                calculateButton.addActionListener(new calculateButtonListener());

                txtfield = new JTextField(15);


                panel = new JPanel();
                panel.add(calculateButton);

                add(panel);
                panel.add(msg);
                panel.add(txtfield);

                //Makes the Frame visible
                setVisible(true);


	}



OR call validate() when done adding to an already visible frame.
Was This Post Helpful? 1
  • +
  • -

#4 irmoathlete  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 98
  • Joined: 09-July 10

Re: Learning swing

Posted 18 July 2010 - 10:04 PM

luckless i do create a frame and the reason for the awt is for the awt events.. am i correct? correct me if im wrong please
Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Learning swing

Posted 18 July 2010 - 10:05 PM

View PostLuckless, on 19 July 2010 - 12:02 AM, said:

First of, you never create a Frame. Secondly, you aren't using Swing, you're using AWT in your current code. Swing includes JComponents. This mean's you should remove your AWT imports at the top and convert your program to the standard JComponents (add a J to the beginning of every component)

//example of what goes in your constructor
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JLabel label = mew JLabel("Learning");
panel.add(label);
frame.add(panel);
frame.pack();
frame.setVisable(true);



I don't know if I want to -1 you for this....he is using Swing...look:
     JMenuBar menu;
     JLabel msg,displayText;
     JButton calculateButton;
     JButton displayString;
     JPanel panel;
     JTextField txtfield;



And he does create a JFrame...look in the main() method.
Was This Post Helpful? 1
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Learning swing

Posted 18 July 2010 - 10:07 PM

@Luckless: There are many AWT tools like Color, Graphics, Graphics2D, Events, and Listeners that are still widely used today. Though most AWT Components have been replaced by their Swing counterparts, there are still some like Canvas which are still useful.
Was This Post Helpful? 1
  • +
  • -

#7 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Learning swing

Posted 18 July 2010 - 10:09 PM

please do, my mistake (and it most certainly wasn't helpful) in not reading close enough. The import in question that I am referring to is the java.awt.Component. That's not necessary since you are using Swing components, correct?
Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Learning swing

Posted 18 July 2010 - 10:11 PM

View PostLuckless, on 19 July 2010 - 12:09 AM, said:

please do, my mistake (and it most certainly wasn't helpful) in not reading close enough. The import in question that I am referring to is the java.awt.Component. That's not necessary since you are using Swing components, correct?


All sins forgiven. I won't mark you cause you realized your mistake.

@irmoathlete, read the third post. I fixed your issue there.
Was This Post Helpful? 1
  • +
  • -

#9 irmoathlete  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 98
  • Joined: 09-July 10

Re: Learning swing

Posted 18 July 2010 - 10:11 PM

probably, it was a class where i was experimenting with both. Dogstopper thanks for the help that worked beautifully. Swing is something new and it's pretty cool learning it.

Luckless don't worry about it, it happens man. Thanks for the effort.
Was This Post Helpful? 0
  • +
  • -

#10 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Learning swing

Posted 18 July 2010 - 10:12 PM

It's three in the morning here. I shouldn't be on here helping since I'm halfway incoherent, lol.
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Learning swing

Posted 18 July 2010 - 10:12 PM

View PostLuckless, on 19 July 2010 - 01:02 AM, said:

Edit: didn't read close enough:

I saw the AWT imports and started to question your code. Swing is the newer version of GUI while AWT is outdated. You can remove the Component one

Importing Component and awt.* are redundant. You can remove the Component import.

As for your second point, many AWT Components like TextField, Label, Button, etc. shouldn't really be used in contemporary Java GUI programming b/c they have better Swing counterparts. There is nothing wrong with something like Canvas, though, as it is still the most efficient and powerful for painting.
Was This Post Helpful? 1
  • +
  • -

#12 irmoathlete  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 98
  • Joined: 09-July 10

Re: Learning swing

Posted 18 July 2010 - 10:13 PM

wait, in the swing tutorial i read i thought it encouraged those?

nevermind... edit.. thats because theyre jcomponents. my bad

This post has been edited by irmoathlete: 18 July 2010 - 10:14 PM

Was This Post Helpful? 0
  • +
  • -

#13 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Learning swing

Posted 18 July 2010 - 10:15 PM

View PostLuckless, on 19 July 2010 - 12:12 AM, said:

It's three in the morning here. I shouldn't be on here helping since I'm halfway incoherent, lol.


I know the feeling.

@irmoathlete, glad I could help :D
Was This Post Helpful? 1
  • +
  • -

#14 irmoathlete  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 98
  • Joined: 09-July 10

Re: Learning swing

Posted 18 July 2010 - 10:18 PM

while i'm on this thread, is there a way to add a textField to a JOptionPane?
Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Learning swing

Posted 18 July 2010 - 10:19 PM

@Luckless: Where in the US are you? I'm on the East Coast, and it's almost 1:30 AM my time.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2