Changing content on Applet

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 438 Views - Last Post: 28 January 2013 - 11:57 AM Rate Topic: -----

#1 novakasss  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 352
  • Joined: 11-July 12

Changing content on Applet

Posted 26 January 2013 - 04:53 AM

I have a JApplet program, that first of all adds a login form. If user exists, then I want to move the user to another screen - "Instructions screen". But how I need to change those "screens" ? I used setContentPane() method to do that, but after filling the form, it freezes...
Is This A Good Question/Topic? 0
  • +

Replies To: Changing content on Applet

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2204
  • View blog
  • Posts: 5,236
  • Joined: 10-September 10

Re: Changing content on Applet

Posted 26 January 2013 - 05:00 AM

So the content of the JApplet is a JFrame and to swap content you're changing the JFrame's content pane? Can you show what that code looks like? Show the init() method and then the part that does the swap. Show the whole thing if you can.

When the freeze occurs, are you getting an error message? If so, please include the error message, copied and pasted.
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2780
  • View blog
  • Posts: 11,765
  • Joined: 20-September 08

Re: Changing content on Applet

Posted 26 January 2013 - 05:45 AM

http://technojeeves....with-cardlayout
Was This Post Helpful? 1
  • +
  • -

#4 novakasss  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 352
  • Joined: 11-July 12

Re: Changing content on Applet

Posted 26 January 2013 - 05:59 AM

View PostGregBrannon, on 26 January 2013 - 05:00 AM, said:

So the content of the JApplet is a JFrame and to swap content you're changing the JFrame's content pane? Can you show what that code looks like? Show the init() method and then the part that does the swap. Show the whole thing if you can.

When the freeze occurs, are you getting an error message? If so, please include the error message, copied and pasted.

package kurybinis;
import java.sql.*;
import java.sql.DriverManager;

import javax.swing.*;
/*
 * Programos paleidimo klase. Paleidžia programą.
 */
public class Pagrindine extends JApplet{

	/*
	 * Kintamuju aprašymas
	 */
	private PrisijungimoLangas pl;
	private Connection con;
	
	/*
	 * Pagrindinis metodas. Sukuria klases "Pagrindine" objektą
	 */
	public static void main(String[] args){
		Pagrindine pgr = new Pagrindine();
		pgr.init();
	}
	
	/*
	 * JApplet paleidimo metodas.
	 */
	public void init(){
		try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                	prisijungtiPrieBazes();
                	start();
                }
            });
        } catch (Exception e) {
            System.err.println("createGUI didn't complete successfully");
            e.printStackTrace();
        }
	}
	
	/*
	 * Prisijungia prie duomenu bazes
	 */
	public void prisijungtiPrieBazes(){
		try{
			
			String drive = "sun.jdbc.odbc.JdbcOdbcDriver";
			Class.forName(drive);
			String db = "jdbc:odbc:kurybinis";
			con = DriverManager.getConnection(db);
			System.out.println("Prisijungta");
			
		}catch(Exception e){}
	}
	
	/*
	 * Sukuria GUI objektą, grafiką. 
	 */
	public void start(){
		pl = new PrisijungimoLangas(con,this);
    	setContentPane(pl);
    	setSize(600,600);
	}

}



Main class. And here is the first screen:
package kurybinis;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import java.sql.*;

public class PrisijungimoLangas extends JPanel implements ActionListener{
	
	/*
	 * Kintamuju aprašymas
	 */
	private JPanel vidus;
	private JTextField vartotojoVardas;
	private JTextField slaptazodis;
	private JButton prisijungti;
	private JLabel pavadinimas;
	private Connection con;
	private Statement ps;
	private ResultSet rt;
	private String vardas;
	private String slaptaz;
	private Pagrindine pagr;

	/*
	 * Konstruktorius. Gauna prisijungimo i duomenu baze kintamaji. 
	 */
	public PrisijungimoLangas(Connection con, Pagrindine pagr){
		this.con = con;
		this.pagr = pagr;
		nustatytiElementus();
	}
	
