Creating applications to update a JList with reference to items added in runtime
06 August 2012
1 Comments
I want to improve this coding in a better way....
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.JOptionPane;
/**
*
* @author Vbabey
*/
/**
* @param args the command line arguments
*/
public class JlistApp1 extends JFrame {
public String getname;
public String listname;
private JButton okButton;
private JTextField nameText;
private JList list;
private DefaultListModel listModel;
public JlistApp1(){
super("JList Programme");
setLayout(new FlowLayout());
listModel = new DefaultListModel();
listModel.addElement("Jane Doe");
listModel.addElement("John Smith");
listModel.addElement("Kathy Green");
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("ListItem.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
listModel.addElement(strLine);
}
//Close the input stream
in.close();
}catch (Exception ex){//Catch exception if any
System.err.println("Error: " + ex.getMessage());
}
list = new JList(listModel);
list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
list.setLayoutOrientation(JList.VERTICAL_WRAP);
list.setVisibleRowCount(3);
list.setBorder(BorderFactory.createLineBorder(Color.BLACK));
JScrollPane listScroller = new JScrollPane(list);
listScroller.setPreferredSize(new Dimension(250, 300));
JLabel nameLabel = new JLabel("Enter Name:");
nameText = new JTextField(20);
okButton = new JButton("Pass Value");
okButton.setToolTipText("Add item to label");
JLabel entryLabel = new JLabel("Entered name is displayed here");
add(list,BorderLayout.CENTER);
add(nameLabel,BorderLayout.AFTER_LINE_ENDS);
add(nameText,BorderLayout.LINE_END);
add(okButton,BorderLayout.EAST);
add(entryLabel,BorderLayout.LINE_START);
thehandler handler = new thehandler();
okButton.addActionListener(handler);
}
private class thehandler implements ActionListener{
public void actionPerformed(ActionEvent e){
getname = nameText.getText();
JOptionPane.showMessageDialog(null,"The text you entered is "+getname+ " is added to the list",null,JOptionPane.INFORMATION_MESSAGE);
try{
// Create file
FileWriter fstream = new FileWriter("ListItem.txt", true);
BufferedWriter out = new BufferedWriter(fstream);
//append the data from a new line in the existing text
out.write(getname);
out.newLine();
//Close the output stream
System.out.println("Your data has been successfully appended to the file...");
out.close();
}
catch (Exception ex){//Catch exception if any
System.err.println("Error: " + ex.getMessage());
}
listModel.addElement(getname) ;
nameText.setText("");
}
}
}
1 Comments On This Entry
Page 1 of 1
modi123_1
07 August 2012 - 07:19 AM
Congrats.. so is this a blog journey of you doing improving that chunk of code?
Page 1 of 1
Trackbacks for this entry [ Trackback URL ]
Tags
My Blog Links
Recent Entries
-
Creating applications to update a JList with reference to items added in runtimeon Aug 06 2012 08:56 PM
Recent Comments
-
modi123_1
on Aug 07 2012 07:19 AM
Creating applications to update a JList with reference to items added in runtime
Search My Blog
0 user(s) viewing
0 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)
Categories
|
|











|