View Full Version : Java question
pevergreen
09-21-2010, 03:39
Hi everyone.
Currently trying to do a couple things with java and not having any luck.
I'm trying to grab the values of a couple text fields and write them to a text file (as one entry), then pull them to a j list, and be able to edit and remove single entries (comprising of multiple text fields)
Yeah, i'm guessing its a lot more work than I realise, and may be too much to help me with, if so, just tell me :beam:
pever.
Tellos Athenaios
09-21-2010, 04:09
If you do not need anything fancy then you can slurp the file during startup into the JList directly (notice that this approach blocks on I/O so it is not suitable for a program where the GUI must remain responsive at all times). The following example code will be something to build on: http://download.oracle.com/javase/tutorial/uiswing/examples/components/ListDemoProject/src/components/ListDemo.java
Then add something like this:
import java.io.*;
/* more code */
private final DefaultListModel model;
/* slurp textFile into the list mode */
private void initList(String textFile, String encoding) {
try {
this.model=initList(new DefaultListModel(), textFile, encoding);
}
catch(Exception e) { e.printStackTrace(System.err); }
}
/* slurp text file into a list model: note that to use the default encoding of the host OS you should pass a null.*/
private DefaultListModel initList(DefaultListModel m, String textFile, String encoding) throws Exception {
BufferedReader br;
FileInputStream fis;
try {
// open file stream
fis = new FileInputStream(textFile);
// default to the standard encoding on the OS if not specified:
br = new BufferedReader(encoding == null? new InputStreamReader(fis) : new InputStreamReader(fis, encoding));
for(String s = br.readLine(), s!=null; s = br.readLine()) {
m.addElement(s);
}
return m;
}
finally {
if(fis!=null) { fis.close(); }
if(br!=null) { br.close(); }
}
}
If you need to simply convert multiple text fields -> JList and do edit/remove functions, why not simply concatenate strings and add them to the DefaultListModel, avoiding I/O overhead of unecessary writing to files... ?
pevergreen
09-21-2010, 05:22
If you need to simply convert multiple text fields -> JList and do edit/remove functions, why not simply concatenate strings and add them to the DefaultListModel, avoiding I/O overhead of unecessary writing to files... ?
Hey Tellos, thanks for the super quick reply.
I'm very new to java, so that was still nothing to me. I'll work off what you've given me though, that should be enough.
Thank you very much.
:bow:
Tellos Athenaios
09-21-2010, 15:12
Well concatenating text from textfields is something like this:
private String concat(JTextComponent ... components) {
StringBuilder sb = new StringBuilder();
for(JTextComponent c: components) {
sb.append(c.getText());
}
return sb.toString();
}
pevergreen
09-22-2010, 04:21
Sorry to be a pain, I've gone off on a tangent to the original idea, currently trying to get it removing elements from the list
private void btnremoveActionPerformed(java.awt.event.ActionEvent evt) {
int index = listemail.getSelectedIndex();
listemail.remove(index);
int size = listModel.getSize();
if (size == 0) { //No email records left, disable the button
btnremove.setEnabled(false);
} else { //Select an index.
if (index == listModel.getSize()) {
//removed item in last position
index--;
}
listemail.setSelectedIndex(index);
listemail.ensureIndexIsVisible(index);
}
}
Its throwing an error on listmodel, and again on adding items to the list
private void btnremoveActionPerformed(java.awt.event.ActionEvent evt) {
int index = listemail.getSelectedIndex();
listemail.remove(index);
int size = listModel.getSize();
if (size == 0) { //No email records left, disable the button
btnremove.setEnabled(false);
} else { //Select an index.
if (index == listModel.getSize()) {
//removed item in last position
index--;
}
listemail.setSelectedIndex(index);
listemail.ensureIndexIsVisible(index);
}
}
I grabbed the base code from http://download.oracle.com/javase/tutorial/uiswing/components/list.html#mutable and modified it to fit the form I've got, but I can't figure out what I should be changing listModel to.
Man, why does java have to be so complicated. :laugh4:
Edit: Having moved on, I now can't run my darn program to test the other code I've written (thanks to your first post for the platform though :bow:)
It may work, but it can't find the main class, I think I scouted the problem, but it wants me to declare my public class in its own .java file (using netbeans 6.9)
I only added in that public class, because without it it my "class" does not have a main method.
:laugh4: I'm digging myself deeper into a hole.
Tellos Athenaios
09-22-2010, 04:55
You've copied the same code twice; but in the code sample that is supposed to remove things from the list you will notice (on careful reading) that at first you are using a variable called “listemail” (which I guess is a ListModel or DefaultListModel) and then you suddenly switch to “listModel” (different name) for what should logically remain the same list model (unless I'm missing something) and finally you switch back again to “listemail”. I expect that the code can be fixed by simply changing “listModel” to “listemail”.
Also for ease of reading I would advise you to clean up (i.e. make consistent) the source indentation. ~;)
pevergreen
09-22-2010, 06:19
Apologies for the indentation.
Yup, the variable 'listemail' is simply a jList, but changing 'listModel' to 'listemail' does not work.
Yet now that I've restarted my pc (other issues) and loaded it up, its not showing an error for listModel.
Oh well, ignoring that, the issue now is that I can't run the darn thing.
Attempting to run by any of the possible methods gives the error: "No main classes found".
So I add a public class above my 'main' (As I was getting an error related to that before) but it won't run without that having its own filename.java "class 'whatever name' is public, should be declared in a file named whatever name.java"
Creating a new file named the same doesnt help, I'm trying to call it or something, and nothing seems to be working. I'm kinda new, would you prefer if I started giving small screenshots of what exactly is happening?
Tellos Athenaios
09-22-2010, 18:01
Could you simply upload the entire source file as text, that way I can see how the various methods and variables ought to fit together.
pevergreen
09-23-2010, 02:23
http://www.filefront.com/17312342/ContactEditorUI.txt/
http://www.filefront.com/17312343/ContactEditorUI.java/
http://www.filefront.com/17312344/ContactEditorUI.form/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* ContactEditorUI.java
*
* Created on 15/09/2010, 1:13:56 PM
*/
package my.contacteditor;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
/**
*
* @author 408797
*/
public class ContactEditorUI extends javax.swing.JFrame {
/** Creates new form ContactEditorUI */
public ContactEditorUI() {
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">
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
txtname = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
txtsurname = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
txttile = new javax.swing.JTextField();
txtnickname = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
jComboBox1 = new javax.swing.JComboBox();
btnname = new javax.swing.JButton();
jPanel2 = new javax.swing.JPanel();
jLabel6 = new javax.swing.JLabel();
txtemail = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
listemail = new javax.swing.JList();
btnadd = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
btnremove = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jLabel7 = new javax.swing.JLabel();
jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton2 = new javax.swing.JRadioButton();
jRadioButton3 = new javax.swing.JRadioButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Name", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N
jLabel1.setText("First Name:");
txtname.setText(" ");
jLabel2.setText("Last Name:");
txtsurname.setText(" ");
jLabel3.setText("Title:");
jLabel4.setText("Nickname:");
txttile.setText(" ");
txtnickname.setText(" ");
jLabel5.setText("Display Format:");
btnname.setText("Add");
btnname.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnnameActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(btnname, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel5)
.addComponent(jLabel1)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtname, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE)
.addComponent(txttile, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtnickname, javax.swing.GroupLayout.DEFAULT_SIZE, 331, Short.MAX_VALUE)
.addComponent(txtsurname, javax.swing.GroupLayout.DEFAULT_SIZE, 331, Short.MAX_VALUE)))
.addComponent(jComboBox1, 0, 454, Short.MAX_VALUE))))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(txtname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2)
.addComponent(txtsurname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(jLabel4)
.addComponent(txtnickname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txttile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnname))
);
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "E-mail", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N
jLabel6.setText("E-mail Address:");
txtemail.setText(" ");
jScrollPane1.setViewportView(listemail);
btnadd.setText("Add");
btnadd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnaddActionPerformed(evt);
}
});
jButton2.setText("Edit");
btnremove.setText("Remove");
btnremove.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnremoveActionPerformed(evt);
}
});
jButton4.setText("Advanced");
jLabel7.setText("Mail Format:");
buttonGroup1.add(jRadioButton1);
jRadioButton1.setText("HTML");
jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton1ActionPerformed(evt);
}
});
buttonGroup1.add(jRadioButton2);
jRadioButton2.setText("Plain Text");
buttonGroup1.add(jRadioButton3);
jRadioButton3.setText("Custom");
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(txtemail, javax.swing.GroupLayout.PREFERRED_SIZE, 367, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 451, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnremove, 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)
.addComponent(btnadd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(11, Short.MAX_VALUE))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel7)
.addContainerGap(490, Short.MAX_VALUE))))
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(69, 69, 69)
.addComponent(jRadioButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jRadioButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jRadioButton3)
.addContainerGap(301, Short.MAX_VALUE))
);
jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnadd, btnremove, jButton2, jButton4});
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(txtemail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(btnadd)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnremove)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton4)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jRadioButton1)
.addComponent(jRadioButton2)
.addComponent(jRadioButton3))
.addContainerGap(17, Short.MAX_VALUE))
);
jButton5.setText("OK");
jButton6.setText("Cancel");
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)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jButton5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton6)
.addGap(31, 31, 31))))
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton5, jButton6});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton5)
.addComponent(jButton6))
.addContainerGap(54, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btnaddActionPerformed(java.awt.event.ActionEvent evt) {
String name = txtemail.getText();
int index = listemail.getSelectedIndex(); //get selected index
if (index == -1) { //no selection, so insert at beginning
index = 0;
} else { //add after the selected item
index++;
}
listModel.insertElementAt(txtemail.getText(), index);
//Reset the text field.
txtemail.requestFocusInWindow();
txtemail.setText("");
//Select the new item and make it visible.
listemail.setSelectedIndex(index);
listemail.ensureIndexIsVisible(index);
// TODO add your handling code here:
}
private void btnremoveActionPerformed(java.awt.event.ActionEvent evt) {
int index = listemail.getSelectedIndex();
listemail.remove(index);
int size = listModel.getSize();
if (size == 0) { //No email records left, disable the button
btnremove.setEnabled(false);
} else { //Select an index.
if (index == listModel.getSize()) {
//removed item in last position
index--;
}
listemail.setSelectedIndex(index);
listemail.ensureIndexIsVisible(index);
}
}
private void btnnameActionPerformed(java.awt.event.ActionEvent evt) {
String name = txtname.getText();
String surname = txtsurname.getText();
String title = txttitle.getText();
String nickname = txtnickname.getText();
File outFile = new File("Customer.txt");
FileWriter out = new FileWriter(outFile, true);
FileUtils.writeStringToFile(name, surname, nickname, title);
out.close();
}
// TODO add your handling code here:
}
public class Program
{
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ContactEditorUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnadd;
private javax.swing.JButton btnname;
private javax.swing.JButton btnremove;
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
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.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JRadioButton jRadioButton3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JList listemail;
private javax.swing.JTextField txtemail;
private javax.swing.JTextField txtname;
private javax.swing.JTextField txtnickname;
private javax.swing.JTextField txtsurname;
private javax.swing.JTextField txttile;
// End of variables declaration
}
Tellos Athenaios
09-23-2010, 05:56
Simply overwrite both form & java file with these contents:
Java: http://pastebin.com/dj4gHDu4
Form: http://pastebin.com/tTUygE7k
And then you may want to read carefully to see what I changed and where.
pevergreen
09-23-2010, 06:18
Thank you so very much Tellos, its still throwing an error though. it doesn't recognise this:
import org.apache.commons.FileUtils;
package org.apache.commons does not exist
Unused Import
Output from running is:
run:
java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Uncompilable source code - package org.apache.commons does not exist
at my.contacteditor.ContactEditorUI.<clinit>(ContactEditorUI.java:16)
Could not find the main class: my.contacteditor.ContactEditorUI. Program will exit.
Exception in thread "main" Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
I removed it as a test: it runs, it adds things to the email list, but doesnt remove and it doesnt write to file, but it doesnt throw an error. It does create the text file though.
Error of trying to remove:
run:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 7
at java.awt.Container.remove(Container.java:1136)
at my.contacteditor.ContactEditorUI.btnremoveActionPerformed(ContactEditorUI.java:342)
at my.contacteditor.ContactEditorUI.access$200(ContactEditorUI.java:22)
at my.contacteditor.ContactEditorUI$3.actionPerformed(ContactEditorUI.java:171)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Looking into it.
Tellos Athenaios
09-23-2010, 06:49
Well you used FileUtils.writeStringToFile() which is from Apache Commons. So you would need to have that library in an appropriate location or the classpath set appropriately to use it. The ArrayIndexOutOfBoundsException is due to an error on my part: (size -1) should've been size. The corrected version is at: http://pastebin.com/E7zL000f
As for FileUtils: you used it, so I assumed that you knew what you were doing and since it appears to be from Apache Commons I added a corresponding import statement. Clearly I was wrong. :tongue: Anyway, in Netbeans you can right click your project, go to “Properties” and add Libraries to your project there, that will ensure that the Classpath of your code is set appropriately for using the libraries. If you do not want to use FileUtils: you can remove:
private void toFile () throws Exception {
// = null is needed to make the compiler happy otherwise it'll complain
// about unintialised variables.
FileWriter out = null;
try {
String name = txtname.getText();
String surname = txtsurname.getText();
String title = txttitle.getText();
String nickname = txtnickname.getText();
File outFile = new File("Customer.txt");
out = new FileWriter(outFile, true);
FileUtils.writeStringToFile(name, surname, nickname, title);
}
finally {
if (out != null) {
out.close();
}
}
}
And replace it with:
private void toFile () throws Exception {
// = null is needed to make the compiler happy otherwise it'll complain
// about unintialised variables.
java.io.PrintWriter out = null;
try {
String name = txtname.getText();
String surname = txtsurname.getText();
String title = txttitle.getText();
String nickname = txtnickname.getText();
File outFile = new File("Customer.txt");
out = new java.io.PrintWriter(new FileWriter(outFile, true));
// write contents to the file, one item per line
for(String line: new String[] { name, surname, nickname, title }) {
out.println(line);
}
out.flush();
}
finally {
if (out != null) {
out.close();
}
}
}
pevergreen
09-23-2010, 07:10
Well you used FileUtils.writeStringToFile() which is from Apache Commons. So you would need to have that library in an appropriate location or the classpath set appropriately to use it. The ArrayIndexOutOfBoundsException is due to an error on my part: (size -1) should've been size. The corrected version is at: http://pastebin.com/E7zL000f
As for FileUtils: you used it, so I assumed that you knew what you were doing and since it appears to be from Apache Commons I added a corresponding import statement. Clearly I was wrong. :tongue:.
Yeah, you were kind of. :tongue:
Everything works great, except it still isn't removing items from the list.
run:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 3
at java.awt.Container.remove(Container.java:1136)
at my.contacteditor.ContactEditorUI.btnremoveActionPerformed(ContactEditorUI.java:361)
at my.contacteditor.ContactEditorUI.access$200(ContactEditorUI.java:21)
at my.contacteditor.ContactEditorUI$3.actionPerformed(ContactEditorUI.java:170)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
You've helped so much, if I knew you in real life, I'd owe you like 3 cases of beer. :laugh4:
Tellos Athenaios
09-23-2010, 07:48
Oh I see:
listemail.remove(index) should be
listModel().remove(index)
pevergreen
09-23-2010, 07:52
Oh I see:
listemail.remove(index) should be
listModel().remove(index)
:guitarist:
How do I thank you Tellos. I can not thank you enough.
vBulletin® v3.7.1, Copyright ©2000-2025, Jelsoft Enterprises Ltd.