GUI inquiry

A

a

Dear all,

I'm new to JAVA GUI and hope you could help on 2 questions.

1) No matter how I constrain textarea (e.g. set column, row), when it is fed
with a long string, the textarea just shows 2 rows and many columns. Is
there any trick to force the swapping of line? I've tried both textarea and
jtextarea.



2) I'm modifying the mouse listening codes written by somebody else before:


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

public class Listener implements ActionListener
{
private BioProject bioProject;

public Listener(BioProject bioProject)
{
this.bioProject = bioProject;
}

public void actionPerformed(ActionEvent e)
{
JButton jButton = (JButton) e.getSource();

if(jButton == bioProject.getFileButton())
{
FileDialog fileDialog = new FileDialog(bioProject.getJFrame(), "Select
the source file" , FileDialog.LOAD);
fileDialog.show();

if(fileDialog.getFile() != null)
bioProject.readFile( fileDialog.getDirectory() + fileDialog.getFile());
}
else if(jButton == bioProject.getPrevious10Button())
bioProject.previous10();
else if
.....


}

}

and then "duplicate" one for key listening, but it does not respond at all
( System.out.print("up") doesn't print anything ). I've already added
addlistener at the bioproject.java, say at jframe, contentpane, display
panel but none of them works. Is that listening unable to work on both mouse
and key events?

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

public class KListener implements KeyListener {

private BioProject bioProject;

public KListener(BioProject bioProject)
{
// super();
this.bioProject = bioProject;
}

public void keyPressed(KeyEvent e) {
System.out.print("up");


int Key = e.getKeyCode();
if(Key == KeyEvent.VK_UP) {
System.out.print("up");
bioProject.up();

} else if(Key == KeyEvent.VK_DOWN) {
System.out.print("dn");
bioProject.down();
} else if(Key == KeyEvent.VK_PAGE_UP) {
System.out.print("pgup");
bioProject.previous();
} else if(Key == KeyEvent.VK_PAGE_DOWN) {
System.out.print("pgdn");
bioProject.next();
}
}

public void keyTyped(KeyEvent e) {
System.out.print("KEY TYPED: ");
}

/** Handle the key released event from the text field. */
public void keyReleased(KeyEvent e) {
System.out.print("KEY RELEASED: ");
}
}
 
A

Andrew Thompson

a wrote:
....
I'm new to JAVA GUI and hope you could help on 2 questions.

Note that two GUI questions are better placed
on two separate threads with two separate (and
meaningful) titles - most profitably posted to
comp.lang.java.gui.
1) No matter how I constrain textarea (e.g. set column, row), when it is fed
with a long string, the textarea just shows 2 rows and many columns. Is
there any trick to force the swapping of line? I've tried both textarea and
jtextarea.

I am unfamilir with those classes. Do you mean
(java.awt.)TextArea and (javax.swing.)JTextArea?
If so, please be specific and use the correct
capitalisation, as it helps avoid confusion.

Assuming you are referring to the J2SE core
GUI classes, decide whether this GUI is being
built using AWT or Swing and stick with it.
It is unwise to mix the two.

Assuming Swing.
<sscce>
import javax.swing.*;

class TestTextAreaSize {
public static void main(String[] args) {
JTextArea ta = new JTextArea(10,5);
ta.setLineWrap(true);
ta.setWrapStyleWord(true);
JScrollPane sp = new JScrollPane(ta);
JOptionPane.showMessageDialog(null, sp);
}
}
</sscce>

Andrew T.
 
H

hiwa

a said:
Dear all,

I'm new to JAVA GUI and hope you could help on 2 questions.

1) No matter how I constrain textarea (e.g. set column, row), when it is fed
with a long string, the textarea just shows 2 rows and many columns. Is
there any trick to force the swapping of line? I've tried both textarea and
jtextarea.



2) I'm modifying the mouse listening codes written by somebody else before:


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

