Results 1 to 3 of 3
  1. #1
    VinTiger is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default how to add event handling to my buttons

    I'm working on a program here, that is suppose to read a file, have the ability to navigate back and forth through it with the help of buttons and also have editing as a added functionality....

    I'm completely new to programming, so i'm having some difficulties trying to figure out my next step here, in particular how to make the buttons work..any help is greately appreciated.

    Java Code:
    import java.awt.Dimension;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.*;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    
    public class MyText extends JFrame {
    
        private Toolkit toolkit;
    
    
    public static int LineCount = 0; //Counts the lines.
       public static String[] content;  
       
       public static void ReadFile() throws Exception { 
    
          BufferedReader TextFileReader = new BufferedReader(new FileReader("./info.txt")); 
    
          String c;
          String t;
    
          t = "Blank";
          while((c = TextFileReader.readLine()) != null) {  
             LineCount++;
             t += ","+ c;
          }
    
          t = t.replaceAll("Blank,",""); 
          content = new String[LineCount];
          content = t.split(",");
          TextFileReader.close(); 
          
          
          for(int i = 0; i < 7; i++)  //Keep printing as long as i is smaller than 7, which is what I need for my text file.
              System.out.println(content[i]); //prints the first part of my list of names.
             
       }
    
    
        public MyText() {
    
            setTitle("Name and Address");
            setSize(300, 400);
    
            toolkit = getToolkit();
            Dimension size = toolkit.getScreenSize();
            setLocation((size.width - getWidth())/2, (size.height - getHeight())/2);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
    
            JPanel panel = new JPanel();
            getContentPane().add(panel);
    
    	panel.setLayout(null);
    
    
            JButton beep = new JButton("Next");
            beep.setBounds(150, 60, 80, 30);
            beep.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent event) {
                    toolkit.beep();
    
    System.out.println("Next");
    
    
                }
            });
    
           JButton close = new JButton("Back");
           close.setBounds(50, 60, 80, 30);
           close.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent event) {
                   toolkit.beep();
    
    System.out.println("Go back");
    
              }
           });
    
            panel.add(beep);
            panel.add(close);
    
        }
    
        public static void main(String[] args) {
    
            MyText buttons = new MyText();
            buttons.setVisible(true);
    
        }
    }

  2. #2
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    For something as simple as this, use the ActionListener interface. See the Java Tutorial's section on this topic for more information, as it is a very large subject.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    What suppose to do the following code segment?

    Java Code:
    JButton close = new JButton("Back");
           close.setBounds(50, 60, 80, 30);
           close.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent event) {
                   toolkit.beep();
    
    System.out.println("Go back");
    
              }
           });
    Did you get any error message? If so better to copy-paste here to see.

Similar Threads

  1. Event handling... help?
    By kevzspeare in forum New To Java
    Replies: 2
    Last Post: 04-04-2009, 08:46 PM
  2. Event handling in JSF
    By java08 in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 03-24-2009, 06:42 AM
  3. rmi and event handling
    By darkhorse in forum Advanced Java
    Replies: 0
    Last Post: 03-15-2009, 08:20 AM
  4. SWT Event Handling
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-30-2007, 12:21 PM
  5. Event Handling
    By luisarca in forum Sun Java Wireless Toolkit
    Replies: 5
    Last Post: 05-07-2007, 06:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •