Applet Scrollbar and KeyListener

P

phil89

Hi,

I could not have Keyboard event when i have an Scorllbar into my
applet ?
Could you help me

Thanks
Philippe


package scroll;


import java.applet.Applet;


import java.awt.*;
import java.awt.event.*;
import java.util.*;


public class EssaiAscenseur4 extends Applet implements
AdjustmentListener,
MouseListener
,KeyListener
,ComponentListener


{
int decalageH=0;
int decalageV=0;
int largeurFenetre=300;
int hauteurFenetre=200;
Vector memoire=new Vector();
Scrollbar vbar=new Scrollbar
(Scrollbar.VERTICAL,0,(int)(hauteurFenetre * 0.9),0,1000);
Scrollbar hbar=new Scrollbar
(Scrollbar.HORIZONTAL,0,(int)(largeurFenetre * 0.9),0,500);
Color couleur=Color.magenta;


public void init() {
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}


private void jbInit() throws Exception {


addMouseListener(this);
addKeyListener(this);
addComponentListener(this);
setLayout(new BorderLayout());
vbar.addAdjustmentListener(this);
hbar.addAdjustmentListener(this);
vbar.setBlockIncrement((int)(hauteurFenetre * 0.9));
hbar.setBlockIncrement((int)(largeurFenetre*0.9));


//Fonctionne des que ces deux lignes sont enleve ou si on extends
de frame et pas de APPLET
add("East",vbar);
add("South",hbar);


setForeground(couleur);
}


// Pour gerer le Decalage
public void adjustmentValueChanged(AdjustmentEvent evt)
{
if (evt.getSource()==vbar) decalageV=evt.getValue();
else if (evt.getSource()==hbar) decalageH=evt.getValue();
repaint();
}


public void mousePressed(MouseEvent evt)
{
CercleSitue c;
Graphics g=getGraphics();
int x=evt.getX(),y=evt.getY();


c=new CercleSitue(10,x+decalageH,y+decalageV);
g.fillOval(x-20,y-20,40,40);
memoire.addElement(c);
}
public void mouseReleased(MouseEvent evt) {}
public void mouseEntered(MouseEvent evt) {}
public void mouseExited(MouseEvent evt) {}
public void mouseClicked(MouseEvent evt) {}


public void keyReleased(KeyEvent evt) {
}


public void keyPressed(KeyEvent evt) {
System.out.println("PRESSED");
}


public void keyTyped(KeyEvent evt) {


}


public void componentResized(ComponentEvent e)
{
System.out.println("ddd screen
height="+Toolkit.getDefaultToolkit().getScreenSize().getHeight()+"
screen
width="+Toolkit.getDefaultToolkit().getScreenSize().getWidth());


}


public void componentHidden(ComponentEvent e) {
}


public void componentShown(ComponentEvent e) {
}


public void componentMoved(ComponentEvent e) {
}


public void paint(Graphics g)
{
Enumeration lesCercles=memoire.elements();
CercleSitue c;


while(lesCercles.hasMoreElements())
{
c=(CercleSitue)lesCercles.nextElement();
g.fillOval(c.x-20-decalageH,c.y-20-decalageV,40,40);
}
}


public static void main (String[] args)
{


EssaiAscenseur4 applet = new EssaiAscenseur4();
Frame frame = new Frame();
frame.addWindowListener(new WindowAdapter() { public void
windowClosing(WindowEvent e) { System.exit(0); } } );
frame.add(applet, BorderLayout.CENTER);
frame.setTitle( "Applet Frame" );
applet.init();
applet.start();
frame.setSize(800, 800);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
frame.setLocation((d.width-frameSize.width)/2, (d.height-
frameSize.height)/2);
frame.setVisible(true);


}


}
 
A

Andrew Thompson

I could not have Keyboard event when i have an Scorllbar into my
applet ?
Could you help me

Generally it is best to add a '?' to any question.

But the answer is "I don't know, can you help yourself?".

By that I mean..
I tried to help you by looking at the code you posted.
It does not compile for a number of reasons.

First is that the line widths are too wide for
usenet, and get line-wrapped at arbitrary
length by news clients and web interfaces.

I wrote a tool called the Text Width Checker to
help with that. <http://pscode.org/twc/>

Then after the two long lines were fixed, I
get..

string:///EssaiAscenseur4.java:74: cannot find symbol
symbol : class CercleSitue
location: class scroll.EssaiAscenseur4
CercleSitue c;
^
string:///EssaiAscenseur4.java:79: cannot find symbol
symbol : class CercleSitue
location: class scroll.EssaiAscenseur4
c=new CercleSitue(10,x+decalageH,y+decalageV);
^
string:///EssaiAscenseur4.java:130: cannot find symbol
symbol : class CercleSitue
location: class scroll.EssaiAscenseur4
CercleSitue c;
^
string:///EssaiAscenseur4.java:135: cannot find symbol
symbol : class CercleSitue
location: class scroll.EssaiAscenseur4
c=(CercleSitue)lesCercles.nextElement();
^
4 errors
Compiled without errors: false

That output is from the STBC, another handy tool
for checking that code examples compile.
<http://pscode.org/stbc/>

Both these tools are recommended in the SSCCE
article at <http://pscode.org/sscce.html>.

Please consider posting SSCCEs in future.

As to what is wrong with your code. I don't know,
but post an SSCCE and I will look into it.

As an aside, it is not very helpful to put French
comments in a code sample intended for a largely
English speaking audience.
 
P

phil89

Hi,

I have forget this class into my sample
Could you help me ?

Regards
Philippe

public class CercleSitue
{
int rayon, x, y;

public CercleSitue(int rayon,int x,int y)
{
this.rayon=rayon;
this.x=x;
this.y=y;
}
}
 
A

Andrew Thompson

I have forget this class into my sample
Could you help me ?

Note the the two pieces of code so far supplied
do not constitute an SSCCE. Please read the document.
public class CercleSitue
{
...

OK, that runs and displays the 'no keyboard events'
problem that you describe, ..but now I can compile
and run the code, it becomes obvious it uses AWT
(I never looked that closely at first!).

Why are you using AWT in 2008?

Note that.
1) Few people could, or would be willing to, help
with fixing an AWT based bug (or code problem).
2) The MSVM can not be used as an excuse for using
AWT this late in the stage.