	/*
	 * Sukuria lango aplinką.
	 */
	public void nustatytiElementus(){
		pavadinimas = new JLabel("PRISIJUNGIMAS");
		pavadinimas.setFont(new Font("Serif",Font.BOLD,20));
		vidus = new JPanel();
		vidus.add(Box.createRigidArea(new Dimension(10, 170)));
		vidus.add(pavadinimas);
		vartotojoVardas = new JTextField("Prisijungimo vardas",20);
		slaptazodis = new JPasswordField("slaptazodis",20);
		prisijungti = new JButton("Prisijungti");
		prisijungti.addActionListener(this);
		vidus.setLayout(new BoxLayout(vidus,BoxLayout.Y_AXIS));
		vidus.add(Box.createRigidArea(new Dimension(10, 30)));
		vidus.add(vartotojoVardas);
		vidus.add(Box.createRigidArea(new Dimension(10, 20)));
		vidus.add(slaptazodis);
		vidus.add(Box.createRigidArea(new Dimension(10, 30)));
		vidus.add(prisijungti);
		this.add(vidus);
	}

	/*
	 * Metodas paleidžiamas vartotojui užpildžius formą.
	 */
	@Override
	public void actionPerformed(ActionEvent ae) {
		try {
		vardas = vartotojoVardas.getText().trim();
		slaptaz = slaptazodis.getText().trim();
		
		String sql = "select Username,Password from Table1 where Username = '"+vardas+"'and Password = '"+slaptaz+"'";
		String sql2 = "select Already,Score from Table1 where Username = '"+vardas+"'and Password = '"+slaptaz+"'";
		String sql3 = "select Score from Table1 where Username = '"+vardas+"'and Password = '"+slaptaz+"'";
		ps = con.createStatement();
		rt = ps.executeQuery(sql);
		
		int count = 0;
		while(rt.next()){
			count++;
		}
		
		ps.close();

		if(count == 1){
			ps = con.createStatement();
			ps = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
	                   ResultSet.CONCUR_UPDATABLE);
			rt = ps.executeQuery(sql2);
			while(rt.next()){
				int points = rt.getInt("Score");
				rt.updateString("Already","Yes");
				rt.updateInt("Score", points+7);
				rt.updateRow();
			}
			ps.close();
			System.out.println("OK");
			pagr.setContentPane(new Instrukcijos(con,pagr));
		}
		else if(count == 0)
			JOptionPane.showMessageDialog(this, "Tokio vartotojo nera. Bandykite dar kartą");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}


Here is simple instructions screen:
package kurybinis;

import javax.swing.*;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.*;
import java.sql.*;

public class Instrukcijos extends JPanel implements ActionListener{

	private Connection con;
	private Pagrindine pagr;
	private JLabel pavadinimas;
	private JPanel vidus;
	
	public Instrukcijos ( Connection con, Pagrindine pagr ){
		this.con = con;
		this.pagr = pagr;
		rasytiInstrukcijas();
	}
	
