13 Replies - 618 Views - Last Post: 30 September 2013 - 06:17 PM Rate Topic: -----

#1 fftk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-September 13

JTextField double array to file. how to?

Posted 17 September 2013 - 06:25 PM

I am designing a game, and need help in a simple item addition that will also act as a mod creation helper. Now, using a grid of JTextFields i have tried to make the TextFields data into a file. the problem is, it wont work. Not even an error message. Any suggestions on to fix my code to make a TextField grid that will allow the buffered writer to actuly write to the file, or just any ideas would be greatly appreciated.
package ModCreator;

import Util.findFile;
import java.awt.event.*;
import java.io.*;


import javax.swing.*;

public class Craftingsystemcreator implements ActionListener {
    int use;
    JTextFieldGridutil grid;
    JButton button = new JButton("Create");
   public static JFrame frame = new JFrame("Frame");
    public Craftingsystemcreator(){
        JMenu men = new JMenu("Grid Types");
        button.addActionListener(this);
        JMenuItem It1 = new JMenuItem("3x3");
        It1.addActionListener(this);
        JMenuItem It2 = new JMenuItem("4x4");
        It2.addActionListener(this);
        JMenuItem It3 = new JMenuItem("5x5");
        It3.addActionListener(this);
        JMenuItem It4 = new JMenuItem("6x6");
        It4.addActionListener(this);
        JMenuItem It5 = new JMenuItem("11x11");
        It5.addActionListener(this);
        JMenuBar bar = new JMenuBar();
        men.add(It1);
        men.add(It2);
        men.add(It3);
        men.add(It4);
        men.add(It5);
        bar.add(men);
        frame.setJMenuBar(bar);
        
        
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        if(ae.getActionCommand().equals("3x3")){   
            use = 3;
            grid = new JTextFieldGridutil(3 , 3);
            JPanel pan = grid.frame;
            frame.add(pan);
            pan.add(button);
                
            pan.revalidate();
        }
        if(ae.getActionCommand().equals("4x4")){
             use = 4;
            grid = new JTextFieldGridutil(4, 4);
            JPanel pan = grid.frame;
            frame.add(pan);
            pan.add(button);
             pan.revalidate();
        }
        if(ae.getActionCommand().equals("5x5")){
             use = 5;
            grid = new JTextFieldGridutil(5 , 5);
            JPanel pan = grid.frame;
            frame.add(pan);
            pan.add(button);
             pan.revalidate();
        }
        if(ae.getActionCommand().equals("6x6")){
             use = 6;
            grid = new JTextFieldGridutil(6 , 6);
            JPanel pan = grid.frame;
            frame.add(pan);
            pan.add(button);
             pan.revalidate();
        }
        if(ae.getActionCommand().equals("11x11")){
             use = 11;
            grid = new JTextFieldGridutil(11 , 11);
            JPanel pan = grid.frame;
            frame.add(pan);
            pan.add(button);
             pan.revalidate();
        }
        if(ae.getActionCommand().equals("Create")){
            try{
        File file = new File("C:\\Program Files\\Lifeoffallen\\Craftingsystme\\" +use +".craft" );
        FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
        BufferedWriter dgf = new BufferedWriter(fw);
        JTextField[][] feild = grid.grid;
        int i = 1;
        int n = 1; 
        dgf.newLine();
        for(i = 1; i != use; i++){
        while(feild[i][n] !=null){
        String ar = new String(feild[i][n].getText());
        
        
        dgf.write(ar + "\t");

        
        }}}catch(IOException ioe){
                System.out.print(ioe);
            }
        }
        
    }
    public static void main(String[] args){
        new Craftingsystemcreator();
        
        frame.pack();
        frame.setVisible(true);
    }
}



Here is the Grid maker
package ModCreator;

import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.JPanel;


public class JTextFieldGridutil {

 
        JPanel frame=new JPanel(); //creates frame
        JTextField[][] grid; //names the grid of buttons
 
        public JTextFieldGridutil(int width, int length){ //constructor
                frame.setLayout(new GridLayout(width,length)); //set layout
                grid=new JTextField[width][length]; //allocate the size of grid
                
                for(int y=0; y<length; y++){
                        for(int x=0; x<width; x++){
                                grid[x][y]=new JTextField("("+x+","+y+")"); //creates new button    
                                frame.add(grid[x][y]); //adds button to grid
                        }
                }

                frame.setVisible(true); //makes frame visible
        }
        public JPanel pan(){
            return frame;
        }

}





Is This A Good Question/Topic? 0
  • +

Replies To: JTextField double array to file. how to?

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,202
  • Joined: 20-September 08