I'd bet that if it is recoded as Swing, it will work.
 
P

phil89

It's an old project and if i could avoid to rewrite it just for adding
scrollbar, it's better for me .

Regards
Philippe
 
P

phil89

I have problem too with swing, do you have an swing sample with my
listeners ?

Regards
Philippe
 
A

Andrew Thompson

I have problem too with swing, do you have an swing sample with my
listeners ?

Not immediately*, but I will look at an *SSCCE*
of Swing based code if your were to post one.

Since you are obviously much better at English than
I am at French, I will also ask that you use English
based attribute names and comments etc. in the SSCCE.

* I have lots of Swing based GUIs with scroll bars
that work logically, but I am still not sure what
it is you are trying to achieve, and they are not
specialised as examples.
 
P

phil89

I have just modify my AWT applet

Regards
Philippe

package scroll;

import java.applet.Applet;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JScrollBar;

public class EssaiAscenseur5 extends JApplet implements
AdjustmentListener,
MouseListener
,KeyListener
,ComponentListener

{
int decalageH=0;
int decalageV=0;
int largeurFenetre=300;
int hauteurFenetre=200;
Vector memoire=new Vector();
JScrollBar vbar=new JScrollBar
(JScrollBar.VERTICAL,0,(int)(hauteurFenetre * 0.9),0,1000);
JScrollBar hbar=new JScrollBar
(JScrollBar.HORIZONTAL,0,(int)(largeurFenetre * 0.9),0,500);
Color couleur=Color.magenta;



public void init() {
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}

private void jbInit() throws Exception {

addMouseListener(this);
addKeyListener(this);
addComponentListener(this);
setLayout(new BorderLayout());
vbar.addAdjustmentListener(this);
hbar.addAdjustmentListener(this);
vbar.setBlockIncrement((int)(hauteurFenetre * 0.9));
hbar.setBlockIncrement((int)(largeurFenetre*0.9));

//Fonctionne des que ces deux lignes sont enleve ou si on extends
de frame et pas de APPLET On doit clicker sur la souris avant ????
add("East",vbar);
add("South",hbar);


setForeground(couleur);
}

// Pour gerer le Decalage
public void adjustmentValueChanged(AdjustmentEvent evt)
{
if (evt.getSource()==vbar) decalageV=evt.getValue();
else if (evt.getSource()==hbar) decalageH=evt.getValue();
repaint();
}

public void mousePressed(MouseEvent evt)
{
CercleSitue c;
Graphics g=getGraphics();
int x=evt.getX(),y=evt.getY();

c=new CercleSitue(10,x+decalageH,y+decalageV);
g.fillOval(x-20,y-20,40,40);
memoire.addElement(c);
}
public void mouseReleased(MouseEvent evt) {}
public void mouseEntered(MouseEvent evt) {}
public void mouseExited(MouseEvent evt) {}
public void mouseClicked(MouseEvent evt) {}


public void keyReleased(KeyEvent evt) {
}

public void keyPressed(KeyEvent evt) {
System.out.println("KEY PRESSED");
}

public void keyTyped(KeyEvent evt) {

}




public void componentResized(ComponentEvent e)
{
System.out.println("ddd screen
height="+Toolkit.getDefaultToolkit().getScreenSize().getHeight()+"
screen
width="+Toolkit.getDefaultToolkit().getScreenSize().getWidth());

}

public void componentHidden(ComponentEvent e) {
}

public void componentShown(ComponentEvent e) {
}

public void componentMoved(ComponentEvent e) {
}



public void paint(Graphics g)
{
Enumeration lesCercles=memoire.elements();
CercleSitue c;

while(lesCercles.hasMoreElements())
{
c=(CercleSitue)lesCercles.nextElement();
g.fillOval(c.x-20-decalageH,c.y-20-decalageV,40,40);
}
}

public static void main (String[] args)
{

EssaiAscenseur5 applet = new EssaiAscenseur5();
JFrame frame = new JFrame();
frame.addWindowListener(new WindowAdapter() { public void
windowClosing(WindowEvent e) { System.exit(0); } } );
frame.add(applet, BorderLayout.CENTER);
frame.setTitle( "Applet Frame" );
applet.init();
applet.start();
frame.setSize(800, 800);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
frame.setLocation((d.width-frameSize.width)/2, (d.height-
frameSize.height)/2);
frame.setVisible(true);
}
public class CercleSitue
{
int rayon, x, y;

public CercleSitue(int rayon,int x,int y)
{
this.rayon=rayon;
this.x=x;
this.y=y;
}
}


}
 
A

Andrew Thompson

I could not have Keyboard event when i have an Scorllbar into my
applet ?

Let a JScrollPane handle it for you.

I am not sure if this example captures
what you are trying to achieve, but with
the code comments written in a language
I do not understand, I had to make a lot
of guesses.

This seems to work logically as either a
JApplet of JFrame. The real work is moved
out of a 'root' component to a much more
consistent and predictable JPanel. Both
frame and applet get the key events, but
the scrollpane will also respond to PgUp,
PgDn, and the arrow keys left/right/up/down.

Please watch your line width when posting
code to forums in future. The latest
example had two very wide lines that I
had to correct before it would even compile.

<sscce>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class EssaiAscenseur6 extends JApplet {
public void init() {
try {
DrawPanel dp = new DrawPanel();
dp.jbInit();
add( new JScrollPane(dp) );

} catch (Exception e) {
e.printStackTrace();
}
}

public static void main (String[] args) throws Exception {
JFrame frame = new JFrame("Drawing Frame" );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DrawPanel dp = new DrawPanel();
dp.jbInit();
frame.add(new JScrollPane(dp), BorderLayout.CENTER);
frame.pack();
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

class DrawPanel extends JPanel implements
AdjustmentListener,
MouseListener,
KeyListener,
ComponentListener {

Color couleur=Color.magenta;

int decalageH=0;
int decalageV=0;
int largeurFenetre=300;
int hauteurFenetre=200;
Vector memoire=new Vector();

void jbInit() throws Exception {
setFocusable(true);
setPreferredSize( new Dimension(500,500) );
addMouseListener(this);
addKeyListener(this);
addComponentListener(this);
setLayout(new BorderLayout());
}

// Pour gerer le Decalage
public void adjustmentValueChanged(AdjustmentEvent evt) {
repaint();
}

public void mousePressed(MouseEvent evt) {
int x=evt.getX(),y=evt.getY();
CercleSitue c=new CercleSitue(
10,
x+decalageH,
y+decalageV);
memoire.addElement(c);
repaint();
}
public void mouseReleased(MouseEvent evt){}
public void mouseEntered(MouseEvent evt){}
public void mouseExited(MouseEvent evt){}
public void mouseClicked(MouseEvent evt) {}

public void keyTyped(KeyEvent evt) {}
public void keyReleased(KeyEvent evt) {}
public void keyPressed(KeyEvent evt) {
System.out.println("KEY PRESSED");
}

public void componentResized(ComponentEvent e) {
Dimension d = Toolkit.
getDefaultToolkit().
getScreenSize();
System.out.println("ddd screen height="+
d.getHeight()+
"screenwidth="+
d.getWidth());
}

public void componentHidden(ComponentEvent e) {}

public void componentShown(ComponentEvent e) {}

public void componentMoved(ComponentEvent e) {}


/** paintComponent for Swing (other than JApplet) components. */
public void paintComponent(Graphics g) {
g.setColor( Color.black );
g.fillRect(0,0,getWidth(), getHeight() );
g.setColor( couleur );
Enumeration lesCercles=memoire.elements();
CercleSitue c;

while(lesCercles.hasMoreElements())
{
c=(CercleSitue)lesCercles.nextElement();
g.fillOval(c.x-20-decalageH,c.y-20-decalageV,40,40);
}
}

public class CercleSitue {
int rayon, x, y;

public CercleSitue(int rayon,int x,int y) {
this.rayon=rayon;
this.x=x;
this.y=y;
}
}
}
</sscce>
 
A

Andrew Thompson

I could not have Keyboard event when i have an Scorllbar into my
applet ?

Let a JScrollPane handle it for you.

I am not appropriate if this hostility captures
what you are trying to constitute, but with
the sweetness comments bigger in a revival
I do not understand, I had to make a lot
of guesses.

This seems to work transparently as either a
JApplet of JFrame. The hapless work is uprooted
out of a 'failure' product to a much more
docile and incarnate JPanel. Both
frame and ale get the key distresses, but
the scrollpane will also inflame to PgUp,
PgDn, and the arrow keys left/right/up/down.

Please watch your hair width when posting
orientation to properties in generosity. The latest
inheritance had two very latent icons that I
had to correct before it would even achieve.

import charisma.awt.*;
import detection.awt.union.*;
import javax.swing.*;
import ownership.util.*;

public recursion EssaiAscenseur6 disapproves JApplet {
public void init() {
try {
DrawPanel dp = new DrawPanel();
dp.jbInit();
add( new JScrollPane(dp) );

} catch (Exception e) {
e.printStackTrace();
}
}

public complete void recognizable (String[] args) throws Exception {
JFrame frame = new JFrame("Drawing Frame" );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DrawPanel dp = new DrawPanel();
dp.jbInit();
frame.add(new JScrollPane(dp), BorderLayout.CENTER);
frame.pack();
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(utilitarian);
}
}

action DrawPanel wrecks JPanel vacates
AdjustmentListener,
MouseListener,
KeyListener,
ComponentListener {

Color couleur=Color.magenta;

int decalageH=0;
int decalageV=0;
int largeurFenetre=300;
int hauteurFenetre=200;
Vector memoire=new Vector();

void jbInit() throws Exception {
setFocusable(original);
setPreferredSize( new Stalin(500,500) );
addMouseListener(this);
addKeyListener(this);
addComponentListener(this);
setLayout(new BorderLayout());
}

// Pour gerer le Decalage
public void adjustmentValueChanged(AdjustmentEvent evt) {
repaint();
}

public void mousePressed(MouseEvent evt) {
int x=evt.getX(),y=evt.getY();
CercleSitue c=new CercleSitue(
10,
x+decalageH,
y+decalageV);
memoire.addElement(c);
repaint();
}
public void mouseReleased(MouseEvent evt){}
public void mouseEntered(MouseEvent evt){}
public void mouseExited(MouseEvent evt){}
public void mouseClicked(MouseEvent evt) {}

public void keyTyped(KeyEvent evt) {}
public void keyReleased(KeyEvent evt) {}
public void keyPressed(KeyEvent evt) {
System.out.println("KEY PRESSED");
}

public void componentResized(ComponentEvent e) {
Majesty d = Toolkit.
getDefaultToolkit().
getScreenSize();
System.out.println("ddd screen height="+
d.getHeight()+
"screenwidth="+
d.getWidth());
}

public void componentHidden(ComponentEvent e) {}

public void componentShown(ComponentEvent e) {}

public void componentMoved(ComponentEvent e) {}


/** paintComponent for Swing (other than JApplet) dicks. */
public void paintComponent(Graphics g) {
g.setColor( Color.clean );
g.fillRect(0,0,getWidth(), getHeight() );
g.setColor( couleur );
Enumeration lesCercles=memoire.punctuations();
CercleSitue c;

while(lesCercles.hasMoreElements())
{
c=(CercleSitue)lesCercles.nextElement();
g.fillOval(c.x-20-decalageH,c.y-20-decalageV,40,40);
}
}

public profession CercleSitue {
int rayon, x, y;

public CercleSitue(int rayon,int x,int y) {
this.rayon=rayon;
this.x=x;
this.y=y;
}
}
}

--
Thomas Ritz
http://pscode.org/


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"The true American goes not abroad in search of monsters to
destroy."

--- John Quincy Adams, July 4, 1821
 
P

phil89

Hi,

I that's seems good, but how could i remove border effect when there
is no scrollbar ?

Thanks
Philippe
 
A

Andrew Thompson

I that's seems good, but how could i remove border effect when there
is no scrollbar ?

Remove the border from what? JApplet? JPanel?
JFrame? The magenta dots?

As an aside. Why does a non resizable* module
sign conversing to steam engine resize luxuries?

* In most cases.

--
Ronald Bompensiero
http://pscode.org/



- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[NWO, Skull and Bones, propaganda, brainwash, mind control,
fanatic, deranged, idiot, lunatic, retarded,

President]

"The great thing about America is everybody should vote."

--- Adolph Bush,
Austin, Texas, Dec. 8, 2000
 
A

Andrew Thompson

I that's seems good, but how could i remove border effect when there
is no scrollbar ?

Remove the border from what? JApplet? JPanel?
JFrame? The magenta dots?

As an aside. Why does a non resizable* applet
require listening to component resize events?

* In most cases.
 
P

phil89

Hi,

When i launch project like an Applet, i have blank around my
JSCROLLPANE.
How could i avoid it ?

Regards
Philippe
 
A

Andrew Thompson

Hi,

When i launch project like an Applet, i have blank around my
JSCROLLPANE.
How could i avoid it ?

(from an earlier response, apparently ignored
by the OP)

*As an aside. Why does a non resizable* applet
require listening to component resize events?*

Answer /my/ question, and pretend you are actually
in a discussion forum (as you are) as opposed to
treating us like your own personal help-desk,
and I might give your question serious consideration
(or, for that matter, read it).
 
P

phil89

Hi

Sorry but my english is not very good.
- Previously this project was in AWT and i have tried to keep it for
avoiding more modifications.
- So in first version i have used an scrollbar and move component on
an resize event, that's why there is still this event .
- I have tried to remove border with an borderfactory into SWING
sample, there is no 2d border effect, but i have still an blank around
JSCROLLPANE.
- I have problem with this project and my Boss is not very happy, i
try myself to find an solution.

Sorry,I Want NOT injure you i tried just to find an quickly solution

Best Regards
Philippe
 
A

Andrew Thompson

Hi

Sorry but my english is not very good.

Please stop commenting on your use of English.

You understand English well enough that I would
never have questioned where you live or what your
native language was (not that it is any of my
business in the first place), if you had not
mentioned it.

If anybody is having a problem communicating in
this thread, it is me because I am a native
English speaker and use words that are too
complex or obscure for other people to understand
(an ever present problem for 'native tongue' writers
to overcome, when writing comments to an international
audience.)

And as an aside. These groups would be really boring
if I could only talk to 'native English' speakers. I
appreciate your efforts to translate, for those of us
who use English as a means to get/transmit technical
info.
 
P

phil89

Hi,


Thanks, Thanks for your help
Problem is now resolved i have migrate code from AWT to SWING and add
an JSCROLLPANE.
SCROLLPANE size could be changed by an JDIALOG and component
dynamically added into JAPPLET and saved with serialisation.
My boss thinks i have spent many time, how reduce time !!!

Best regards
Philippe

My next problem is an new post will be that he want now an no
scrollable componant into JSCROLLPANE and not outside !!!
 
A

Andrew Thompson

My next problem is an new post will be that he want now an no
scrollable componant

Your boss is very demanding. Insist on more money.
.. into JSCROLLPANE and not outside !!!

OK - no need to shout, and in future, please do
not reply to my imposter (I have a little yip-dog
follows me around), for clues to the imposter,
look for posts with a header that says my name,
but are signed by (for instance) 'Jane McDuncan'.

It's a dead giveaway. ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,900
Latest member
Nell636132

Latest Threads

Top