Constructor class

P

palmis

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
 
P

palmis

I call displayFile(String myString) in this way:

MessageViewer.displayFile(String myString).
 
R

Ranganath Kini

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" );
 
P

palmis

If I make this in the class that call construstor authomatically, it
create two Jframe.
 
S

Stefan Schulz

palmis wrote:

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) {

// 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

Use by creating an instance of MessageViewer (with new) and then call
displayFile on that instance.
 
I

IchBin

palmis said:
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?

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-)
 

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,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top