public class Listener implements ActionListener
{
private BioProject bioProject;

public Listener(BioProject bioProject)
{
this.bioProject = bioProject;
}

public void actionPerformed(ActionEvent e)
{
JButton jButton = (JButton) e.getSource();

if(jButton == bioProject.getFileButton())
{
FileDialog fileDialog = new FileDialog(bioProject.getJFrame(), "Select
the source file" , FileDialog.LOAD);
fileDialog.show();

if(fileDialog.getFile() != null)
bioProject.readFile( fileDialog.getDirectory() + fileDialog.getFile());
}
else if(jButton == bioProject.getPrevious10Button())
bioProject.previous10();
else if
....


}

}

and then "duplicate" one for key listening, but it does not respond at all
( System.out.print("up") doesn't print anything ). I've already added
addlistener at the bioproject.java, say at jframe, contentpane, display
panel but none of them works. Is that listening unable to work on both mouse
and key events?

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

public class KListener implements KeyListener {

private BioProject bioProject;

public KListener(BioProject bioProject)
{
// super();
this.bioProject = bioProject;
}

public void keyPressed(KeyEvent e) {
System.out.print("up");


int Key = e.getKeyCode();
if(Key == KeyEvent.VK_UP) {
System.out.print("up");
bioProject.up();

} else if(Key == KeyEvent.VK_DOWN) {
System.out.print("dn");
bioProject.down();
} else if(Key == KeyEvent.VK_PAGE_UP) {
System.out.print("pgup");
bioProject.previous();
} else if(Key == KeyEvent.VK_PAGE_DOWN) {
System.out.print("pgdn");
bioProject.next();
}
}

public void keyTyped(KeyEvent e) {
System.out.print("KEY TYPED: ");
}

/** Handle the key released event from the text field. */
public void keyReleased(KeyEvent e) {
System.out.print("KEY RELEASED: ");
}
}
For 1), you should read the API documentation of JTextArea class. You
will find relevant methods there. For 2), you need to make a GUI
component on which you press keys. Your key listener should be 'added'
to the component for it to be working.
 
A

a

Assuming Swing.
<sscce>
import javax.swing.*;

class TestTextAreaSize {
public static void main(String[] args) {
JTextArea ta = new JTextArea(10,5);
ta.setLineWrap(true);
ta.setWrapStyleWord(true);
JScrollPane sp = new JScrollPane(ta);
JOptionPane.showMessageDialog(null, sp);
}
}
</sscce>

Andrew T.

setLineWrap(true) solves the problem but the speed of the program for
printing out 5000 characters has dropped tremendously. TextArea doesn't have
the wrap function.
 
A

a

For 2), you need to make a GUI
component on which you press keys. Your key listener should be 'added'
to the component for it to be working.

I've already added addlistener at the bioproject.java, say at jframe,
contentpane, display
panel but none of them works.
 
H

hiwa

a said:
I've already added addlistener at the bioproject.java, say at jframe,
contentpane, display
panel but none of them works.
Sorry, we don't see them on your code...
 
A

a

hiwa said:
Sorry, we don't see them on your code...

Dear Hiwa,

Thanks for your follow-up. The code excerpts are:

private Listener listener;
private KListener Klistener;

listener = new Listener(this);
Klistener = new KListener(this);

fileButton.setBounds(new Rectangle(880,10,100,35));
fileButton.addActionListener(listener);
//fileButton.addActionListener(Klistener);

previous10Button.setBounds(new Rectangle(877,100-voffset,35,35));
previous10Button.addActionListener(listener);

// displayPanel.addKeyListener(Klistener);
// jFrame.getContentPane().addKeyListener(Klistener);
jFrame.addKeyListener(Klistener);
 
H

hiwa

a said:
Dear Hiwa,

Thanks for your follow-up. The code excerpts are:

private Listener listener;
private KListener Klistener;

listener = new Listener(this);
Klistener = new KListener(this);

fileButton.setBounds(new Rectangle(880,10,100,35));
fileButton.addActionListener(listener);
//fileButton.addActionListener(Klistener);

previous10Button.setBounds(new Rectangle(877,100-voffset,35,35));
previous10Button.addActionListener(listener);

// displayPanel.addKeyListener(Klistener);
// jFrame.getContentPane().addKeyListener(Klistener);
jFrame.addKeyListener(Klistener);
Then, if your key listener is properly written and your JFrame has the
key focus, the thing should happen as expected.
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top