JScrollPane in Applet

  • Follow


Hello,

I wrote some code to handle with my databse:

import java.io.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.*;
import java.sql.*;
import javax.swing.*;

/*
<APPLET CODE="shortVersion.class" WIDTH="1000" HEIGHT="500">
</APPLET>
*/

public class shortVersion extends Applet implements ActionListener {
	
	ResultSet rs;
	Statement s;	
	Button button, back;
	int op = 0;
	JScrollPane scrollPane;
	JTable table;
	String[] columnNames = {"A", "B", "C"};
     int size = 100;
	String[][] data = new String[size][3];
	
	public void init() {
		
		button = new Button("Click me");
		button.addActionListener(this);
		button.setBounds(10,90,130,30);
		add(button);
		
		back = new Button("Back");
		back.addActionListener(this);
		back.setBounds(10, 300, 130, 30);
		
		table = new JTable(data, columnNames);
		table.setPreferredScrollableViewportSize(new Dimension(500, 100));
		scrollPane = new JScrollPane(table);
		//add(scrollPane);
	}
	
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == button) {
			removeAll();
			repaint();
			add(back);
			op = 1;
		} else if (e.getSource() == back) {
			removeAll();
			repaint();
			menu();
			op = 0;
		}
	}
	
	public void paint(Graphics g) {
		super.paint(g);
		
		if (op == 1) {
			
			try {
					Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        	    		String filename = "C:\\Documents and 
Settings\\Abc\\Pulpit\\db\\db.mdb";

             		String database = "jdbc:odbc:Driver={Microsoft Access 
Driver (*.mdb)};DBQ=";
             		database+= filename.trim() + ";DriverID=22;READONLY=True}";
             		Connection con = DriverManager.getConnection( database 
,"","");
		   			s = con.createStatement();
				
					s.execute("SELECT Invoices.InvoiceNumber, Invoices.Reseller, 
Invoices.Date FROM Invoices");
					rs = s.getResultSet();
			
				int t = 0;
				String inDate, inDate2;

				if (rs != null) {
					while (rs.next()) {
						inDate = rs.getString(3);
						inDate2 = inDate.substring(0, 10);

						data[t][0] = rs.getString(1);
						data[t][1] = rs.getString(2);
						data[t][2] = inDate2;
						
						if (t == size) {
							String[][] temp = new String[size * 2][3];
							System.arraycopy(data, 0, temp, 0, data.length);
							data = temp;
							temp = null;
						}
						t++;
					}
				}

				s.close();
				con.close();
			} catch (Exception error) {
				error.printStackTrace();
				g.drawString("Error while querying", 250, 250);
			}

			add(scrollPane);
		}
	}
	
	public void menu() {
		add(button);
	}
}

The problem is, that when I want to add the JScrollPane after clicking 
the "Click me" button. I found it, that when I uncomment the 
add(scrollPane) in init() method, then scrollPane shows up after 
starting the applet and after pressing the button also. While 
add(scrollPane) in init() is commented there will be no results. And I 
just want to see it when I press this button.

Anyone can tell me what I did wrong, and help me to find solution?

Lukasz.
0
Reply fatmouseREMOVEIT (1) 7/13/2006 6:55:57 PM

Lukasz wrote:

> I wrote some code to handle with my databse:

The database is unimportant, and if you took out the
refererences to it, I could run your code..

> public class shortVersion extends Applet implements ActionListener {

The convention for class names in Java is to have
EachWordStartUpperCase, it helps other people to
understand the code if you stick to the usual conventions.

Note that Applet is an AWT based class (even though
it is in the 'applet' package). *

> 	public void init() {
>
> 		button = new Button("Click me");

...it helps not to indent so much for posting to usenet, many news
readers will wrap text lines longer than about 65-70 characters.

> 		table = new JTable(data, columnNames);
> 		table.setPreferredScrollableViewportSize(new Dimension(500, 100));
> 		scrollPane = new JScrollPane(table);

* ..but both JTable and JScrollPane are part of Swing.  Swing and AWT
components should not be mixed together (as a general rule).


> 					s.execute("SELECT Invoices.InvoiceNumber, Invoices.Reseller,
> Invoices.Date FROM Invoices");

Please change all 'tabs' for '  ' before posting, tabs are another
thing
which news readers handle unpredictably.

> The problem is, that when I want to add the JScrollPane after clicking
> the "Click me" button. I found it, that when I uncomment the
> add(scrollPane) in init() method, then scrollPane shows up after
> starting the applet and after pressing the button also. While
> add(scrollPane) in init() is commented there will be no results. And I
> just want to see it when I press this button.
>
> Anyone can tell me what I did wrong, and help me to find solution?

As I mentioned earlier, you should not mix AWT and Swing.
Since AWT has no table, you are probably best sticking with Swing.
Change the Applet for a JApplet, the Button for a JButton, and
add everything to..
  swingapplet.getContentPane();

OTOH, I suspect that is not the actual problem here.  If you
post something I can compile and run (without having to fix
wrapped lines and install a D/B) I might be able to advise further.

Andrew T.

0
Reply andrewthommo 7/13/2006 7:16:58 PM


>[cut]

import java.io.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.*;
import java.sql.*;
import javax.swing.*;

/*
<APPLET CODE="ShortVersion.class" WIDTH="1000" HEIGHT="500">
</APPLET>
*/

public class ShortVersion extends Applet implements ActionListener {	
ResultSet rs;
Statement s;	
Button button, back;
int op = 0;
JScrollPane scrollPane;
JTable table;
String[] columnNames = {"A", "B", "C"};
int size = 100;
String[][] data = new String[size][3];	
public void init() {
button = new Button("Click me");
button.addActionListener(this);
button.setBounds(10,90,130,30);
add(button);
back = new Button("Back");
back.addActionListener(this);
back.setBounds(10, 300, 130, 30);
table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 100));
scrollPane = new JScrollPane(table);
add(scrollPane);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
removeAll();
repaint();
add(back);
op = 1;
} else if (e.getSource() == back) {
removeAll();
repaint();
menu();
op = 0;
}
}
public void paint(Graphics g) {
super.paint(g);
if (op == 1) {
try {
int t = 0;
data[t][0] = "text1";
data[t][1] = "text2";
data[t][2] = "text3";
if (t == size) {
String[][] temp = new String[size * 2][3];
System.arraycopy(data, 0, temp, 0, data.length);
data = temp;
temp = null;
}
t++;
} catch (Exception error) {
error.printStackTrace();
g.drawString("Error while querying", 250, 250);
}
add(scrollPane);
}
}
public void menu() {
add(button);
}
}

And here, if that would be needed, a HTML code:

<HTML>
<HEAD></HEAD>
<BODY>
<CENTER><APPLET
CODE="shortVersion.class"
WIDTH="900"
HEIGHT="600"
 >
</APPLET></CENTER>
</BODY>
</HTML>

I hope that this would be compilable and runnable for You. There is no 
longer connection with the database.
0
Reply Lukasz 7/13/2006 8:15:35 PM

Lukasz wrote:
....
> I hope that this would be compilable and runnable for You.

Sure is - it's on-screen now.  I notice you fixed some of
the other things I mentioned as well.

