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 17 of 17

Thread: Help Please! :(

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Location
    philippines
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Unhappy Help Please! :(

    Why does not showing the object?

    This my project. I would like to show the object using text file and store in JTable per row and line by line and split it to "-".
    here the example of textfile or database.txt:
    07-12345-Wilson-Dacles-T.-February 1, 1995-male-lordwils02@gmail.com-cuasay st.
    21-1234-Wilson-Dacles-T.-February 1, 1995-male-lordwils02@gmail.com-cuasay st.

    Then, here is the output of my JTable like that I said the object is does'nt show:
    sasasasaaaaa.jpg

    Sorry for my grammar.! I'm veteran for filipino language.. I hope you help me!!

    try{
    		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("Database.txt"))));
    		String line = "";
    		int width = 0, height = 0;
     
    		while((line = br.readLine())!=null){
     
    		if(width == 0){
     
    		String data[] = line.split("-");
    		width = data.length;
     
    			}
    		height++;
     
    		String matrix[][] = new String[height][width];
    		model = new DefaultTableModel(matrix, col);
    		table = new JTable(model){
    		public boolean isCellEditable(int row, int column){
    		return false;
    		}};
    		}
    	}catch (IOException ex){
    		     ex.printStackTrace();
    }

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

    Default Re: Help Please! :(

    would like to show the object
    What is "the object" and how do you want to show it? As an image? As lines in a text area? How???

    Please make a small, simple program that compiles, executes and shows the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Help Please! :(

    Well during your while loop you really aren't storing any data at all. You create the 2D String array afterwards, but all of it's contents are null. You never actually get any data from the input file

  4. #4
    Junior Member
    Join Date
    Jan 2013
    Location
    philippines
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help Please! :(

    Like this, and i would like to automatic display object on text file and put the jtable, but doesn't show
    aaaaaaaaaaaa.jpg

    Here is my full code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.table.*;
    import java.io.*;
    import java.util.*;
     
    public class Administrator extends JFrame {
     
    		private String col []= {"Account No.","PinCode","First Name",
    		"Last Name", "MI","BirthDate","Sex","E-mail","Address"};
    		private JScrollPane scroll;
    		private JTable table;
    		private DefaultTableModel model;
     
         public Administrator(){
    		//ImageIcon bcr = new ImageIcon(getClass().getResource("/images/bcr.jpg"));
    		final JButton back = new JButton("back");
    		back.setLocation(20,480);
    		back.setSize(70,30);
    		back.setPreferredSize(new Dimension(90,30));
    		back.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    			dispose();
    			}
    		}
    		);
    		try{
     
    			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("Database.txt"))));
     
    			String line = "";
     
    			int width = 0, height = 0;
     
    			while((line = br.readLine())!=null){
     
    			if(width == 0){
     
    			String data[] = line.split("-");
    			width = data.length;
     
    			}
    			height++;
     
    			String matrix[][] = new String[height][width];
    			model = new DefaultTableModel(matrix, col);
    			table = new JTable(model){
    			public boolean isCellEditable(int row, int column){
    			return false;
    				}
    			};
    		}
    		}
    		catch (IOException ex){
    		ex.printStackTrace();
    		}
     
    		DefaultTableCellRenderer cent = new DefaultTableCellRenderer();
    		cent.setHorizontalAlignment( JLabel.CENTER );
    		table.getColumnModel().getColumn(0).setCellRenderer(cent);
    		table.getColumnModel().getColumn(1).setCellRenderer(cent);
    		table.getColumnModel().getColumn(2).setCellRenderer(cent);
    		table.getColumnModel().getColumn(3).setCellRenderer(cent);
    		table.getColumnModel().getColumn(4).setCellRenderer(cent);
    		table.getColumnModel().getColumn(5).setCellRenderer(cent);
    		table.getColumnModel().getColumn(6).setCellRenderer(cent);
    		table.getColumnModel().getColumn(7).setCellRenderer(cent);
    		table.getColumnModel().getColumn(0).setPreferredWidth(100);
    		table.getColumnModel().getColumn(1).setPreferredWidth(100);
    		table.getColumnModel().getColumn(2).setPreferredWidth(100);
    		table.getColumnModel().getColumn(3).setPreferredWidth(100);
    		table.getColumnModel().getColumn(4).setPreferredWidth(50);
    		table.getColumnModel().getColumn(5).setPreferredWidth(100);
    		table.getColumnModel().getColumn(6).setPreferredWidth(50);
    		table.getColumnModel().getColumn(7).setPreferredWidth(120);
     
    		scroll = new JScrollPane(table);
    		scroll.setLocation(20, 320);
    		scroll.setSize(750,150);
     
    		Container pane = getContentPane();
    		pane.setLayout(null);
    		pane.add(back);
    		pane.add(scroll);
     
    		/*setIconImage(new ImageIcon("/images/icon.jpg").getImage());
    		setLayout(new BorderLayout());
    		JLabel www = new JLabel(new ImageIcon(getClass().getResource("/images/admin.jpg")));
    		add(www);*/
    		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    		setTitle("Account Profile");
    		setSize(800,550);
    		setResizable(false);
    		setVisible(true);
    		setLocation(110, 30);
         }
         public static void main(String[]args){
         	new Administrator();
         }
    }

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help Please! :(

    Where does the code assign any Strings to the matrix array? See post#3
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Jan 2013
    Location
    philippines
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help Please! :(

    Can you give me some sample or simple code sir Parranoia? sorry for the disturbance . I really really hard to forget.. cause I am the beginner..

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help Please! :(

    Another problem is that the model and table should be created AFTER all the data has been read from the file and stored in the array. The posted code has that code inside of the loop so it will create a model and table for every line that is read from the file instead of one time after all the lines have been read.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Jan 2013
    Location
    philippines
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help Please! :(

    so what should I do Mr.Norm ?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help Please! :(

    begin loop to read lines
    Read the lines from the file
    break the lines into parts
    save the lines
    end loop reading lines
    create the model
    create the table
    If you don't understand my answer, don't ignore it, ask a question.

  10. The Following User Says Thank You to Norm For This Useful Post:

    shen_punkz21 (March 1st, 2013)

  11. #10
    Junior Member
    Join Date
    Jan 2013
    Location
    philippines
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help Please! :(

    ok tnx ..

    --- Update ---

    Wait MR. Norm ,, how can i do for using 2d array ?

  12. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help Please! :(

    Think of a 2 dim array as an array of arrays. A one dim array can be assigned to the first dim of a two dim array
      int[][] twoDim = new int[4][];      //  define 2 dim array, leaving second dim size unassigned
      twoDim[0] = new int[]{1,2,3};    //  assign an array to first dim of twoDim
      twoDim[1] = new int[]{5,6,7,8};  //  assign a different sized array to second dim of twoDim
      System.out.println(java.util.Arrays.deepToString(twoDim)); //  print contents of twoDim
    If you don't understand my answer, don't ignore it, ask a question.

  13. The Following User Says Thank You to Norm For This Useful Post:

    shen_punkz21 (March 1st, 2013)

  14. #12
    Junior Member
    Join Date
    Jan 2013
    Location
    philippines
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help Please! :(

    Mr. Norm:

    Actually it works but the first row only ..How can i loop this height or other row.?
    here is the image of the result:
    nnnaaaannnaa.jpg

    here is my code:
    try{
     
    			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("Database.txt"))));
     
    			String line = "";
    			int j = 0;
     
    			line = br.readLine();
    			String data[] = line.split("\n");
     
    			String matrix[][] = new String[data.length][];
     
    			for(String row: data){
    				matrix[j++] = row.split("-");
    			}
     
    			model = new DefaultTableModel(matrix, col);
    			table = new JTable(model){
    			public boolean isCellEditable(int row, int column){
    			return false;
    				}
    			};
     
    		}
    		catch (IOException ex){
    		ex.printStackTrace();
    		}

  15. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help Please! :(

    Where is the loop to read all the lines in the file?
    If you don't understand my answer, don't ignore it, ask a question.

  16. #14
    Junior Member
    Join Date
    Jan 2013
    Location
    philippines
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help Please! :(

    can you please help me ..

  17. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help Please! :(

    Make a loop to read the lines from the file and save them in the array.
    The code in post#4 had the loop but there was extra code inside the loop that should be moved out.
    The code in post#12 puts one line in the array ok. It needs to be changed to put all of the lines in the array using a loop like in post#4
    If you don't understand my answer, don't ignore it, ask a question.

  18. #16
    Junior Member
    Join Date
    Jan 2013
    Location
    philippines
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help Please! :(

    can you edit my code ?

  19. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help Please! :(

    You should do it since it is your program and you will be getting the credit for it.
    The more programming you do, the more you will learn. If someone else does it, you will learn very little.
    If you don't understand my answer, don't ignore it, ask a question.