	private void rasytiInstrukcijas() {
		pavadinimas = new JLabel("INSTRUKCIJOS");
		pavadinimas.setFont(new Font("Serif",Font.BOLD,20));
		vidus = new JPanel();
		vidus.add(Box.createRigidArea(new Dimension(10, 70)));
		vidus.add(pavadinimas);
		this.add(vidus);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
	}

}


I don't get any error messages. It simply freezes, and for example if I pop up another window (browser or any other) and then open again my program window it becomes blank white.
Was This Post Helpful? 0
  • +
  • -

#5 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1407
  • View blog
  • Posts: 3,123
  • Joined: 05-April 11

Re: Changing content on Applet

Posted 26 January 2013 - 09:02 AM

You have misunderstood how to use PreparedStatements
Read this article
Was This Post Helpful? 1
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2780
  • View blog
  • Posts: 11,765
  • Joined: 20-September 08

Re: Changing content on Applet

Posted 26 January 2013 - 09:25 AM

Why are you loading the jdbc-odbc bridge in an applet?

a. the applet will need to be signed to do that
b. the database needs to be on the same machine as the browser you're viewing it in. In which case - why not use an application?

This post has been edited by g00se: 26 January 2013 - 09:29 AM

Was This Post Helpful? 0
  • +
  • -

#7 novakasss  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 352
  • Joined: 11-July 12

Re: Changing content on Applet

Posted 26 January 2013 - 09:32 AM

View Postg00se, on 26 January 2013 - 09:25 AM, said:

Why are you loading the jdbc-odbc bridge in an applet?

a. the applet will need to be signed to do that
b. the database needs to be on the same machine as the browser you're viewing it in. In which case - why not use an application?

How can I add application in the browser? DO I need to use Java Web Start?
My programming knowledge is still poor, so don't judge me if I make so many mistakes.

This post has been edited by novakasss: 26 January 2013 - 10:12 AM

Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2780
  • View blog
  • Posts: 11,765
  • Joined: 20-September 08

Re: Changing content on Applet

Posted 26 January 2013 - 10:36 AM

Quote

How can I add application in the browser? DO I need to use Java Web Start?


Well that wouldn't be much different to using an applet. Questions for you?

a. where is the db - on the browser machine or a server somewhere?
b. why do you want a web browser have to be involved at all?
Was This Post Helpful? 0
  • +
  • -

#9 novakasss  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 352
  • Joined: 11-July 12

Re: Changing content on Applet

Posted 26 January 2013 - 10:37 AM

View Postg00se, on 26 January 2013 - 10:36 AM, said:

Quote

How can I add application in the browser? DO I need to use Java Web Start?


Well that wouldn't be much different to using an applet. Questions for you?

a. where is the db - on the browser machine or a server somewhere?
b. why do you want a web browser have to be involved at all?


At this time db is in my computer. I created it with Microsoft Access. But I will create another MySQL on web host.
I want to involve web browser, because I will add this program in to my website.

This post has been edited by novakasss: 26 January 2013 - 10:38 AM

Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10668
  • View blog
  • Posts: 39,615
  • Joined: 27-December 08

Re: Changing content on Applet

Posted 26 January 2013 - 10:37 AM

The OP is trying to use an Applet as a substitute for learning either Java EE or PHP.

Per this thread.
Was This Post Helpful? 0
  • +
  • -

#11 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2780
  • View blog
  • Posts: 11,765
  • Joined: 20-September 08

Re: Changing content on Applet

Posted 26 January 2013 - 10:42 AM

Quote

At this time db is in my computer. I created it with Microsoft Access. But I will create another MySQL on web host.

That's a completely different issue and a much better scenario. Host the applet on the same machine as the MySql db - problem solved. The applet is a front end to your db and doesn't even need to be signed.

The only small problem is that a client-side db connection is not quite as secure, but if you enforce login, it's not too bad, though theoretically credentials might be sniffable
Was This Post Helpful? 1
  • +
  • -

#12 novakasss  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 352
  • Joined: 11-July 12

Re: Changing content on Applet

Posted 26 January 2013 - 10:47 AM

View Postg00se, on 26 January 2013 - 10:42 AM, said:

Quote

At this time db is in my computer. I created it with Microsoft Access. But I will create another MySQL on web host.

That's a completely different issue and a much better scenario. Host the applet on the same machine as the MySql db - problem solved. The applet is a front end to your db and doesn't even need to be signed.

The only small problem is that a client-side db connection is not quite as secure, but if you enforce login, it's not too bad, though theoretically credentials might be sniffable


I wanted first to create a program inside my pc. And I don't understand this sentence:
The applet is a front end to your db and doesn't even need to be signed.


sign applet. What is that mean?
Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2780
  • View blog
  • Posts: 11,765
  • Joined: 20-September 08

Re: Changing content on Applet

Posted 26 January 2013 - 10:50 AM

http://docs.oracle.c...t/security.html
Was This Post Helpful? 0
  • +
  • -

#14 novakasss  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 352
  • Joined: 11-July 12

Re: Changing content on Applet

Posted 26 January 2013 - 01:02 PM

So. Now I'm trying to connect to mysql server. I get this error message:
java.sql.SQLException: Access denied for user 'sportovikt_base'@'78.61.73.165' (using password: YES)


Is that mean that I correctly connected to database, and only password I need?
I'm asking because I'm using this form:
on = DriverManager.getConnection(url,userName,password);


And I think I filled in information correctly.

And here is the url:
String url = "jdbc:mysql://sportoviktorina.lt/phpmyadmin/sportovikt_db";


Is this a good form? Do I need to write my website address after that mysql?
Was This Post Helpful? 0
  • +
  • -

#15 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2780
  • View blog
  • Posts: 11,765
  • Joined: 20-September 08

Re: Changing content on Applet

Posted 26 January 2013 - 01:07 PM

That's a db configuration issue
http://www.thegeekst...ent-connection/
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2