I'll get back shortly after I have a look over the code..
(but don't let that stop anyone else jumping in)

Oh.. and when I mentioned taking out some of the space
in the indentation, I did not actually mean all of it!  I'll try
and demonstrate in any code I post back.

Andrew T.

0
Reply andrewthommo 7/13/2006 8:28:33 PM

Lukasz wrote:
> >[cut]

OK.  Here's the best I could hack together quickly,
I had not noticed your ..interesting strategy of layout
in the early version.

<sscce>
import java.io.*;
import java.awt.*;
import java.awt.event.*;
// what is this import supposed to do?
import java.*;
import javax.swing.*;
import javax.swing.border.*;

/*
<APPLET
  CODE="ShortVersion.class"
  WIDTH="1000" HEIGHT="500">
</APPLET>
*/
public class ShortVersion
  extends JApplet
  implements ActionListener {

  // don't mix Swing and AWT unless you can explain why
  // (and when) they should -not- be mixed!
  JButton button, back;
  int op = 0;
  JScrollPane scrollPane;
  JTable table;
  String[] columnNames = {"A", "B", "C"};
  int size = 100;
  String[][] data = new String[size][3];
  BoxLayout bl;
  Container c;

  public void init() {
    c = getContentPane();
    bl = new BoxLayout(c, BoxLayout.Y_AXIS);
    c.setLayout(bl);

    button = new JButton("Click me");
    button.addActionListener(this);
    // this is not the best way to size
    // and layout components!
    // instead you should overide the getPreferredSize()
    // method and use a layout to position the component
    // ..see the layouts section of the Java Tutorial
    // button.setBounds(10,90,130,30);
    c.add(button);

    back = new JButton("Back");
    back.addActionListener(this);
    // back.setBounds(10, 300, 130, 30);

    table = new JTable(data, columnNames);
    // note how I 'break' this line to make it shorter
    table.setPreferredScrollableViewportSize(
      new Dimension(500, 100));
    // you should always validate() or pack()
    // the layout
    validate();
  }


  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == button) {
      c.removeAll();
      c.add(back);
      op = 1;
      // add the scrollpane here!
      scrollPane = new JScrollPane(table);
      c.add(scrollPane);
      // but do not forget to 'validate' the layout.
      validate();
      repaint();
    } else if (e.getSource() == back) {
      c.removeAll();
      menu();
      op = 0;
      validate();
      repaint();
    }
  }

  public void paint(Graphics g) {
    super.paint(g);
    if (op == 1) {
    try {
    int t = 0;
    data[t][0] = "text1";
    data[t][1] = "text2";
    data[t][2] = "text3";
    if (t == size) {
      String[][] temp = new String[size * 2][3];
      System.arraycopy(data, 0, temp, 0, data.length);
      data = temp;
      temp = null;
    }
      t++;
    } catch (Exception error) {
      error.printStackTrace();
      g.drawString("Error while querying", 250, 250);
    }
      c.add(scrollPane);
    }
  }

  public void menu() {
    c.add(button);
  }
}
</sscce>

Note that each 'level' is indented by two spaces '  ',
rather than an entire 'tab'.  This allows a viewer to get
the basic idea of where the brackets line up, but still
makes the code lines fairly short.

HTH

Andrew T.

0
Reply andrewthommo 7/13/2006 9:00:21 PM

All the import I use in my applet, I forgot to remove it from ShortVersion.

Why I mixed AWT with Swing? I needed a nice table to present data from 
the database and since AWT does not have one, I used JTable and 
JScrollPane, I didn't want to change all the code to use Swing.
Another reason is that I also tried wiht scrollPane, but a similar error 
occured.
ScrollPane sp = new ScrollPane();
add(sp);

When this piece is in init(), scrollPane has scroll bars. But when 
adding it outside init(), the scroll bars are not visible. Even when I 
make them visible, they don't work.

Sorry for my irritating way of posting (and my English) :)

Thanks for Your help Andrew.
0
Reply Lukasz 7/14/2006 6:21:06 AM

Lukasz napisal(a):
> All the import I use in my applet, I forgot to remove it from ShortVersion.
>
> Why I mixed AWT with Swing? I needed a nice table to present data from
> the database and since AWT does not have one, I used JTable and
> JScrollPane, I didn't want to change all the code to use Swing.
> Another reason is that I also tried wiht scrollPane, but a similar error
> occured.
> ScrollPane sp = new ScrollPane();
> add(sp);
>
> When this piece is in init(), scrollPane has scroll bars. But when
> adding it outside init(), the scroll bars are not visible. Even when I
> make them visible, they don't work.
>
> Sorry for my irritating way of posting (and my English) :)
>
> Thanks for Your help Andrew.

Finally, I made JScrollPane work inside Applet. Once again Andrew,
thanks for Your usefull tips.

0
Reply Lukasz 7/14/2006 7:49:39 AM

Lukasz wrote:
....
> Sorry for my irritating way of posting (and my English) :)

It is more important to me that you are willing to
listen to other suggestions for posting, whch you are.

The English?  You already use English better than
many who were born to the language.  Your English
is easily understood - that is the main thing.

> Thanks for Your help Andrew.

You're welcome.  Glad you solved it.

Andrew T.

0
Reply Andrew 7/14/2006 9:01:19 AM

7 Replies
152 Views

(page loaded in 0.187 seconds)

Similiar Articles:










7/25/2012 4:13:09 AM


Reply: