Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Another Quick Question

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Location
    Maine
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Another Quick Question

    Alright here's what I'm trying to do. I'm making a item drop logger for an MMO I play. The goal of the program is for the user to be able to click the image of the drop they got (button) and have the button write the drop into a text file. So far I have 2 seperate codes that I can't figure out how to combine. I'm also trying to figure out how to put an if statement into the writter file.

    Writter code:
    import java.io.*;
    import javax.swing.JOptionPane;
     
    public class logger {
     
     
     
      Writer output = null;
      String text = "Dragon Hatchet";
      File file = new File("Dagannoth Droplog.txt");
      output = new BufferedWriter(new FileWriter(file));
      output.write(text);
      output.close(); 
      }
    }


    Button code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    import java.awt.Toolkit; 
    import java.io.*;
     
    public class SwingButtonDemo {
            public static void main(String[] a) {
     
                    JFrame f = new JFrame();
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    f.add(new ButtonDemo());
                    f.setSize(550, 400);
                    f.setVisible(true);               
            }
    }
     
     
     
     
    class ButtonDemo extends JPanel implements ActionListener {
            JTextField jtf;
     
            public ButtonDemo() {
                    try {
                            makeGUI();
                    } catch (Exception exc) {
                            System.out.println("Can't create because of " + exc);
                    }
            }
     
            private void makeGUI() {
                    setLayout(new FlowLayout());
     
                    ImageIcon DragonHatchet = new ImageIcon(
                                    "C:/Users/Jackson/Desktop/Dragon_hatchet.gif");
                    JButton jb = new JButton(DragonHatchet);
                    jb.setActionCommand("Dragon Hatchet");
                    jb.addActionListener(this);
                    add(jb);
     
                    ImageIcon BerserkerRing = new ImageIcon(
                                    "C:/Users/Jackson/Desktop/Berserker_ring_(i).gif");
                    jb = new JButton(BerserkerRing);
                    jb.setActionCommand("Berserker Ring");
                    jb.addActionListener(this);
                    add(jb);
     
                    ImageIcon WarriorRing = new ImageIcon(
                                    "C:/Users/Jackson/Desktop/Warrior_Ring.gif");
                    jb = new JButton(WarriorRing);
                    jb.setActionCommand("Warrior Ring");
                    jb.addActionListener(this);
                    add(jb);
     
                    ImageIcon EliteClue = new ImageIcon("C:/Users/Jackson/Desktop/Clue_scroll.gif");
                    jb = new JButton(EliteClue);
                    jb.setActionCommand("Elite Clue Scroll");
                    jb.addActionListener(this);
                    add(jb);
     
                    jtf = new JTextField(15);
                    add(jtf);
     
                    ImageIcon ii=new ImageIcon("C:/Users/Jackson/Desktop/droplogger.png");  
                    JLabel label=new JLabel(ii); 
                    label = new JLabel("Dragon Hatchet"); 
            }
     
     
            public void actionPerformed(ActionEvent ae) {
                    jtf.setText(ae.getActionCommand());
            }
    }

    If anyone could tell me how I could incorporate the two codes into one that would be awesome.

    Also, to avoid making another topic: How would I write and if then statement to tell the difference between the drops? An example would be

    if (user clicks dragon hatchet image)
    then log "1x dragon hatchet" to droplog.txt


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,106
    Thanks
    65
    Thanked 2,717 Times in 2,667 Posts

    Default Re: Another Quick Question

    First you need to change the code in the first post to have a method in the class the contains the code you have there.
    Then depending on how you configure the Logger class (note Capital letter in name) you can create an instance of it in the second code and call it to write out the text you want written.

    How would I write and if then statement to tell the difference between the drops
    If a Drop is a class and you are comparing instances of the class, then write a compare method in the Drop class to compare the two Drops and return true if they are the same.

Similar Threads

  1. Quick question about layout
    By danty in forum AWT / Java Swing
    Replies: 1
    Last Post: June 12th, 2011, 02:13 PM
  2. Quick question, arraylists
    By Intensity` in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 16th, 2011, 08:47 PM
  3. Hi, quick question
    By curras in forum Member Introductions
    Replies: 1
    Last Post: March 21st, 2011, 03:21 PM
  4. scope - quick question
    By bbr201 in forum Java Theory & Questions
    Replies: 4
    Last Post: July 28th, 2010, 08:30 AM
  5. Quick Question about Mergesort
    By Shadow703793 in forum Algorithms & Recursion
    Replies: 4
    Last Post: March 4th, 2010, 05:48 PM