applet problem

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 1884 Views - Last Post: 03 September 2012 - 06:26 AM Rate Topic: -----

#1 HopelessDev  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 136
  • Joined: 10-August 12

applet problem

Posted 31 August 2012 - 09:15 AM

Im using Japplet to develop a web app for our project, where your inputs will be transfered to a database. I dont know any language yet except java so i decided to do it with applet and embedding it to html. But the problem with the my code is that the labels and the textfields are overlapping each other when loaded to the browser. Launching it in eclipse works a little bit fine.

here's my code


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


public class firstApp extends JApplet {
	
	private double sum;
	JLabel nameLabel;
	JLabel nameLabel1;
	JLabel nameLabel2;
	JLabel nameLabel3;
	JTextField tf1;
	JTextField tf2;
	JTextField tf3;
	JTextField tf4;

	public void init() {
		nameLabel = new JLabel("Student number");
		nameLabel1 = new JLabel("Name");
		nameLabel2= new JLabel("Section");
		nameLabel3 = new JLabel("Course");
		tf1 = new JTextField("",10);
		tf2 = new JTextField("",10);
		tf3 = new JTextField("",10);
		tf4 = new JTextField("",10);
		
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(0,1));
		panel.add(nameLabel);
		panel.add(nameLabel1);
		panel.add(nameLabel2);
		panel.add(nameLabel3);
		
		JPanel fieldPanel = new JPanel();
		fieldPanel.setLayout(new GridLayout(0,1));
		fieldPanel.add(tf1);
		fieldPanel.add(tf2);
		fieldPanel.add(tf3);
		fieldPanel.add(tf4);
		
		JPanel contentPane = new JPanel();
		contentPane.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
		contentPane.setLayout(new BorderLayout());
		contentPane.add(panel, BorderLayout.CENTER);
		contentPane.add(fieldPanel, BorderLayout.EAST);
		
		setContentPane(contentPane);
		
	}	
}



Is This A Good Question/Topic? 0
  • +

Replies To: applet problem

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 204
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: applet problem

Posted 01 September 2012 - 04:42 PM

Your layouts are guessing how big to make everything and since the JTextFields are the only thing with widths, it uses that as a 'hint'.
Add a size to your applet and all should be fine, something like:
setContentPane(contentPane);
setSize( 250, 200 ); //add this


should work for what you have now.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10185
  • View blog
  • Posts: 37,603
  • Joined: 27-December 08

Re: applet problem

Posted 01 September 2012 - 05:53 PM

Moved to Java.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: applet problem

Posted 01 September 2012 - 07:11 PM

When you use a GridLayour and you know the number elements it will contain, just "help" it

panel.setLayout(new GridLayout(4,1));

will be more appropriate
Was This Post Helpful? 0
  • +
  • -

#5 HopelessDev  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 136
  • Joined: 10-August 12

Re: applet problem

Posted 02 September 2012 - 01:27 AM

Thanks, But none of the suggestions fixed it. They still overlap.
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2555
  • View blog
  • Posts: 10,660
  • Joined: 20-September 08

Re: applet problem

Posted 02 September 2012 - 04:02 AM

Quote

i decided to do it with applet and embedding it to html.


Please post that html
Was This Post Helpful? 0
  • +
  • -

#7 HopelessDev  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 136
  • Joined: 10-August 12

Re: applet problem

Posted 02 September 2012 - 04:12 AM

here
<html>
<body>
<h1>hello </h1>
<applet code = "firstApp.class" width = "400" height "300">
</applet>
</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2555
  • View blog
  • Posts: 10,660
  • Joined: 20-September 08

Re: applet problem

Posted 02 September 2012 - 04:14 AM

Quote

<applet code = "firstApp.class" width = "400" height "300">


should be
<applet code="firstApp.class" width="400" height="300"></applet>


Was This Post Helpful? 0
  • +
  • -

#9 HopelessDev  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 136
  • Joined: 10-August 12

Re: applet problem

Posted 02 September 2012 - 04:18 AM

I already have </aplet> under that line. But anyway, I still did waht you said and made it
<applet code="firstApp.class" width="400" height="300"></applet>



they still overlap
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2555
  • View blog
  • Posts: 10,660
  • Joined: 20-September 08

Re: applet problem

Posted 02 September 2012 - 04:25 AM

That's a little strange. Try it in another browser
Was This Post Helpful? 0
  • +
  • -

#11 HopelessDev  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 136
  • Joined: 10-August 12

Re: applet problem

Posted 02 September 2012 - 04:33 AM

I see. I'm current using google chrome. hm. I will download mozilla and post the results. thanks!
Was This Post Helpful? 0
  • +
  • -

#12 HopelessDev  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 136
  • Joined: 10-August 12

Re: applet problem

Posted 02 September 2012 - 04:41 AM

Wow! It's working properly in mozilla firefox. What could be the reason why it's not working with chrome? This is for a project in school and I think it should be compatible with any browser.

Anyway i have another question. How can I make the labels and the textfield have the same height? The layout technique I did with this is what i usually do with JFrame to make the field and labels have the same height but it's not working with applets.
Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2555
  • View blog
  • Posts: 10,660
  • Joined: 20-September 08

Re: applet problem

Posted 02 September 2012 - 05:03 AM

Quote

What could be the reason why it's not working with chrome?

Possibly incomplete html. Try following in Chrome
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
	<title></title>
    </head>
    <body>
	<applet code="firstApp" width="400" height="300"></applet>
    </body>
</html>


Quote

How can I make the labels and the textfield have the same height? The layout technique I did with this is what i usually do with JFrame to make the field and labels have the same height but it's not working with applets.

You're going to have to change your layout to avoid that ugly field stretching

This post has been edited by g00se: 02 September 2012 - 05:04 AM
Reason for edit:: Typo

Was This Post Helpful? 0
  • +
  • -

#14 HopelessDev  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 136
  • Joined: 10-August 12

Re: applet problem

Posted 02 September 2012 - 05:14 AM

Thanks. Can you teach me how to do it?
Was This Post Helpful? 0
  • +
  • -

#15 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2555
  • View blog
  • Posts: 10,660
  • Joined: 20-September 08

Re: applet problem

Posted 02 September 2012 - 05:18 AM

imo, simple forms in Java are much more difficult to do than they should be. So much so that i made my own class to do it so i could do
	    fm = new FormMaker("Student no.", "Name", "Section", "Course");
	    panel.add(fm);


That was before layout managers such as BoxLayout, SpringLayout etc. came out, but still it's more difficult than it ought to be. There should really be a FormLayout class, since the use case is so ubiquitous

Please let me know if that html fixed it in Chrome

This post has been edited by g00se: 02 September 2012 - 05:21 AM
Reason for edit:: Chrome outcome

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2