Subscribe to vbabey's Blog        RSS Feed
-----

Creating applications to update a JList with reference to items added in runtime

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

07 August 2012 - 07:19 AM
Congrats.. so is this a blog journey of you doing improving that chunk of code?
0
Page 1 of 1

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

October 2014

S M T W T F S
   1234
567891011
12131415161718
19202122232425
26272829 30 31 

Tags

    Search My Blog

    0 user(s) viewing

    0 Guests
    0 member(s)
    0 anonymous member(s)

    Categories