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