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: Help with my application

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with my application

    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 :

    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..

    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()).getLayoutCompon ent(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(""));
    }
    }


    }


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Help with my application

    To be fair, that's just too much code to post in one go.
    Edit your post to include code tags, as found in my signature and narrow the code down to where you think the issue might be occurring.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. how to run any installed application through my java application??
    By sgsamanthjain in forum Java Theory & Questions
    Replies: 1
    Last Post: April 1st, 2011, 08:17 AM
  2. Replies: 2
    Last Post: March 23rd, 2011, 08:51 AM
  3. Replies: 1
    Last Post: January 12th, 2011, 05:55 AM
  4. [SOLVED] [help] the application file (.jad) for application does not appear to be the ....
    By ordinarypeople in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: April 4th, 2010, 03:50 AM
  5. Replies: 0
    Last Post: December 3rd, 2009, 04:43 PM