Solved

Java Program

Posted on 2001-07-14
10
216 Views
Last Modified: 2006-11-17
I am doing a project in Java that deals with random file. I am suppose to use buttons to browse, next, previous etc. However, I am having a problem implementing the next,previous, browse, buttons. Could u help me please or refer me a site.

0
Comment
Question by:rwhittle
10 Comments
 

Expert Comment

by:kommineni_sridhar
ID: 6283055
u can do that by 2 types
1 . adding a hidden field n then transfering the prevoius data
2. by using cookies
0
 
LVL 10

Expert Comment

by:Nushi
ID: 6283157
i can write u a sample code but youll have to explain what u need from the buttons to do.

Nushi.
0
 
LVL 15

Expert Comment

by:mohan_sekar
ID: 6285331
Hi,

     Please post the question elaborately. so that I can find u a solution.

bye
0
 
LVL 1

Expert Comment

by:prasad2000
ID: 6285515
code the program in such a way that everytime user sends a request it should contain values to identify the type of button user has clicked .
i suppose u r using servlets.so send these values as form parameters.
then write the servlet prog as u want.
0
 

Author Comment

by:rwhittle
ID: 6286530
Mr/Mrs Nushi

I would really appreciate if you could sent me sample codes.
First of all the records are stored in a file.

a. I need the code to browse all records
b. To goto to of file
c. to move to next record etc

When the use enters a record the file stores the strings but not the integers eg.

I enter John Brown, 96q1000, history
the file is storing history, John Brown but not 96q1000

0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 10

Expert Comment

by:Nushi
ID: 6333255
rwhittle

im sorry, im wasnt here for a while ill write you some sample code today.

Nushi.
0
 
LVL 10

Expert Comment

by:Nushi
ID: 6333270
i will wirte you a full application for more points.
Nushi.
0
 
LVL 10

Accepted Solution

by:
Nushi earned 50 total points
ID: 6337541
//this class will show you how to use JList to move throw the records.
//you can use the buttons to navigate throw the list or to
//search specific item in the list by the search button.
//you will also need to copy and paste the following file:
//just copy it as it is and name it Nushi.txt

-- file starts here without this line

###      Save this file as:
###
###      Nushi.txt
###
###      the records are in the following format:
###      RecordIndex = RecordText
###
###      when we use the next/back button we just take the previuos/next
###      record. when we do search we get back the record index
###
###
###

Record0 = this is the text for record 0
Record1 = place here any text you want to set for record 1
Record2 = record 2 is just this text
Record3 = place here record 3 text
Record4 = are you sure you want to choose record 4?
Record5 = why did you choose me (record 5)
Record6 = whats up man? how are you doing today?

-- end file dont copy this line

//java class and again copy and paste it as it is
//and then adjust it to your needs.

import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
//in the class i used properties due to
//the fact that i dont exactly what u tring to do
//im reading and writing to a property file.
      
/**
 * This class implements Frame window
 * and the class functions for browsing
 */
public class NushiBrowser {
      
      private JFrame            g_mainFrame = new JFrame();
      private Properties      g_prop = new Properties();
      private File            g_file = new File("Nushi.txt");
      
      private JList            g_list  = null;
      private JDialog            g_dialog= null;
      private JTextField      g_search= new JTextField();
      private JTextArea      g_text      = new JTextArea();
      private JScrollPane g_textScroll= new JScrollPane(g_text);
      
      private JPanel      g_buttonsPanel = new JPanel();
      private JPanel  g_bottomPanel  = new JPanel(new GridLayout(2,1));
      private JLabel      g_label  = new JLabel("No messages yet");
      private JButton      g_browse = new JButton("Search");
      private JButton      g_back       = new JButton(" Back ");
      private JButton      g_next       = new JButton(" Next ");
      
      /**
       * Default CTOR
       */
      public NushiBrowser(){
            initFrame("Nushi's sample");
            }
      
      
      /**
       * THis function wll draw the frame
       */
      public void initFrame(String p_title){
            loadHistory();
            Dimension d=g_mainFrame.getToolkit().getScreenSize();
            
            g_mainFrame.setTitle(p_title);
            g_mainFrame.setDefaultCloseOperation(g_mainFrame.EXIT_ON_CLOSE);
            g_mainFrame.setBounds(d.width/4, d.height/4, d.width/2, d.height/2);
            
            g_text.setBackground(Color.white);
            
            g_mainFrame.getContentPane().setLayout(new BorderLayout());
            g_mainFrame.getContentPane().add(g_list                  , BorderLayout.WEST);
            g_mainFrame.getContentPane().add(g_textScroll      , BorderLayout.CENTER);
            g_mainFrame.getContentPane().add(g_bottomPanel      , BorderLayout.SOUTH);
            
            g_back.addMouseListener(new myBackLister());
            g_next.addMouseListener(new myNextLister());
            g_browse.addMouseListener(new myBrowseLister());
            
            g_buttonsPanel.add(g_back);
            g_buttonsPanel.add(g_browse);
            g_buttonsPanel.add(g_next);
            g_buttonsPanel.add(g_label);
            
            g_bottomPanel.add(g_buttonsPanel);
            g_bottomPanel.add(g_label);
            
            g_mainFrame.setVisible(true);
            g_mainFrame.validate();
            initSearchDialog();
            }
      
