Results 1 to 9 of 9
  1. #1
    berry88 is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default Help with Java Application please...

    I am implementing an application in Java using eclipse.
    I have one JFrame with BoarderLayout and some panels.
    Lets say in one panel I have a button and when I press it some images are shown in another panel.
    every time i press the button some numbers are read from a txt file and according to the numbers the images are shown. every new time the button is pressed different images should and are shown. tht problem is that it keeps the old images along with the new ones.
    I have tried :

    Java Code:
    			panel5 panel=new panel5();
    			add( panel,BorderLayout.CENTER);
    			panel.revalidate();
    It doesn't work though.
    Please help me.. I am posting my full code below..

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.io.File;
    import java.io.IOException;
    import java.awt.Choice;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowListener;
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Arrays;
    import java.util.Scanner;
    
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class test2 extends JFrame{
    	
    private JLabel label1=new JLabel("Secret Prints Project!!!", JLabel.CENTER);
    private JLabel label2=new JLabel("Please choose the cluster you wish to show:");
    private JLabel label3=new JLabel("Give a name for the cluster:");
    private JLabel label4=new JLabel("Enter the number of the letter you wish to delete from this cluster:");
    private JButton button1=new JButton("Show Cluster");
    private JButton button2=new JButton("Give Name");
    private JButton button3=new JButton("Delete letter");
    private JButton button4=new JButton("Done");
    private JTextField text1=new JTextField();
    private JTextField text2=new JTextField();
    private Choice ch=new Choice();
    int clickcount=0 ; 
    byte []data=new byte[1000];
    String str="";
    StringBuffer sb = new StringBuffer();		  
    File inputfile=new File("C:/Users/berry/Desktop/samples/clusters.txt");
    
    
    public void init() throws IOException{
    	JFrame j=new JFrame();
    	setLayout(new BorderLayout(5,5));
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	add(new panel1(),BorderLayout.NORTH);
    	add(new panel2(),BorderLayout.WEST);
    	add(new panel3(),BorderLayout.EAST);
    	add(new panel4(),BorderLayout.SOUTH);
    	
    	this.setSize(1200,600);
    	this.setVisible(true);
    	
    	
    	
    	
    }
    
    
    public class panel1 extends JPanel{
    	public panel1(){
    		add(label1);
    		label1.setForeground(new Color(0,0,240));
    		Font f = new Font("Freestyle Script", Font.PLAIN, 30);
    		label1.setFont(f);
    	}
    }
    
    public class panel2 extends JPanel implements ActionListener{
    	public panel2(){
    		
    		add(label2);
    		for (int i=0; i<30; i++){
        		int x=i+1;
        		String number=String.format("%d",x);
        		ch.addItem(number);
        	}
    		add(ch);
    		add(button1);
    		button1.addActionListener(this);
    		
    	}
    
    	public void actionPerformed(ActionEvent e) {
    		//remove(((BorderLayout)getLayout()).getLayoutComponent(BorderLayout.CENTER));
    
    		//clickcount=clickcount+1;
    		try{
    			int clusternumber=ch.getSelectedIndex()+1;
    			System.out.println(clusternumber);
    			int count=0 , num; 
    			StringBuffer sb = new StringBuffer();		  
    			Scanner fileScan = new Scanner(new File("C:/Users/berry/Desktop/samples/clusters.txt"));
    			
    			while (fileScan.hasNext())
    			  {
    			  num = fileScan.nextInt();
    			  count++;
    			  
    			  }
    
    			  // start a new scanner to "rewind" the file
    			  //fileScan.close();
    			  
    			  fileScan = new Scanner (new File("C:/Users/berry/Desktop/samples/clusters.txt"));
    
    			  File clusterfile=new File("C:/Users/berry/Desktop/samples/clusterstoshow.txt");
    			  FileOutputStream f=new FileOutputStream(clusterfile);
    			  int values[] = new int[count];
    			  
    			  for (int i = 0; i < values.length; i++)
    			  { 
    			  // this should be an if statement and not a while loop
    			  // because you want to read the file incrementally
    			  // for each index in the array, rather than read the
    			  // whole file for the first index (which you were doing
    			  // with the while loop)
    			  if (fileScan.hasNext())
    			  {
    				  num = fileScan.nextInt();
    				  values[i] = num;
    
    			  }
    			  }
    			  //int x=values[99];
    			  //System.out.println(values.length);
    
    			for (int i = 0; i < values.length; i++){
    				if (clusternumber==values[i]){
    				String output = Integer.toString(i+1);
    				int l = output.length();
    				    
    				for (int j=l+1; j<=3; j++){
    					output =new StringBuffer(output).insert(0,"0").toString();
    				}
    				System.out.println(output);
    				sb.append("\n"+output);
    				
    				if (f!=null){
    			  		for (int j=0; j<sb.length(); j++){
    			  			try {
    			  				
    							f.write((int)sb.charAt(j));
    						} catch (IOException e1) {
    							// TODO Auto-generated catch block
    							e1.printStackTrace();
    						}
    			  		}
    			  		
    			  	}
    		    //System.out.println(output);
    				}	
    				sb.setLength(0);
    			}
    			try {
    				f.close();
    			} catch (IOException e1) {
    				// TODO Auto-generated catch block
    				e1.printStackTrace();
    			}
    			
    		}catch(FileNotFoundException e1) {
    			System.out.println("Clusters file not found!");
    		}
    		//add(new panel6(),BorderLayout.CENTER);
    			panel5 panel=new panel5();
    			add( panel,BorderLayout.CENTER);
    			panel.revalidate();
    		
    		
    		
    	}
    }
    
    public class panel3 extends JPanel implements ActionListener{
    	public panel3(){
    		setLayout(new GridLayout(6,1,5,5));
    		add(label3);
    		add(text1);
    		add(button2);
    		button2.addActionListener(this);
    		add(label4);
    		add(text2);
    		add(button3);
    		button3.addActionListener(this);
    	}
    
    	
    	public void actionPerformed(ActionEvent e) {
    		if(e.getSource() == button2){
    			System.out.println("button2");
    		}else if(e.getSource() == button3){
    			System.out.println("button3");
    		}
    		
    	}
    }
    
    public class panel4 extends JPanel implements ActionListener{
    	public panel4(){
    		add(button4,JButton.CENTER);
    		button4.addActionListener(this);
    	}
    
    	public void actionPerformed(ActionEvent e) {
    		System.out.println("Done");
    		
    	}
    }
    
    public class panel5 extends JPanel {
    	public panel5(){
    	
    		GridLayout grid=new GridLayout(10,1,5,5);
    		setLayout(grid );
    		int count=0 , num; 
    		Scanner fileScan;		
    		
    		try {
    			fileScan = new Scanner(new File("C:/Users/berry/Desktop/samples/clusterstoshow.txt"));
    		
    
    	  	  while (fileScan.hasNext())
    	  	  {
    	  	  num = fileScan.nextInt();
    	  	  count++;
    	  	  
    	  	  }
    
    	  	  // start a new scanner to "rewind" the file
    	  	  //fileScan.close();
    	  	  
    	  	  fileScan = new Scanner (new File("C:/Users/berry/Desktop/samples/clusterstoshow.txt"));
    
    		  int values[] = new int[count];
    	  	  
    	  	  for (int i = 0; i < values.length; i++)
    	  	  { 
    	  	  // this should be an if statement and not a while loop
    	  	  // because you want to read the file incrementally
    	  	  // for each index in the array, rather than read the
    	  	  // whole file for the first index (which you were doing
    	  	  // with the while loop)
    	  	  if (fileScan.hasNext())
    	  	  {
    	  		  num = fileScan.nextInt();
    	  		  values[i] = num;
    
    	  	  }
    	  	  }
    	  	  System.out.println(values.length);
    	   	for (int i = 0; i < values.length; i++){
    	   		String output = Integer.toString(values[i]);
    	  		int l = output.length();
    	  		    
    	  		for (int k=l+1; k<=3; k++){
    	  			output =new StringBuffer(output).insert(0,"0").toString();
    	  		}
    	  		System.out.println(output);
    	  		Icon warnIcon = new ImageIcon("C:/Users/berry/Desktop/samples/letter"+output+".png");
    	  		add (new JLabel ("letter "+output, warnIcon,JLabel.RIGHT));
    	  		
    	  	}
    	   	  	
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    	}
    }
    public class panel6 extends JPanel{
    	public panel6(){
    		add(new JLabel(""));
    	}
    }
    
    	
    }
    Last edited by Fubarable; 08-13-2011 at 07:59 PM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    If you're swapping components, you will need to remove the old component(s) first, add the new component(s) then call revalidate and repaint on the container, not the added component. Better still is to swap using CardLayout.

  4. #4
    berry88 is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default

    can you please show me something like an example with code?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by berry88 View Post
    can you please show me something like an example with code?
    Better than that, I can show you the whole tutorial with better sample code than I can provide: How to use CardLayout.

    Cheers!

  6. #6
    berry88 is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default

    I didn't mean the cardlayout.. My application must show all the panels in the same frame so no cardlayout for me :(
    thanks for the link though.. :)

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by berry88 View Post
    I didn't mean the cardlayout.. My application must show all the panels in the same frame so no cardlayout for me :(
    Perhaps you want to show an image of what you want your application to look like, and an image of what it's looking like now. You also might wish to create and post an SSCCE, a small program that has no outside dependencies (no need for database, disk images, or other classes that are not part of core Java), that we can test, modify and improve.

  8. #8
    berry88 is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default

    Right!
    Here is an image of my frame when I press the button for the first time.. This is exactly how it should be like:

    Now here is the image after the second press of the button.. As you can see it is like the first and new images have added.. what should have happened was the previous images to be deleted and only the new ones to show..

    I am working on the code u asked for :) thanks

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. how to call Java Bean Ireport frm Java application
    By Prashant.surwade in forum Advanced Java
    Replies: 8
    Last Post: 05-06-2012, 03:39 PM
  2. Replies: 3
    Last Post: 12-04-2009, 12:10 PM
  3. Replies: 1
    Last Post: 08-05-2009, 02:43 AM
  4. Log on for GUI java application
    By VinTiger in forum New To Java
    Replies: 0
    Last Post: 03-29-2009, 07:37 PM
  5. java application
    By jithan in forum New To Java
    Replies: 4
    Last Post: 06-30-2008, 04:30 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
  •