Java : JTextArea can not append() - java

package my.test;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JScrollPane;
public class ChatP2P extends javax.swing.JFrame {
ObjectOutputStream out = null;
ObjectInputStream in = null;
String name = LoginInterface.id;
private final static String newline = "\n";
String message = null;
/**
* Creates new form ChatP2P
*/
public ChatP2P(Socket socket) throws IOException, ClassNotFoundException {
initComponents();
JScrollPane scrollPane = new JScrollPane(text);
text.setLineWrap(true);
chat.setLineWrap(true);
out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream());
new Thread(new receiveThread(message, in, text)).start();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jMenuItem1 = new javax.swing.JMenuItem();
jScrollPane1 = new javax.swing.JScrollPane();
text = new javax.swing.JTextArea();
nameField = new javax.swing.JTextField();
jScrollPane3 = new javax.swing.JScrollPane();
chat = new javax.swing.JTextArea();
sendButton = new javax.swing.JButton();
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane2.setViewportView(jTextArea1);
jMenuItem1.setText("jMenuItem1");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
text.setEditable(false);
text.setColumns(20);
text.setRows(5);
jScrollPane1.setViewportView(text);
nameField.setEditable(false);
chat.setColumns(20);
chat.setRows(5);
jScrollPane3.setViewportView(chat);
sendButton.setText("SEND");
sendButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sendButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(nameField)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(sendButton, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(1, 1, 1)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(sendButton, javax.swing.GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE)
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)))
);
pack();
}// </editor-fold>
private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {
if (!chat.getText().equals("")){
try {
String temp = name + " : " + chat.getText();
out.writeObject(temp);
text.append(temp + newline);
chat.setText("");
} catch (IOException ex) {
Logger.getLogger(ChatP2P.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
/**
* #param args the command line arguments
*/
// Variables declaration - do not modify
private javax.swing.JTextArea chat;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JTextArea jTextArea1;
public javax.swing.JTextField nameField;
private javax.swing.JButton sendButton;
public javax.swing.JTextArea text;
// End of variables declaration
}
So I have a SEND button, a CHAT TextArea for user to input the message and a TEXT TextArea to display the message
but when I typed a messsage and clicked on the SEND button,the TEXT didn't append that new message,but it cleared the message in CHAT. That means the code walks through the line :
text.append(temp + newline);
in the
sendButtonActionPerformed
but nothing is displayed. So what's wrong here ?

Move statement JScrollPane scrollPane = new JScrollPane(text); before pack() and it will be ok. Like this:
jScrollPane1 = new javax.swing.JScrollPane();
text = new javax.swing.JTextArea();
JScrollPane scrollPane = new JScrollPane(text);

Related

How to read file text to JList

I have a question, how can I import existed array string to jlist for each element in array, I know a lot of you guy asked it before, but mostly, I saw that how to do it with a jButton, I also heard about
DefaultListModel but it did't work
private void JListAction(java.awt.event.ActionEvent evt) {
DefaultListModel listModel = new DefaultListModel();
for (int i = 0; i < dm.getSize(); i++) {
listModel.addElement(dic.newWord.get(i).getWordExplain());
}
jList2.setModel(listModel);
}
Here is my code from GUI only
[import java.awt.Color;
import java.awt.Font;
import javax.swing.JTextArea;
import javax.swing.DefaultListModel;
import javax.swing.DefaultListSelectionModel;
import javax.swing.JList;
import javax.swing.JScrollPane;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* #author Quốc Khánh
*/
public class GUI extends javax.swing.JFrame {
DictionaryCommandline dcl = new DictionaryCommandline();
DictionaryManagement dm = new DictionaryManagement();
Dictionary dic = new Dictionary();
DictionaryApplication dicA = new DictionaryApplication();
private Object textArea;
Font font = new Font("consolas", Font.BOLD, 18);
/**
* Creates new form GUI
*/
public GUI() {
setTitle("từ điển Anh Việt v1.0");
dm.insertFromFile(dic);
initComponents();
jTextPane1.setFont(font);
jTextArea1.setFont(font);
jTextArea1.setLineWrap(true);
jTextArea1.setForeground(Color.BLACK);
jButton1.setFont(font);
JScrollPane pane=new JScrollPane(jList2);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTextPane1 = new javax.swing.JTextPane();
jButton1 = new javax.swing.JButton();
jScrollPane3 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jScrollPane4 = new javax.swing.JScrollPane();
jList2 = new javax.swing.JList<>();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jScrollPane1.setViewportView(jTextPane1);
jButton1.setText("translate");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane3.setViewportView(jTextArea1);
jScrollPane4.setViewportView(jList2);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(22, 22, 22)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jScrollPane1)
.addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE))
.addGap(38, 38, 38)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(54, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(37, 37, 37)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jScrollPane1)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE))
.addGap(19, 19, 19)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jScrollPane3)
.addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE))
.addContainerGap(67, Short.MAX_VALUE))
);
jButton1.getAccessibleContext().setAccessibleName("translate");
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String word = jTextPane1.getText();
String mean = dicA.getWord(dic, dm.getSize(), dm, word);
jTextArea1.setText(dicA.getWordEx(dic, dm.getSize(), dm, mean));
}
private void JListAction(java.awt.event.ActionEvent evt) {
DefaultListModel listModel = new DefaultListModel();
for (int i = 0; i < dm.getSize(); i++) {
listModel.addElement(dic.newWord.get(i).getWordExplain());
}
jList2.setModel(listModel);
}
/**
* #param args the command line arguments
*/
public static void main(String\[\] args) {
java.awt.EventQueue.invokeLater(() -> {
new GUI().setVisible(true);
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JList<String> jList2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JScrollPane jScrollPane4;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextPane jTextPane1;
// End of variables declaration
/**
*
*/
}
Is your Array of Strings already available, when the GUI is generated? If so, you can insert it directly into your JList by using it as a parameter.
Instead of using JList jList2 = new javax.swing.JList<>(); (as it is done in line 5 of your initComponent() method) you'd put your String[] into the brackets:
String[] data = {"a","b","c"};
JList jList2 = new javax.swing.JList<>(data);
JScrollPane pane=new JScrollPane(jList2);
In this case your ScrollPane would look something like this

Rdf and java gui

I have a job to do and I want to display results from this program in my GUI but I just can not do it, then I want a method for allowing me to see the results of this program in my graph inetreface, so I created an interface and I reserved a button displays and an area for display (JTextArea), it means when I click on button shows me it shows the results (the contents of my Rdf file). I want your help if possible and thank you in advance
Model model = ModelFactory.createDefaultModel();
// use the FileManager to find the input file
InputStream in = (InputStream) FileManager.get().open("C:/Users/SAMSUNG/Desktop/WICM2/projet/opus_august2007.rdf");
if (in == null) {
throw new IllegalArgumentException(
"File: " + "C:/Users/SAMSUNG/Desktop/WICM2/projet/opus_august2007.rdf"+ " not found");
}
// read the RDF/XML file
model.read(in, "");
// write it to standard out
//model.write(System.out);
model.write(System.out);
and here's my program interface
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.NodeIterator;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.util.FileManager;
/**
*
* #author SAMSUNG
*/
public class frameFinal extends javax.swing.JFrame {
/**
* Creates new form FrameFinal
*/
public frameFinal() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jTabbedPane1 = new javax.swing.JTabbedPane();
jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jTextField3 = new javax.swing.JTextField();
jPanel3 = new javax.swing.JPanel();
jTextArea2 = new javax.swing.JTextArea();
jButton3 = new javax.swing.JButton();
jPanel4 = new javax.swing.JPanel();
jButton4 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenu2 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Rdf & taxonomie");
jPanel1.setBackground(new java.awt.Color(204, 204, 255));
jButton1.setText("PARCOURIR");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTextField3.setBackground(new java.awt.Color(204, 204, 204));
jTextField3.setEnabled(false);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap(53, Short.MAX_VALUE)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 327, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton1)
.addGap(79, 79, 79))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(120, 120, 120)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(157, Short.MAX_VALUE))
);
jTabbedPane1.addTab("Chargement File", jPanel1);
jButton3.setText("Visualise");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jTabbedPane1.addTab("Visualiser", jPanel3);
jButton4.setText("Generer");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jTextArea2.setColumns(20);
jTextArea2.setRows(5);
jScrollPane2.setViewportView(jTextArea2);
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
.addContainerGap(477, Short.MAX_VALUE)
.addComponent(jButton3)
.addGap(20, 20, 20))
.addComponent(jScrollPane2)
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 271, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(jButton3))
);
javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
jPanel4.setLayout(jPanel4Layout);
jPanel4Layout.setHorizontalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createSequentialGroup()
.addGap(0, 489, Short.MAX_VALUE)
.addComponent(jButton4)
.addContainerGap())
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 570, Short.MAX_VALUE)
);
jPanel4Layout.setVerticalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton4)
.addGap(0, 25, Short.MAX_VALUE))
);
jTabbedPane1.addTab("Taxonomie", jPanel4);
jMenu1.setText("File");
jMenuBar1.add(jMenu1);
jMenu2.setText("Edit");
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTabbedPane1)
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new java.io.File("C:\\Users\\SAMSUNG\\Desktop\\WICM2\\projet"));
fileChooser.setFileFilter(new FileNameExtensionFilter("RDF Definitions", "rdf", "RDF"));
int returnVal = fileChooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
this.jTextField3.setText(fileChooser.getSelectedFile().getPath());
}//GEN-LAST:event_jButton1ActionPerformed
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTextField1ActionPerformed
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
jTextArea1.setText("");
Model model = ModelFactory.createDefaultModel();
InputStream in = FileManager.get().open("C:/Users/SAMSUNG/Desktop/WICM2/projet/opus_august2007.rdf");
Model m = model.read(in,null);
NodeIterator nit = m.listObjects();
List<String> lclass = new ArrayList<>();
Map<String,List<String>> map = new HashMap<>();
StmtIterator si = m.listStatements();
while(si.hasNext()){
Statement statement = si.next();
if(statement.getPredicate().getLocalName().equalsIgnoreCase("subClassOf")){
String mere = statement.getObject().toString();
String fils = statement.getSubject().getLocalName();
if(map.containsKey(mere)){
List<String> l = map.get(mere);
l.add(fils);
map.replace(mere, l);
}else{
List<String> l = new ArrayList<>();
l.add(fils);
map.put(mere, l);
}
}
if(statement.getObject().isResource() && statement.getPredicate().getLocalName().equalsIgnoreCase("type")){
if(!lclass.contains(statement.getSubject().getLocalName())){
lclass.add(statement.getSubject().getLocalName()) ;
}
}
}
jTextArea1.append("Les classes:\n");
for(String classe:lclass){
jTextArea1.append(classe+"\n");
}
jTextArea1.append("Les classe et les sous classes:\n");
for(String l : map.keySet()){
jTextArea1.append("Classe mere : "+l+"\n");
for(String s:map.get(l)){
jTextArea1.append(" Classe fils:"+s+"\n");
}
}
}//GEN-LAST:event_jButton4ActionPerformed
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
/*jTextArea2.setText("");
Model model = ModelFactory.createDefaultModel();
// use the FileManager to find the input file
InputStream in = FileManager.get().open("C:/Users/SAMSUNG/Desktop/WICM2/projet/opus_august2007.rdf");
// DataInputStrean st = new DataInputStream() ;
if (in == null) {
throw new IllegalArgumentException(
"File: " + "C:/Users/SAMSUNG/Desktop/WICM2/projet/opus_august2007.rdf"+ " not found");
}
// read the RDF/XML file
model.read(in, "");
// write it to standard out
//model.write(System.out);
model.write(System.out);*/
}
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(frameFinal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(frameFinal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(frameFinal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(frameFinal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new frameFinal().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTabbedPane jTabbedPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextArea jTextArea2;
private javax.swing.JTextField jTextField3;
// End of variables declaration//GEN-END:variables
}
If I understand you want to display entire RDF file in a text area. So if it's that what you want, you just need to read it in using a reader something like...
try{
FileReader f = new FileReader("c:\\opus_august2007.rdf");
BufferedReader b = new BufferedReader(f);
boolean eof = false;
while(! eof)
{
String lineIn = b.readLine();
if(lineIn == null)
{
eof = true;
}
else
{
mytxtarea.append(lineIn + System.getProperty("line.separator"));
}
}
}catch(Exception e){
e.printStackTrace();
}

jLabel won't print from String

I'm trying to populate a jLabel with text from a global String in NetBeans, but every time I try to print anything other than a String literal, it prints nothing. I've tried directly printing the String, and my most recent attempt used a get() method.
Here's the code so far, I'm populating the label on line 73:
public class ChatWindow extends javax.swing.JFrame {
private Integer userID;
private String otherPerson = "";
/**
* Creates new form ChatWindow
* #param id
*/
public ChatWindow(int id) {
initComponents();
userID = id;
otherPerson = userID.toString();
this.setVisible(true);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
otherPersonLabel = new javax.swing.JLabel();
menuBar = new javax.swing.JMenuBar();
fileMenu = new javax.swing.JMenu();
openMenuItem = new javax.swing.JMenuItem();
saveMenuItem = new javax.swing.JMenuItem();
saveAsMenuItem = new javax.swing.JMenuItem();
exitMenuItem = new javax.swing.JMenuItem();
editMenu = new javax.swing.JMenu();
cutMenuItem = new javax.swing.JMenuItem();
copyMenuItem = new javax.swing.JMenuItem();
pasteMenuItem = new javax.swing.JMenuItem();
deleteMenuItem = new javax.swing.JMenuItem();
helpMenu = new javax.swing.JMenu();
contentsMenuItem = new javax.swing.JMenuItem();
aboutMenuItem = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
jTextArea1.setBackground(new java.awt.Color(248, 248, 248));
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jTextArea1.setText("ContactName: FloopyNoops\nYou: Yes, floopynoops");
jScrollPane1.setViewportView(jTextArea1);
jButton1.setText("Send");
jButton2.setText("Save");
jTextField1.setText("jTextField1");
otherPersonLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
otherPersonLabel.setText(getLabelText());
fileMenu.setMnemonic('f');
fileMenu.setText("File");
openMenuItem.setMnemonic('o');
openMenuItem.setText("Open");
fileMenu.add(openMenuItem);
saveMenuItem.setMnemonic('s');
saveMenuItem.setText("Save");
fileMenu.add(saveMenuItem);
saveAsMenuItem.setMnemonic('a');
saveAsMenuItem.setText("Save As ...");
saveAsMenuItem.setDisplayedMnemonicIndex(5);
fileMenu.add(saveAsMenuItem);
exitMenuItem.setMnemonic('x');
exitMenuItem.setText("Exit");
exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitMenuItemActionPerformed(evt);
}
});
fileMenu.add(exitMenuItem);
menuBar.add(fileMenu);
editMenu.setMnemonic('e');
editMenu.setText("Edit");
cutMenuItem.setMnemonic('t');
cutMenuItem.setText("Cut");
editMenu.add(cutMenuItem);
copyMenuItem.setMnemonic('y');
copyMenuItem.setText("Copy");
editMenu.add(copyMenuItem);
pasteMenuItem.setMnemonic('p');
pasteMenuItem.setText("Paste");
editMenu.add(pasteMenuItem);
deleteMenuItem.setMnemonic('d');
deleteMenuItem.setText("Delete");
editMenu.add(deleteMenuItem);
menuBar.add(editMenu);
helpMenu.setMnemonic('h');
helpMenu.setText("Help");
contentsMenuItem.setMnemonic('c');
contentsMenuItem.setText("Contents");
helpMenu.add(contentsMenuItem);
aboutMenuItem.setMnemonic('a');
aboutMenuItem.setText("About");
helpMenu.add(aboutMenuItem);
menuBar.add(helpMenu);
setJMenuBar(menuBar);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jTextField1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addComponent(otherPersonLabel)
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(5, 5, 5)
.addComponent(otherPersonLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(6, 6, 6)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
}
private String getLabelText(){
return otherPerson;
}
// Variables declaration - do not modify
private javax.swing.JMenuItem aboutMenuItem;
private javax.swing.JMenuItem contentsMenuItem;
private javax.swing.JMenuItem copyMenuItem;
private javax.swing.JMenuItem cutMenuItem;
private javax.swing.JMenuItem deleteMenuItem;
private javax.swing.JMenu editMenu;
private javax.swing.JMenuItem exitMenuItem;
private javax.swing.JMenu fileMenu;
private javax.swing.JMenu helpMenu;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
private javax.swing.JMenuBar menuBar;
private javax.swing.JMenuItem openMenuItem;
private javax.swing.JLabel otherPersonLabel;
private javax.swing.JMenuItem pasteMenuItem;
private javax.swing.JMenuItem saveAsMenuItem;
private javax.swing.JMenuItem saveMenuItem;
// End of variables declaration
}
I believe the constructor is your issue.
public ChatWindow(int id) {
initComponents(); //otherPerson = "";
userID = id;
otherPerson = userID.toString(); //otherPerson = id;
this.setVisible(true);
}
You initialize the component before otherPerson is set. Move initComponents after setting otherPerson and it should work again.
You are calling "initcomponent" first and then initializing "otherPerson" class member. Do it like this:
public ChatWindow(int id)
{
userID = id;
otherPerson = userID.toString();
initComponents();
this.setVisible(true);
}

Netbeans: string not displaying

I want to display string in textbox of vpsfame class below my code, but I am unable to dispaly it. Can you give me suggestion this string in new8 class show in code and serial class gives me string from gps continue. It also displays it at run time via System.out.println, but does not display it through the textbox of vpsfame class
package vpspro;
import javax.comm.*;
import java.util.*;
import java.io.*;
public class Serial
{
public void com() throws UnsupportedCommOperationException, IOException, TooManyListenersException
{
int c=1;
String wantedPortName="COM6";
Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers();
CommPortIdentifier portId = null;
while(portIdentifiers.hasMoreElements())
{
CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement();
if(pid.getPortType() == CommPortIdentifier.PORT_SERIAL &&
pid.getName().equals(wantedPortName))
{
portId = pid;
break;
}
}
if(portId == null)
{
System.err.println("Could not find serial port " + wantedPortName);
System.exit(1);
}
else
{
System.out.println("system find gps reciever");
}
SerialPort port = null;
try {
port = (SerialPort) portId.open(
"RMC",
1);
System.out.println("all are ok");
} catch(PortInUseException e) {
System.err.println("Port already in use: " + e);
System.exit(1);
}
port.setSerialPortParams(
4800,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
BufferedReader is = null;
try {
is = new BufferedReader(new InputStreamReader(port.getInputStream()));
System.out.println("data is ok");
} catch (IOException e) {
System.err.println("Can't open input stream: write-only");
is = null;
}
String pt=null;
while(true)
{
String st = is.readLine();
System.out.print("(" + c + ")");
c++;
new8 obj1 = new new8();
obj1.decode(st);
System.out.println(st);
st = st.replace(st, "");
}
if (is != null) is.close();
/*if (os != null) os.close();*/
if (port != null) port.close();
}
}
this code for start communication with serial class
package vpspro;
import java.io.IOException;
import java.util.TooManyListenersException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.comm.UnsupportedCommOperationException;
public class getcon extends javax.swing.JFrame {
public getcon() {
initComponents();
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
sc = new javax.swing.JComboBox();
jLabel2 = new javax.swing.JLabel();
br = new javax.swing.JComboBox();
help = new javax.swing.JButton();
ok = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("select comport");
sc.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "COM10", "COM11", "COM12" }));
sc.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
scActionPerformed(evt);
}
});
sc.addInputMethodListener(new java.awt.event.InputMethodListener() {
public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
}
public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
scInputMethodTextChanged(evt);
}
});
jLabel2.setText("select buad rate");
br.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "4800", "1900", "150000" }));
br.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
brActionPerformed(evt);
}
});
help.setText("help");
ok.setText("ok");
ok.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
okActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addGap(67, 67, 67)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(sc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(105, 105, 105))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(171, Short.MAX_VALUE)
.addComponent(ok)
.addGap(28, 28, 28)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(help)
.addComponent(br, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(80, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(57, 57, 57)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(sc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(br, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(57, 57, 57)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(help)
.addComponent(ok))
.addContainerGap(118, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void brActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(evt.getSource()==br)
br.addItem("4800");
br.addItem("9600");
}
private void scActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(evt.getSource()==sc)
sc.addItem("COM6");
sc.addItem("COM7");
}
private void scInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
// TODO add your handling code here:
if(evt.getSource()==sc)
sc.addItem("COM6");
sc.addItem("COM7");
}
private void okActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(evt.getSource()==ok)
{
try {
new Serial().com();
} catch (UnsupportedCommOperationException ex) {
Logger.getLogger(getcon.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(getcon.class.getName()).log(Level.SEVERE, null, ex);
} catch (TooManyListenersException ex) {
Logger.getLogger(getcon.class.getName()).log(Level.SEVERE, null, ex);
}
new new8();
}
}
public void getcom() {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new getcon().setVisible(true);
}
});
}
private javax.swing.JComboBox br;
private javax.swing.JButton help;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
public javax.swing.JButton ok;
private javax.swing.JComboBox sc;
}
this code decoding my gps string and dispaly and hear problem created i can not dispaly below string in textbox when i rum program textbox are empty.
package vpa.io.*;
import vpspro.vpsfame;
public class new8
{
static String[] a2;
static String a1[];
static String c;
public static String[] t;
public void decode(String st)
{
if(st.isEmpty())
{
System.out.println("System has proble");
vpsfame obj=new vpsfame();
obj.ab.setText("ok");
}
else
{
a1=st.split("\\$",0);
a2=a1[1].split("\\*",0);
checksum();
if(a2[1].equalsIgnoreCase(c))
{
t=st.split(",",0);
switch (t[0].charAt(0))
{
case '$':
String s=t[0].substring(3,6);
String s3="RMC";
if (s.compareTo(s3)==0)
{
System.out.println(" NMEA 0183 Details.....");
System.out.println("System identity: "+t[0].substring(1,3));
System.out.println("NMEA Formate: "+t[0].substring(3,6));
System.out.println("(UTC)Time: "+t[1].substring(1,2));
new vpsfame().ac.setText(t[1].substring(1,2));
System.out.println("Lattitude: "+t[3]);
System.out.println("Direction of Lattitude: "+t[4]);
new vpsfame().ab.setText(t[3].concat(t[4]));
System.out.println("Longitude: "+t[5]);
System.out.println("Direction of Longitude: "+t[6]);
new vpsfame().bc.setText(t[5].concat(t[6]));
System.out.println("Speed over Ground: "+t[7]);
new vpsfame().cd.setText(t[7]);
/*System.out.println("Track mode: "+t[8]);
System.out.println(st.isEmpty());
System.out.println("DATE: "+t[9]);
System.out.println("adjustment declination "+t[10]);
System.out.println("Direction of Magnetic variation: "+t[11].charAt(0));*/
}
}
else
System.out.println(" Invalid Formate...");
break;
default:
System.out.println(" Invalid DATA....");
}
}
else
System.out.println(" no match checksum");
}
}
public static void checksum()
{
int l=a2[0].length();
char[] a=a2[0].toCharArray();
int b=0;
for(int i=0;i<l;i++)
{
b=b^a[i];
}
c=Integer.toHexString(b);
}
}
and this is my main class
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* vpsfame.java
*
* Created on Jan 26, 2013, 11:02:32 PM
*/
package vpspro;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Image;
import java.awt.TextField;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.TooManyListenersException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.comm.UnsupportedCommOperationException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
/**
*
* #author DHARMA
*/
public class vpsfame extends javax.swing.JFrame {
public vpsfame()
{
initComponents();
}
/** Creates new form vpsfame */
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jComboBox1 = new javax.swing.JComboBox();
ab = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
bc = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
ac = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
cd = new javax.swing.JTextField();
jPanel1 = new javax.swing.JPanel();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
gc = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
lm = new javax.swing.JMenuItem();
jMenuItem4 = new javax.swing.JMenuItem();
jMenuItem5 = new javax.swing.JMenuItem();
jMenuItem6 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jMenuItem7 = new javax.swing.JMenuItem();
jMenuItem8 = new javax.swing.JMenuItem();
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
ab.setEditable(false);
ab.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
abActionPerformed(evt);
}
});
jLabel1.setText("latitude");
jLabel2.setText("longitude");
bc.setText(" ");
bc.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bcActionPerformed(evt);
}
});
jLabel4.setText("UTC");
ac.setText(" ");
ac.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
acActionPerformed(evt);
}
});
jLabel5.setText("Speed of vessel");
cd.setText(" ");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 621, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 368, Short.MAX_VALUE)
);
jMenu1.setText("File");
gc.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_G, java.awt.event.InputEvent.CTRL_MASK));
gc.setIcon(new javax.swing.ImageIcon("C:\\Users\\DHARMA\\Desktop\\png\\20x20\\55.png")); // NOI18N
gc.setText("get connection");
gc.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
gcActionPerformed(evt);
}
});
jMenu1.add(gc);
jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_T, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem2.setText("track");
jMenu1.add(jMenuItem2);
lm.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_M, java.awt.event.InputEvent.ALT_MASK));
lm.setText("load map");
lm.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
lmActionPerformed(evt);
}
});
jMenu1.add(lm);
jMenuItem4.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem4.setText("data save");
jMenu1.add(jMenuItem4);
jMenuItem5.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_T, java.awt.event.InputEvent.META_MASK));
jMenuItem5.setText("set time zone");
jMenu1.add(jMenuItem5);
jMenuItem6.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem6.setText("quit");
jMenu1.add(jMenuItem6);
jMenuBar1.add(jMenu1);
jMenu2.setText("view");
jMenuItem7.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.ALT_MASK));
jMenuItem7.setText("saved data");
jMenu2.add(jMenuItem7);
jMenuItem8.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.ALT_MASK));
jMenuItem8.setText("about us");
jMenu2.add(jMenuItem8);
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(52, 52, 52)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel5)
.addGap(19, 19, 19))
.addGroup(layout.createSequentialGroup()
.addComponent(cd, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(35, 35, 35))
.addComponent(ab, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
.addComponent(jLabel3)
.addComponent(ac, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
.addComponent(jLabel4)
.addComponent(bc, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
.addComponent(jLabel2)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(11, 11, 11)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(ab, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(43, 43, 43)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel2)
.addGap(16, 16, 16)
.addComponent(bc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
.addComponent(jLabel4)
.addGap(18, 18, 18)
.addComponent(ac, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(45, 45, 45)
.addComponent(jLabel5)
.addGap(18, 18, 18)
.addComponent(cd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
pack();
}// </editor-fold>
private void gcActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(evt.getSource()==gc)
{
getcon obj3=new getcon();
obj3.setVisible(true);
}
}
private void lmActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void abActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void acActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void bcActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* #param args the command line arguments
*/
public static void main(String args[]) throws UnsupportedCommOperationException, IOException, TooManyListenersException {
/*public vpsfame()
{
initComponents();*/
new new8();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
/*URL url=getClass().getResource("C:\\Users\\DHARMA\\Documents\\NetBeansProjects\\vpspro\\src");*/
vpsfame obj4=new vpsfame();
new vpsfame().setBackground(Color.yellow);
obj4.setSize(1000,
500);
ImageIcon image1=new ImageIcon("C:\\Users\\DHARMA\\Documents\\NetBeansProjects\\vpspro\\src");
System.out.println("jjdfhj");
obj4.setIconImage(image1.getImage());
obj4.setTitle("VPS");
obj4.setVisible(true);
new new8();
}
});
}
/*public vpsfame()
{
fm=new JFrame("VPS");
fm.setSize(1000, 1000);
fm.setVisible(true);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}*/
// Variables declaration - do not modify
public static javax.swing.JTextField ab;
public javax.swing.JTextField ac;
public static javax.swing.JTextField bc;
public javax.swing.JTextField cd;
private javax.swing.JMenuItem gc;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem4;
private javax.swing.JMenuItem jMenuItem5;
private javax.swing.JMenuItem jMenuItem6;
private javax.swing.JMenuItem jMenuItem7;
private javax.swing.JMenuItem jMenuItem8;
private javax.swing.JPanel jPanel1;
private javax.swing.JMenuItem lm;
// End of variables declaration
}
I would seriously advise you to take a time out, and refactor all of your code. Igonoring that in the class new8, in method decode replace this code:
new vpsfame().ac.setText(t[1].substring(1,2));
System.out.println("Lattitude: "+t[3]);
System.out.println("Direction of Lattitude: "+t[4]);
new vpsfame().ab.setText(t[3].concat(t[4]));
System.out.println("Longitude: "+t[5]);
System.out.println("Direction of Longitude: "+t[6]);
new vpsfame().bc.setText(t[5].concat(t[6]));
System.out.println("Speed over Ground: "+t[7]);
new vpsfame().cd.setText(t[7]);
With this:
//use one object, dont just create new frames
vpsfame myFrame = new vpsfame();
myFrame.ac.setText(t[1].substring(1,2));
System.out.println("Lattitude: "+t[3]);
System.out.println("Direction of Lattitude: "+t[4]);
myFrame.ab.setText(t[3].concat(t[4]));
System.out.println("Longitude: "+t[5]);
System.out.println("Direction of Longitude: "+t[6]);
myFrame.bc.setText(t[5].concat(t[6]));
System.out.println("Speed over Ground: "+t[7]);
myFrame.cd.setText(t[7]);
//Also set the frame to visible, so it can be displayed once you've done all this work.
myFrame.setVisible(true);
The problem is that:
Firstly, you are creating multiple vpsframe objects where you should only have one. It makes no sense to create 5 frames where each will have 4 empty fields. Create one object and initialise all it's fields rather than using 5 diffrent objects.
You are not tracking the objects you are creating, the garbage collector will just get rid of the, so create one object, assign it to a variable and use that.
Set the vpsfame object to visible, otherwise you will never see it.
Your code also needs refactoring, you should be using the camelCase naming convention, and classes should always begin with Capital letters.

Getting Exception connecting Yahoo server

package session;
import java.io.FileWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.swing.JOptionPane;
import org.openymsg.network.FireEvent;
import org.openymsg.network.Session;
import org.openymsg.network.SessionState;
import org.openymsg.network.event.SessionListener;
public class BotGUI extends javax.swing.JFrame implements SessionListener{
/** Creates new form BotGUI */
FileWriter fw;
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
public BotGUI() {
initComponents();
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
jPanel3 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jPanel4 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
uNameTextField = new javax.swing.JTextField();
uPassPasswordField = new javax.swing.JPasswordField();
jButton1 = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
jMenuItem3 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jPanel3.setBackground(new java.awt.Color(51, 51, 51));
jLabel1.setBackground(new java.awt.Color(0, 0, 255));
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
jLabel1.setForeground(new java.awt.Color(255, 255, 255));
jLabel1.setText("Yahoo Login Panel");
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addGap(38, 38, 38)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(532, Short.MAX_VALUE))
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
);
jPanel2.add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 710, 30));
jPanel4.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jLabel2.setText("Username");
jPanel4.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 20, 60, 20));
jLabel3.setText("Password");
jPanel4.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 20, 60, 20));
jPanel4.add(uNameTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 20, 140, 20));
jPanel4.add(uPassPasswordField, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 20, 140, -1));
jButton1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jButton1.setText("Login");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jPanel4.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 15, 90, -1));
jPanel2.add(jPanel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 30, 710, 60));
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(293, Short.MAX_VALUE))
);
jMenu1.setText("Option");
jMenuItem1.setText("Logout");
jMenu1.add(jMenuItem1);
jMenuItem2.setText("Load CSV");
jMenu1.add(jMenuItem2);
jMenuItem3.setText("Exit");
jMenu1.add(jMenuItem3);
jMenuBar1.add(jMenu1);
jMenu2.setText("Help");
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
public void handleConnectionClosed() {
connectionClosed = true;
loggedIn = false;
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if(!uNameTextField.getText().equals("") && !uPassPasswordField.getText().equals("")){
Yahoo_login(uNameTextField.getText(),uPassPasswordField.getText());
}else{
JOptionPane.showMessageDialog(null, "Plese Enter User Id and Password");
}
}
Session yahooMessengerSession;
MySessionListener mySessionListener;
boolean loggedIn = false;
boolean connectionClosed = false;
public void Yahoo_login(String uName, String pass) {
connectionClosed = false;
if (loggedIn == false) {
yahooMessengerSession = new Session();
mySessionListener = new MySessionListener(this);
yahooMessengerSession.addSessionListener(mySessionListener);
try {
if ((uName.equals("")) || (pass.equals("")))
{
System.out.println("User name/password is blank");
}
else{
//initialized a file writer for log file
System.out.println("Login start........");
yahooMessengerSession.login(uName, pass, true);
//checks whether user was succesful in login in
if (yahooMessengerSession!=null && yahooMessengerSession.getSessionStatus()== SessionState.LOGGED_ON) {
//this loop is reached when the user has been successfully logined
System.out.println("Login Success");
fw.write("User (" + uName + ") logged in at : " + dateFormat.format("09.05.10") + " \n");
fw.close();
} else {
yahooMessengerSession.reset();
}
}
} catch(Exception e){ }
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new BotGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JTextField uNameTextField;
private javax.swing.JPasswordField uPassPasswordField;
// End of variables declaration
public void dispatch(FireEvent fe) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
===========================================================================================
i have to find the error
SEVERE: error during the dispatch of event: FireEvent [org.openymsg.network.event.SessionListEvent to:null from:null message:null timestamp:0 status:0 list type:Friends size:2 LIST]
java.lang.UnsupportedOperationException: Not supported yet.
at yahoomessangerbot.MySessionListener.dispatch(MySessionListener.java:131)
at org.openymsg.network.EventDispatcher.runEventNOW(EventDispatcher.java:133)
at org.openymsg.network.EventDispatcher.run(EventDispatcher.java:114)
http://jymsg9.sourceforge.net/docs/ymsg/network/Session.html
This will help u..

Categories

Resources