      //////////////////////////////////////////////////////
      //                              CODE FOR SEARCH                              //
      //////////////////////////////////////////////////////
      public void initSearchDialog(){
            
            JLabel            l_label  = new JLabel("Insert record to search");
            JButton            l_button = new JButton(" Search ");
            g_dialog = new JDialog( g_mainFrame , "Search record" );
            l_button.addMouseListener(new searchLister());
            
            Point      l_point  = g_mainFrame.getLocationOnScreen();
            Dimension l_dim = g_mainFrame.getSize();
            
            g_dialog.setBounds(l_point.x + l_dim.width/4   ,
                                       l_point.y + l_dim.height/4  ,
                                       l_dim.width/2, 95);
            
            g_dialog.getContentPane().add(l_label  , BorderLayout.NORTH);
            g_dialog.getContentPane().add(g_search , BorderLayout.CENTER);
            g_dialog.getContentPane().add(l_button , BorderLayout.SOUTH);
            g_dialog.setModal(true);
            }
      
      //////////////////////////////////////////////////////
      //                        READ AND WRITE DATA                         //
      //////////////////////////////////////////////////////
      public void loadHistory(){
            
            try{
                  FileInputStream l_fin = new FileInputStream(g_file);
                  g_prop.load(l_fin);
                  }catch(Exception e){
                  System.out.println("Exception in loadHistory: "+e);
                  }
            
            // Create the JList, set the number of visible rows, add a
            // listener, and put it in a JScrollPane.
            String l_entrys[]= new String[g_prop.size()];
            int i=0;
            
            for (Enumeration e=g_prop.keys(); e.hasMoreElements(); )
                  l_entrys[i++] = (String)(e.nextElement());
            
            g_list = new JList(l_entrys);
            g_list.setBackground(Color.lightGray);
            g_list.addListSelectionListener(new NushiListListener());
            g_mainFrame.getContentPane().add( g_list , BorderLayout.WEST);
            g_mainFrame.validate();
            }
      
      
      //////////////////////////////////////////////////////
      //                        MOUSELISTENER CLASSES                        //
      //////////////////////////////////////////////////////
      class NushiListListener implements ListSelectionListener {
            /** You get three events in many cases -- one for the deselection
             *  of the originally selected entry, one indicating the selection
             *  is moving, and one for the selection of the new entry. In
             *  the first two cases, getValueIsAdjusting returns true,
             *  thus the test below when only the third case is of interest.
             */
            public void valueChanged(ListSelectionEvent event) {
                  
                  if (!event.getValueIsAdjusting()){
                        g_text.setText((String)g_prop.get(g_list.getSelectedValue()));
                        g_label.setText("Record Selected:    "+(String)g_list.getSelectedValue());
                        }
                  g_mainFrame.validate();
                  }
            }
      
      class myBackLister extends MouseAdapter{
            public void mouseClicked(MouseEvent e){
                  int l_index = g_list.getSelectedIndex();
                  //we can go back only to the 0 index
                  if (l_index >0 )
                        g_list.setSelectedIndex(l_index-1);
                  g_mainFrame.validate();
                  }
            }
      
      class myNextLister extends MouseAdapter{
            public void mouseClicked(MouseEvent e){
                  int l_index = g_list.getSelectedIndex();
                  //we can only go up to the last index
                  if (l_index < g_prop.size()-1 )
                        g_list.setSelectedIndex(l_index+1);
                  g_mainFrame.validate();
                  }
            }
      
      class myBrowseLister extends MouseAdapter{
            
            public void mouseClicked(MouseEvent e){
                  g_search.setText((String)g_list.getSelectedValue());
                  g_dialog.show();
                  g_mainFrame.validate();
                  }
            }
      
      class searchLister extends MouseAdapter{
            public void mouseClicked(MouseEvent e){
                  int l_index=-1;
                  g_dialog.setVisible(false);
                  
                  //look for the record in the properties list
                  String Key = (String)g_prop.get(g_search.getText());
                  
                  if (Key != null){
                        g_list.setSelectedValue(g_search.getText() , true);
                        l_index = g_list.getSelectedIndex();
                        }
                  
                  if (l_index != -1 )
                        g_list.setSelectedIndex(l_index);
                  
                  g_mainFrame.validate();
                  }
            }
      }
      

Nushi.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6892949
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> You cannot delete a question with comments, special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process for further information, if needed.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE Pro user, use the Power Search option to find them.  Anytime you have questions which are LOCKED with a Proposed Answer but does not serve your needs, please reject it and add comments as to why.  In addition, when you do grade the question, if the grade is less than an A, please add a comment as to why.  This helps all involved, as well as future persons who may access this item in the future to seek help.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.11495619.html
http://www.experts-exchange.com/questions/Q.11553818.html
http://www.experts-exchange.com/questions/Q.11754260.html
http://www.experts-exchange.com/questions/Q.12045280.html
http://www.experts-exchange.com/questions/Q.12046779.html
http://www.experts-exchange.com/questions/Q.20149380.html
http://www.experts-exchange.com/questions/Q.20153223.html
http://www.experts-exchange.com/questions/Q.20152485.html
http://www.experts-exchange.com/questions/Q.20152487.html
http://www.experts-exchange.com/questions/Q.20154573.html
http://www.experts-exchange.com/questions/Q.20181947.html
http://www.experts-exchange.com/questions/Q.20183912.html
http://www.experts-exchange.com/questions/Q.20268601.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://www.experts-exchange.com/questions/Q.20150772.html

PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  EXPERTS:  Please leave any comments regarding your closing recommendations if this item remains inactive another seven (7) days.  Also, if you are interested in the cleanup effort, please click this link http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643

Moderators will finalize this question if still open in 7 days, by either moving this to the PAQ (Previously Asked Questions) at zero points, deleting it or awarding expert(s) when recommendations are made, or an independent determination can be made.  Expert input is always appreciated to determine the fair outcome.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange

P.S.  For any year 2000 questions, special attention is needed to ensure the first correct response is awarded, since they are not in the comment date order, but rather in Member ID order.
0
 
LVL 1

Expert Comment

by:Computer101
ID: 6974341
Comment from exeprt accepted as answer

Computer101
E-E Moderator
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now