Re: JTextField double array to file. how to?

Posted 18 September 2013 - 04:27 AM

Much better to create a subclass of JPanel:
import java.awt.GridLayout;

import javax.swing.JPanel;
import javax.swing.JTextField;


public class JTextFieldGrid extends JPanel {
    JTextField[][] grid; //names the grid of fields

    public JTextFieldGrid(int width, int length) { //constructor
        setLayout(new GridLayout(width, length)); //set layout
        grid = new JTextField[width][length]; //allocate the size of grid

        for (int w = 0; w < width; w++) {
            for (int h = 0; h < length; h++) {
                grid[w][h] = new JTextField(String.format("(%d,%d)", w, h));
                add(grid[w][h]); //adds field to grid
            }
        }
    }

    public JTextField getFieldAt(int x, int y) {
        return grid[x][y];
    }
}

This post has been edited by g00se: 18 September 2013 - 04:28 AM
Reason for edit:: typo

Was This Post Helpful? 0
  • +
  • -

#3 fftk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-September 13

Re: JTextField double array to file. how to?

Posted 20 September 2013 - 05:24 AM

Thanks for the help, but the grid method is not the one giving me trouble. The screen will pop up with the grided JTextFields, however i can not save them all into a file. I could go through with writing write around 200 times but that's not happening.
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,202
  • Joined: 20-September 08

Re: JTextField double array to file. how to?

Posted 20 September 2013 - 05:49 AM

You could save them as CSV - is that suitable?
Was This Post Helpful? 1
  • +
  • -

#5 fftk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-September 13

Re: JTextField double array to file. how to?

Posted 20 September 2013 - 08:21 AM

I've read through the idea, and it sounds like a file format I am looking for, could you send me a link to a tutorial for this in java, or is it the same as a .txt?
Was This Post Helpful? 0
  • +
  • -

#6 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: JTextField double array to file. how to?

Posted 20 September 2013 - 08:28 AM

Same as .txt - just with data separated by commas.
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,202
  • Joined: 20-September 08

Re: JTextField double array to file. how to?

Posted 20 September 2013 - 08:37 AM

Try calling the following with a PrintWriter opened on the file. Don't forget to close() it afterwards

    public void textWidgetsToCsv(PrintWriter out, JTextComponent[][] widgets) {
        for (int i = 0; i < widgets.length; i++) {
            String line = toCsv(widgets[i]);
            out.println(line);
        }
    }

    public String toCsv(Object[] objects) {
        String sep = "";
        StringBuilder sb = new StringBuilder();

        for (Object o : objects) {
            sb.append(sep).append(o.toString());
            sep = ",";
        }

        return sb.toString();
    }

Of course, that's 'naive' csv - proper csv needs to be able to cope with escapement and quoting

This post has been edited by g00se: 20 September 2013 - 08:39 AM
Reason for edit:: Clarification

Was This Post Helpful? 1
  • +
  • -

#8 fftk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-September 13

Re: JTextField double array to file. how to?

Posted 20 September 2013 - 12:13 PM

Thanks ill try it and i will report the results
Was This Post Helpful? 0
  • +
  • -

#9 fftk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-September 13

Re: JTextField double array to file. how to?

Posted 24 September 2013 - 05:48 PM

Okay i do not want to be rude, but i thought i could try my own bit of code to fix my problem, however, now i end up getting up an error, see below. this is the new code.
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ModCreator;

import Util.findFile;
import java.awt.event.*;
import java.io.*;
import java.awt.Point;
import java.util.logging.Level;
import java.util.logging.Logger;


import javax.swing.*;

public class Craftingsystemcreator implements ActionListener {
    int use;
    JTextFieldGridutil grid;
    JButton button = new JButton("Create");
   public static JFrame frame = new JFrame("Frame");
    public Craftingsystemcreator(){
        JMenu men = new JMenu("Grid Types");
        button.addActionListener(this);
        JMenuItem It1 = new JMenuItem("3x3");
        It1.addActionListener(this);
        JMenuItem It2 = new JMenuItem("4x4");
        It2.addActionListener(this);
        JMenuItem It3 = new JMenuItem("5x5");
        It3.addActionListener(this);
        JMenuItem It4 = new JMenuItem("6x6");
        It4.addActionListener(this);
        JMenuItem It5 = new JMenuItem("11x11");
        It5.addActionListener(this);
        JMenuBar bar = new JMenuBar();
        men.add(It1);
        men.add(It2);
        men.add(It3);
        men.add(It4);
        men.add(It5);
        bar.add(men);
        frame.setJMenuBar(bar);
        
        
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        if(ae.getActionCommand().equals("3x3")){   
            use = 3;
            grid = new JTextFieldGridutil(3 , 3);
            JPanel pan = grid.frame;
            frame.add(pan);
            pan.add(button);
                
            pan.revalidate();
        }
        if(ae.getActionCommand().equals("4x4")){
             use = 4;
            grid = new JTextFieldGridutil(4, 4);
            JPanel pan = grid.frame;
            frame.add(pan);
            pan.add(button);
             pan.revalidate();
        }
        if(ae.getActionCommand().equals("5x5")){
             use = 5;
            grid = new JTextFieldGridutil(5 , 5);
            JPanel pan = grid.frame;
            frame.add(pan);
            pan.add(button);
             pan.revalidate();
        }
        if(ae.getActionCommand().equals("6x6")){
             use = 6;
            grid = new JTextFieldGridutil(6 , 6);
            JPanel pan = grid.frame;
            frame.add(pan);
            pan.add(button);
             pan.revalidate();
        }
        if(ae.getActionCommand().equals("11x11")){
             use = 11;
            grid = new JTextFieldGridutil(11 , 11);
            JPanel pan = grid.frame;
            frame.add(pan);
            pan.add(button);
             pan.revalidate();
        }
        if(ae.getActionCommand().equals("Create")){
            try{
        File file = new File("C:\\Users\\Marie\\Desktop\\" +use +".craft" );
        FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
        BufferedWriter dgf = new BufferedWriter(fw);
        JTextField[][] feild;
                feild = grid.grid;
        int i = 0;
        int n = 0; 

        
           dgf.newLine(); 
        while(i != use + 1){
            if(i == use){
                i=0;
                if(n != use){
                n++;}
            }else{
               i++; 
            }
            
            String content = new String(feild[i][n].getText());
            dgf.write(content + "\t");
 
        }
        dgf.close();
        
        }catch(IOException ioe){
                System.out.print(ioe);
            }
        }
        
        

    }
    public static void main(String[] args){
        new Craftingsystemcreator();
        
        frame.pack();
        frame.setVisible(true);
    }
}


Error:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3
	at ModCreator.Craftingsystemcreator.actionPerformed(Craftingsystemcreator.java:111)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
	at java.awt.Component.processMouseEvent(Component.java:6505)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
	at java.awt.Component.processEvent(Component.java:6270)
	at java.awt.Container.processEvent(Container.java:2229)
	at java.awt.Component.dispatchEventImpl(Component.java:4861)
	at java.awt.Container.dispatchEventImpl(Container.java:2287)
	at java.awt.Component.dispatchEvent(Component.java:4687)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
	at java.awt.Container.dispatchEventImpl(Container.java:2273)
	at java.awt.window.dispatchEventImpl(window.java:2719)
	at java.awt.Component.dispatchEvent(Component.java:4687)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
	at java.awt.EventQueue.access$200(EventQueue.java:103)
	at java.awt.EventQueue$3.run(EventQueue.java:688)
	at java.awt.EventQueue$3.run(EventQueue.java:686)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
	at java.awt.EventQueue$4.run(EventQueue.java:702)
	at java.awt.EventQueue$4.run(EventQueue.java:700)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)


Was This Post Helpful? 0
  • +
  • -

#10 ChrisNt  Icon User is online

  • D.I.C Addict

Reputation: 200
  • View blog
  • Posts: 625
  • Joined: 31-July 13

Re: JTextField double array to file. how to?

Posted 24 September 2013 - 06:13 PM

Javadoc
IndexOutOfBoundsException
Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.

This post has been edited by ChrisNt: 24 September 2013 - 06:13 PM

Was This Post Helpful? 1
  • +
  • -

#11 fftk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-September 13

Re: JTextField double array to file. how to?

Posted 26 September 2013 - 07:28 PM

View PostChrisNt, on 24 September 2013 - 06:13 PM, said:

Javadoc
IndexOutOfBoundsException
Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.

can you give an example please, this is level of arrays is new to me.
Was This Post Helpful? 0
  • +
  • -

#12 ChrisNt  Icon User is online

  • D.I.C Addict

Reputation: 200
  • View blog
  • Posts: 625
  • Joined: 31-July 13

Re: JTextField double array to file. how to?

Posted 26 September 2013 - 08:21 PM

	public static void main(String args[]) {
		
		                                 // columns
		                                                    
		int array[][] = new int[][] {    { 20, 30 , 40 },    // r
				                         { 50, 60,  70 },    // o
				                         { 80, 90, 100 },    // w 
				                    };                       // s
		
		// In this example we have 3 columns and 3 rows 
		// remember that index in arrays starts from 0
		
		
		
		                    
		                          
		//    0    1     2        
		//  ________________            
		//  | 20 , 30 , 40  |  0        
		//  | 50 , 60 , 70  |  1         
		//  | 80 , 90 , 100 |  2         
	    
		// For example to get value 100 we have just have to print the 
		// appropriate column and row .
		
		System.out.println(array[2][2]);
		
		
		
		// If we try to print row 3 and column 2 we will get an index out of bounds exception
		// because we try to access an illegal index 
		System.out.println(array[3][2]);
	
	}
		

This post has been edited by ChrisNt: 26 September 2013 - 08:29 PM

Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: JTextField double array to file. how to?

Posted 27 September 2013 - 03:50 AM

It is written in your stack trace

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3


You are using an index of 3
there
at ModCreator.Craftingsystemcreator.actionPerformed(Craftingsystemcreator.java:111)


which is
            String content = new String(feild[i][n].getText());


so or i == 3 or n == 3 and it shouldn'tjust println() both before executing that statement
Was This Post Helpful? 1
  • +
  • -

#14 fftk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-September 13

Re: JTextField double array to file. how to?

Posted 30 September 2013 - 06:17 PM

Okay, i fiqured out the problem by looking on how the grid was made, now i am stumped on why it works, can someone help with explaining why do the for statements work instead of when.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ModCreator;

import Util.findFile;
import java.awt.event.*;
import java.io.*;
import java.awt.Point;
import java.util.logging.Level;
import java.util.logging.Logger;


import javax.swing.*;

public class Craftingsystemcreator implements ActionListener {
    int use;
    JTextFieldGridutil grid;
    JButton button = new JButton("Create");
   public static JFrame frame = new JFrame("Frame");
    public Craftingsystemcreator(){
        JMenu men = new JMenu("Grid Types");
        button.addActionListener(this);
        JMenuItem It1 = new JMenuItem("3x3");
        It1.addActionListener(this);
        JMenuItem It2 = new JMenuItem("4x4");
        It2.addActionListener(this);
        JMenuItem It3 = new JMenuItem("5x5");
        It3.addActionListener(this);
        JMenuItem It4 = new JMenuItem("6x6");
        It4.addActionListener(this);
        JMenuItem It5 = new JMenuItem("11x11");
        It5.addActionListener(this);
        JMenuBar bar = new JMenuBar();
        men.add(It1);
        men.add(It2);
        men.add(It3);
        men.add(It4);
        men.add(It5);
        bar.add(men);
        frame.setJMenuBar(bar);
        
        
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        if(ae.getActionCommand().equals("3x3")){   
            use = 3;
            grid = new JTextFieldGridutil(3 , 3);
            JPanel pan = grid.frame;
            frame.add(pan);
            pan.add(button);
                
            pan.revalidate();
        }
        if(ae.getActionCommand().equals("4x4")){
             use = 4;
            grid = new JTextFieldGridutil(4, 4);
            JPanel pan = grid.frame;
            frame.add(pan);
            pan.add(button);
             pan.revalidate();
        }
        if(ae.getActionCommand().equals("5x5")){
             use = 5;
            grid = new JTextFieldGridutil(5 , 5);
            JPanel pan = grid.frame;
            frame.add(pan);
            pan.add(button);
             pan.revalidate();
        }
        if(ae.getActionCommand().equals("6x6")){
             use = 6;
            grid = new JTextFieldGridutil(6 , 6);
            JPanel pan = grid.frame;
            frame.add(pan);
            pan.add(button);
             pan.revalidate();
        }
        if(ae.getActionCommand().equals("11x11")){
             use = 11;
            grid = new JTextFieldGridutil(11 , 11);
            JPanel pan = grid.frame;
            frame.add(pan);
            pan.add(button);
             pan.revalidate();
        }
        if(ae.getActionCommand().equals("Create")){
            try{
        File file = new File("C:\\Users\\Marie\\Desktop\\" +use +".craft" );
        FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
        BufferedWriter dgf = new BufferedWriter(fw);
        JTextField[][] feild;
                feild = grid.grid;
        int i = 0;
        int n = 0; 

        
           dgf.newLine(); 
           try{
        for(i=0; i != use; i++){
            for(n=0; n!=use; n++){          
            String content = new String(feild[i][n].getText());
            dgf.write(content + "\t");
            }
        }
        dgf.close();
           }catch(Exception ex){
               System.out.print(ex);
           }
        }catch(IOException ioe){
                System.out.print(ioe);
            }
        }
        
        

    }
    public static void main(String[] args){
        new Craftingsystemcreator();
        
        frame.pack();
        frame.setVisible(true);
    }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1