How do I setIconImage & a few other properties

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 7119 Views - Last Post: 16 June 2009 - 11:51 AM Rate Topic: -----

#1 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 310
  • Joined: 30-June 08

How do I setIconImage & a few other properties

Posted 15 June 2009 - 03:22 PM

Ok, I did this Java tutorial on Sun's Page HERE, I really didn't have any problems, and I like setting up my project this way (has some VB like quality's). But at the end there is a little quiz, done fine on that, then it tells you to alter some properties. So I tried changing the background color of the form, and nothing happens. What's really driving me nuts is I can't change the icon either, been on this for an hour, :angry: figured I better try to get some help.

Attached Image

Is This A Good Question/Topic? 0
  • +

Replies To: How do I setIconImage & a few other properties

#2 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 310
  • Joined: 30-June 08

Re: How do I setIconImage & a few other properties

Posted 15 June 2009 - 04:46 PM

Also, which property would I change to get the form to open center screen? :blink:
Thanks
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8015
  • View blog
  • Posts: 31,118
  • Joined: 06-March 08

Re: How do I setIconImage & a few other properties

Posted 15 June 2009 - 06:21 PM

View Postjimdandy75, on 15 Jun, 2009 - 03:46 PM, said:

Also, which property would I change to get the form to open center screen? :blink:
Thanks

probably setLocation(x, y) or setBounds(x,y,w,h) don't know which one you used
and for your IconImage without seing the code it is difficult to tell you what you did wrong
Was This Post Helpful? 0
  • +
  • -

#4 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 310
  • Joined: 30-June 08

Re: How do I setIconImage & a few other properties

Posted 15 June 2009 - 07:06 PM

Thanks, setBounds did work to change the startup location. This project is made completely like the tutorial says, in NetBeans by adding a JForm, drag and dropping components, and editing them on the properties palette. What I don't understand is; I made a swing application from scratch before and was able to change the background with this code_
setBackground(Color.red);


But on this one you can't alter the generated code, and when I change the background color on the properties palette I get nothing. And the icon thing is really ticking me off. lol
Here's the code, everything in the generated code area has to be modified through the properties palette. Thanks for looking.

/*
 * CelsiusConverterGUI.java
 *
 * Created on Jun 15, 2009, 3:55:16 PM
 */

package learn;

/**
 *
 * @author Owner
 */
public class CelsiusConverterGUI extends javax.swing.JFrame {

	/** Creates new form CelsiusConverterGUI */
	public CelsiusConverterGUI() {
		initComponents();
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		btnConvert = new javax.swing.JButton();
		txtTemp = new javax.swing.JTextField();
		lblCelsius = new javax.swing.JLabel();
		lblFahrenheit = new javax.swing.JLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("Celsius Converter");
		setBackground(new java.awt.Color(204, 255, 204));
		setBounds(new java.awt.Rectangle(200, 200, 0, 0));
		setResizable(false);

		btnConvert.setFont(new java.awt.Font("SansSerif", 1, 10)); // NOI18N
		btnConvert.setText("Convert");
		btnConvert.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
		btnConvert.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnConvertActionPerformed(evt);
			}
		});

		txtTemp.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				txtTempActionPerformed(evt);
			}
		});

		lblCelsius.setText("Celsius");

		lblFahrenheit.setText("Fahrenheit");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
			layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(layout.createSequentialGroup()
				.addContainerGap()
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
					.addGroup(layout.createSequentialGroup()
						.addComponent(btnConvert, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(lblFahrenheit))
					.addGroup(layout.createSequentialGroup()
						.addComponent(txtTemp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
						.addGap(10, 10, 10)
						.addComponent(lblCelsius)))
				.addContainerGap(62, Short.MAX_VALUE))
		);

		layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnConvert, txtTemp});

		layout.setVerticalGroup(
			layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(layout.createSequentialGroup()
				.addContainerGap()
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
					.addComponent(lblCelsius)
					.addComponent(txtTemp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
					.addComponent(btnConvert)
					.addComponent(lblFahrenheit))
				.addContainerGap(15, Short.MAX_VALUE))
		);

		pack();
	}// </editor-fold>

	private void btnConvertActionPerformed(java.awt.event.ActionEvent evt) {
	   //Parse degrees Celsius as a double and convert to Fahrenheit.
	int tempFahr = (int)((Double.parseDouble(txtTemp.getText()))
			* 1.8 + 32);
	lblFahrenheit.setText(tempFahr + " Fahrenheit");

}

	private void txtTempActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
}

	/**
	* @param args the command line arguments
	*/
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new CelsiusConverterGUI().setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify
	private javax.swing.JButton btnConvert;
	private javax.swing.JLabel lblCelsius;
	private javax.swing.JLabel lblFahrenheit;
	private javax.swing.JTextField txtTemp;
	// End of variables declaration

}


Was This Post Helpful? 0
  • +
  • -

#5 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 559
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: How do I setIconImage & a few other properties

Posted 15 June 2009 - 07:29 PM

This is why people are generally advised not to use drop and drag editors untill they confidently understand the code behind them. The background colour you are trying to change in your code will have no effect on your display. The top half of your code is basically all related to just your JFrame. If you wanted to add a different backgournd colour, you would have to add the same statement somewhere underneath getContentPane().setLayout(layout);
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8015
  • View blog
  • Posts: 31,118
  • Joined: 06-March 08

Re: How do I setIconImage & a few other properties

Posted 15 June 2009 - 07:32 PM

View Postjimdandy75, on 15 Jun, 2009 - 06:06 PM, said:

Thanks, setBounds did work to change the startup location. This project is made completely like the tutorial says, in NetBeans by adding a JForm, drag and dropping components, and editing them on the properties palette. What I don't understand is; I made a swing application from scratch before and was able to change the background with this code_
setBackground(Color.red);


But on this one you can't alter the generated code, and when I change the background color on the properties palette I get nothing. And the icon thing is really ticking me off. lol
Here's the code, everything in the generated code area has to be modified through the properties palette. Thanks for looking.

Yes setBackground() works on code YOU wrote from scratch
but this is GUI generated stuff that plays with GroupLayout
and the GroupLayout fills all your JFame (over it) so you wont see any pixel of your JFrame under it

If you want tu use GUI generated stuff, stick to the GUI generator for all aspects of it (color, size, order,....) and don't try to edit the code (I wont) you have here a very basic application that can be writtent in about 20 to 30 lines your generator generated over 100 lines
Imagine now an application with a few dozens JComponents :blink:

As far as your ImageIcon are concerned, haven't seen an ImageIcon anywhere in your code
Was This Post Helpful? 1
  • +
  • -

#7 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 310
  • Joined: 30-June 08

Re: How do I setIconImage & a few other properties

Posted 15 June 2009 - 07:45 PM

Quote

This is why people are generally advised not to use drop and drag editors untill they confidently understand the code behind them.

As for this matter, I do understand drag and drop editors. I've been using Visual Basics for more than a year. When you change the background color on the palette it does add the code, but doesn't change the background. I think this might be a NetBeans issue.

Now for my icon, I'm sure the code is wrong on this screenshot, but like I said I tried various other codes for an hour. Here's the code with the background color changed and the icon. thanks again.... :D

 * CelsiusConverterGUI.java
 *
 * Created on Jun 15, 2009, 3:55:16 PM
 */

package learn;

/**
 *
 * @author Owner
 */
public class CelsiusConverterGUI extends javax.swing.JFrame {

	/** Creates new form CelsiusConverterGUI */
	public CelsiusConverterGUI() {
		initComponents();
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		btnConvert = new javax.swing.JButton();
		txtTemp = new javax.swing.JTextField();
		lblCelsius = new javax.swing.JLabel();
		lblFahrenheit = new javax.swing.JLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("Celsius Converter");
		setBackground(java.awt.Color.red);
		setBounds(new java.awt.Rectangle(200, 200, 0, 0));
		setIconImage(java.awt.Image("Celsius.jpg"));
		setResizable(false);

		btnConvert.setFont(new java.awt.Font("SansSerif", 1, 10)); // NOI18N
		btnConvert.setText("Convert");
		btnConvert.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
		btnConvert.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnConvertActionPerformed(evt);
			}
		});

		txtTemp.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				txtTempActionPerformed(evt);
			}
		});

		lblCelsius.setText("Celsius");

		lblFahrenheit.setText("Fahrenheit");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
			layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(layout.createSequentialGroup()
				.addContainerGap()
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
					.addGroup(layout.createSequentialGroup()
						.addComponent(btnConvert, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(lblFahrenheit))
					.addGroup(layout.createSequentialGroup()
						.addComponent(txtTemp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
						.addGap(10, 10, 10)
						.addComponent(lblCelsius)))
				.addContainerGap(62, Short.MAX_VALUE))
		);

		layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnConvert, txtTemp});

		layout.setVerticalGroup(
			layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(layout.createSequentialGroup()
				.addContainerGap()
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
					.addComponent(lblCelsius)
					.addComponent(txtTemp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
					.addComponent(btnConvert)
					.addComponent(lblFahrenheit))
				.addContainerGap(15, Short.MAX_VALUE))
		);

		pack();
	}// </editor-fold>

	private void btnConvertActionPerformed(java.awt.event.ActionEvent evt) {
	   //Parse degrees Celsius as a double and convert to Fahrenheit.
	int tempFahr = (int)((Double.parseDouble(txtTemp.getText()))
			* 1.8 + 32);
	lblFahrenheit.setText(tempFahr + " Fahrenheit");

}

	private void txtTempActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
}

	/**
	* @param args the command line arguments
	*/
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new CelsiusConverterGUI().setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify
	private javax.swing.JButton btnConvert;
	private javax.swing.JLabel lblCelsius;
	private javax.swing.JLabel lblFahrenheit;
	private javax.swing.JTextField txtTemp;
	// End of variables declaration

}



Attached ImageAttached Image

This post has been edited by jimdandy75: 15 June 2009 - 07:49 PM

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8015
  • View blog
  • Posts: 31,118
  • Joined: 06-March 08

Re: How do I setIconImage & a few other properties

Posted 15 June 2009 - 07:54 PM

View Postjimdandy75, on 15 Jun, 2009 - 06:45 PM, said:

Quote

This is why people are generally advised not to use drop and drag editors untill they confidently understand the code behind them.

As for this matter, I do understand drag and drop editors.

You may understand how to use them but not the code they generate. If it was the case you wouldn't ask questions here :)
Was This Post Helpful? 0
  • +
  • -

#9 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 310
  • Joined: 30-June 08

Re: How do I setIconImage & a few other properties

Posted 15 June 2009 - 07:58 PM

Guess that's true.... :blush:
But I guess my point is, it won't let me alter the generated code. At least in Visual Basics if you change background color of the Form, it does just that. It changes the background color of that form. Why wouldn't it with Netbeans?
Was This Post Helpful? 0
  • +
  • -

#10 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 559
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: How do I setIconImage & a few other properties

Posted 15 June 2009 - 07:59 PM

What I actually meant was that you shouldnt use gui editors to create your gui's. You should completely write the code yourself. GUI editors adds all this useless crap and confuses things. Now look at your code.
  setBackground(java.awt.Color.red);
  setIconImage(java.awt.Image("Celsius.jpg"));


Do you know the structure of a swing application? You have a JFrame (can be somthing else, but you use a JFrame). I like to look at a JFrame as a window frame with no window in it yet. The frame is there but you can put your hand through the middle. Its just a Frame. The middle is empty. The get somthing into the middle to add your components, so that they dont fall through the Frame, you use getContentPlane. This likes gives you a surface to place your components onto. Without somthing like this, your components will just fall through.

Now look through your code at where your getContentPane is. Its half way down. So you can only make changes to the centre of your application after this point. If you try doing it before, your just wasting your time. So back to your code again
  setBackground(java.awt.Color.red);
  setIconImage(java.awt.Image("Celsius.jpg"));


This is before the getContentPane. This means all these actions will only be performed on the JFrame, which, remember, is just the frame. This is why you cannot see it, because your not doing it onto your contentPane. Now i am not sure how your going to do it in this case because of the silly code produced by the editor, but near the bottom, where you have all your layout.add code, your going to have to add
  layout.setBackground(java.awt.Color.red);
  layout.setIconImage(java.awt.Image("Celsius.jpg"));

This will ensure these actions are performed on your contentPane and not your JFrame.
Hope this helps
Was This Post Helpful? 1
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8015
  • View blog
  • Posts: 31,118
  • Joined: 06-March 08

Re: How do I setIconImage & a few other properties

Posted 15 June 2009 - 08:10 PM

View Postjimdandy75, on 15 Jun, 2009 - 06:58 PM, said:

Guess that's true.... :blush:
But I guess my point is, it won't let me alter the generated code. At least in Visual Basics if you change background color of the Form, it does just that. It changes the background color of that form. Why wouldn't it with Netbeans?

Because your form is not displayed..... it is covered by the GroupLayout generated by the GUI generator
Was This Post Helpful? 0
  • +
  • -

#12 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 310
  • Joined: 30-June 08

Re: How do I setIconImage & a few other properties

Posted 15 June 2009 - 08:10 PM

Wow, that makes sense. I mean I'm a noobie, obvious right :) But I can understand that.
Yeah, this sucks though, because it won't let me alter any of that generated code. Is there any
other free Java IDEs, that have this built in drag and drop capacity and actually put all the code in
the correct order?
And I think my code for the setIcon is wrong, but don't know what the correct code would be ????

thanks for the help guys :^:
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8015
  • View blog
  • Posts: 31,118
  • Joined: 06-March 08

Re: How do I setIconImage & a few other properties

Posted 15 June 2009 - 08:13 PM

getContentPane() is not required since JRE 1.5

frame.add(JLabel) does frame.getContentPane().add(JLabel)
Was This Post Helpful? 0
  • +
  • -

#14 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 559
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: How do I setIconImage & a few other properties

Posted 15 June 2009 - 08:21 PM

View Postpbl, on 15 Jun, 2009 - 07:13 PM, said:

getContentPane() is not required since JRE 1.5

frame.add(JLabel) does frame.getContentPane().add(JLabel)


That makes sense, I was thinking why they didnt do somthing like that originally. contentPane confuses a lot of new people. As for another drop and drag editor which gives you good code, i used this one before HERE which provides good code. The trial version only lets you place like 20 components on your application though.
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8015
  • View blog
  • Posts: 31,118
  • Joined: 06-March 08

Re: How do I setIconImage & a few other properties

Posted 15 June 2009 - 08:25 PM

Was more then time they fixed

in 1.4

frame.add(panel);

generated a compiler error:
"do not add directly to the component() call the component.getContentPane() first and add to this contentPane()"

if the compiler is brilliant enough to tell me what to code... the heck code it for me
was fixed since 1.5
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2