I have this class that create a JFrame to display some messages.
I don't know why if I call displayFile(String myString) from one class,
it call authomatically the constructor MessageViewer() to initialize
the object into JFrame; while if I call it from another class (in the
same way) It don't call the constructor and so I receive
nullPoiterException about textArea.
I don't know what change.
Can you help me?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/*
* Questa classe estende JFrame e costruisce la finestra in cui e'
possibile
* visualizzare i messaggi impacchettati e spediti
*/
public class MessageViewer extends JFrame {
static final long serialVersionUID = 1L;
//Contenitore principale
JFrame frame;
//Area di testo
public static JTextArea textArea;
//Font usato nella textArea
Font textFont;
//Scroll del pannello
JScrollPane scrollPane;
/*
* Questo metodo e' il costruttore della classe ed il responsabile
della
* inizializzazione degli oggetti contenuti nel JFrame.
*/
public MessageViewer() {
//Setta il nome del Frame
super("MessageViewer");
JFrame frame = new JFrame("FrameDemo");
// Definisce cosa fera quando si chiude il frame Frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
//setta il font
textFont = new Font("Courier", Font.PLAIN, 14);
//Setta alcuni parametri della textArea in cui mostrare i messaggi.
textArea = new JTextArea(20, 35);
textArea.setEnabled(false);
textArea.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setBackground(Color.white);
textArea.setDisabledTextColor(Color.black);
textArea.setFont(textFont);
//mette la textarea in un pannello scorrevole.
scrollPane = new JScrollPane(textArea);
scrollPane
.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//Mette JScrollPane e JTextArea nel Jframe
getContentPane().add(scrollPane, BorderLayout.CENTER);
}
/*
* Questo metodo mostra i messaggi nella finestra
*/
public static void displayFile(String myString) {
// Mette i messaggi nella textarea. Concatena "\n" per andare a capo
textArea.append(myString + "\n" + "\n");
//Setta la posizione del cursore alla testa del file
textArea.setCaretPosition(0);
textArea.grabFocus();
} //Fine metodo displayFile()
} //Fine classe Messageviewer
palmis - 01 Feb 2006 08:32 GMT
I call displayFile(String myString) in this way:
MessageViewer.displayFile(String myString).
Ranganath Kini - 01 Feb 2006 09:18 GMT
Maybe u are not instantiating your MessageViewer class before invoking
its displayFile() method.
If you want to call the method, first instantiate the MessageViewer
class like this:
MessageViewer myViewer = new MessageViewer();
// Now call the displayFile() method
myViewer.displayFile( "pass the text here" );
palmis - 01 Feb 2006 10:27 GMT
If I make this in the class that call construstor authomatically, it
create two Jframe.
Stefan Schulz - 01 Feb 2006 12:28 GMT
> public class MessageViewer extends JFrame {
>
> static final long serialVersionUID = 1L;
>
> //Contenitore principale
> JFrame frame;
Remove this static
> //Area di testo
> public static JTextArea textArea;
[...]
Remove this static
> public static void displayFile(String myString) {
>
[quoted text clipped - 7 lines]
>
> } //Fine classe Messageviewer
Use by creating an instance of MessageViewer (with new) and then call
displayFile on that instance.
IchBin - 01 Feb 2006 16:23 GMT
> I have this class that create a JFrame to display some messages.
>
[quoted text clipped - 5 lines]
> I don't know what change.
> Can you help me?
Do not need to extend JFrame
public class MessageViewer {
> public class MessageViewer extends JFrame {
delete 'final'
public JTextArea textArea;
> public static JTextArea textArea;
delete this
> super("MessageViewer");
Move super Title to here
> JFrame frame = new JFrame("MessageViewer");
add getContentPane() of frame
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
> getContentPane().add(scrollPane, BorderLayout.CENTER);
delete 'final'
public void displayFile(String myString) {
> public static void displayFile(String myString)
Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
palmis - 02 Feb 2006 12:09 GMT
thank you very much
Palmis