Page 1 of 2 12 LastLast
Results 1 to 20 of 26
  1. #1
    cassysumandak is offline Member
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default Controlling loop using next button

    Hi everyone,

    I have problem with displaying images on JPanel or JLabel.
    How can I control the loop using next button?

    Here is a part of the code.

    int m = 0;
    String [ ] a = {"drawing1", "drawing2", "drawing3", "drawing"};
    List<String> files = Arrays.asList(a);
    Collections.shuffle(files);


    //Loop function
    for (i=0; i<4; i++) {
    readInputFile(files, i); // method to display the image (code working fine)
    .......... (my problem is here- how can i control this loop using next button)
    }


    Thanks.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    First of all you want to build the button click listener event. Do you know how to do it?

    On click event you can call the same process which you want to have in a loop.

  3. #3
    cassysumandak is offline Member
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default

    Hi,

    Thanks for the quick reply and help, however I have no idea on how the onclick event function.
    Is it same with when we click button e.g.:

    if (e.getSource()==buttonNext)
    do....

    I have used this before but it doesnt work in the loop. I am just too dumb in programming.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Are you already added a button in to your application. Can you show the code segment you have.

  5. #5
    cassysumandak is offline Member
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default

    This is a segment of the code I wrote:

    public class mainFrame extends JFrame implements ActionListener{
    private JButton next = new JButton("Next");
    .
    .
    drawingPanel draw = new drawingPanel();
    Public mainFrame () {
    .
    .
    //this is to display the drawing
    panel = new JPanel();
    .
    .
    panel.add(draw);

    //Button next
    .
    .
    c.add(next); next.addActionListener(this);


    //listener for the button
    public void actionPerformed(ActionEvent e){
    if (e.getSource () ==next){
    drawing = new drawingPanel();


    NEW CLASS
    //............................................
    public class drawingPanel (){
    .
    .
    int counter =0;
    String [ ] a = {"image1.txt", image2.txt", "image3.txt", "image4.txt"};
    List<String> files = Arrays.asList(a);
    Collections.shuffle(files);
    int counter;

    for (counter = 0; counter < 4; counter++) {
    readInputFile(files,counter);
    }

    //read the Inputfile and draw it - work fine
    public void readInputFile(List<String> a, int counter) {
    File fil = new File(a.get(counter));
    .
    .
    }

    //Drawing
    public void paintComponent()

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Fine, you have done that click event handling part.

    Java Code:
    //listener for the button
    public void actionPerformed(ActionEvent e){
         if (e.getSource () ==next){
             // Rest of the code
         }
    }
    Now the thing is who to call that image display method from the other class. I can see that you have initiate the second class within the click event. That's not good. Initiate outside of the click event and call a public method of the other class.

  7. #7
    cassysumandak is offline Member
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default

    Hi, Thxs so much for the hint on how to do it...
    Now I have another problem, I dont know how to display it on the JPanel, since now i only use one class.


    public class mainFrame extends JFrame implements ActionListener{

    private JButton answer1 = new JButton;
    private JButton answer2 = new JButton;
    private JButton answer3 = new JButton;
    private JButton answer4 = new JButton;
    private JButton answer5 = new JButton;
    private JButton answer6 = new JButton;

    JPanel panel;

    int fileNumber = 0;
    int totalFiles;

    String[] a = {"image1.txt", "image2.txt",image3.txt",image4.txt" };
    List<String> files = Arrays.asList(a);


    //CONSTRUCT THE FRAME
    public mainFrame(){

    Collections.shuffle(files);
    ......
    GridBagLayout m = new GridBagLayout();
    Container c = (Container)getContentPane();
    c.setLayout (m);
    GridBagConstraints con;

    panel = new JPanel();
    panel.setPreferredSize(new Dimension(600,600));
    panel.setBackground(Color.white);
    con = new GridBagConstraints();
    con.gridy = 2; con.gridx = 0;
    con.gridwidth = 50; con.gridheight = 10;
    con.fill = GridBagConstraints.BOTH;
    m.setConstraints(panel, con);
    c.add(panel);

    //Button
    con = new GridBagConstraints();
    con.anchor=GridBagConstraints.EAST;
    con.gridy = 3; con.gridx = 50;
    con.insets= new Insets(0,10,3,0);
    m.setConstraints(answer1, con);
    c.add(answer1); answer1.addActionListener(this);
    .............

    functionLoadInterface();

    setSize(new Dimension(780,700));
    setVisible(true);
    }

    public void functionLoadInterface () {
    answer1.addActionListener(this);
    answer2.addActionListener(this);
    answer3.addActionListener(this);
    answer4.addActionListener(this);
    answer5.addActionListener(this);
    answer6.addActionListener(this);

    panel.add () -------------> I dont know what method to put on the bracket so that the drawing will be displayed on the JPanel
    }

    public void actionPerformed(ActionEvent e) {
    fileNumber++;
    this.readInputFile(files, totalFiles);
    }

    // Code working fine
    public void readInputFile(List<String> files, int totalFiles) {}
    public void paintComponent(Graphics g) { }

    }

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

    Default

    It's kind of hard to read your code.

    When posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]

  9. #9
    cassysumandak is offline Member
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default

    Dear Fubarable,

    Im sorry I did not realised that,I will definitly do it next time. Thanks.

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

    Default

    You still can do it with the code currently posted, and it won't take much effort to do. Why not edit your code-post above and give it a try?

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

    Default

    Also, I would advise you to avoid the GridBagLayout if possible and to instead concentrate on using several simpler layouts in several nested JPanels. Usually this is easier to set up and maintain.

  12. #12
    cassysumandak is offline Member
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default

    Here is the segment of the code.
    I really have no idea, how to display the drawing on JPanel.


    Java Code:
    public class mainFrame extends JFrame implements ActionListener{
    	 
    	private JButton answer1 = new JButton ();
    	private JButton answer2 = new JButton ();
    	private JButton answer3 = new JButton ();
    	private JButton answer4 = new JButton ();
    	private JButton answer5 = new JButton ();
    	private JButton answer6 = new JButton ();
    		
    	JPanel panel;
    	
    	int []x = new int[300];
    	int i, j;
            int TriaNum; 
    	int fileNumber = 0;
    	int totalFiles;
    
    	String[] a = {"image1.txt", "image2.txt","image3.txt","image4"};
    	List<String> files = Arrays.asList(a);
    
    
    	//CONSTRUCT THE FRAME
    	public mainFrame(){		
    		Collections.shuffle(files);	
    
    		GridBagLayout m = new GridBagLayout();
    		Container c = (Container)getContentPane();
    		c.setLayout (m);
    		GridBagConstraints con;
    		
    		panel = new JPanel();
    		panel.setPreferredSize(new Dimension(600,600));	
    		panel.setBackground(Color.white);
    		con = new GridBagConstraints();
    		con.gridy = 2; con.gridx = 0;
    		con.gridwidth = 50; con.gridheight = 10;		
    		con.fill = GridBagConstraints.BOTH;
    		m.setConstraints(panel, con);
    		c.add(panel);
    	
    		//Button
    		con = new GridBagConstraints();
    		con.anchor=GridBagConstraints.EAST;
    		con.gridy = 3; con.gridx = 50;
    		con.insets= new Insets(0,10,3,0);
    		m.setConstraints(answer1, con);
    		c.add(answer1); answer1.addActionListener(this);
    
    		con.anchor=GridBagConstraints.EAST;
    		con.gridy = 4; con.gridx = 50;
    		con.insets= new Insets(10,10,3,0);
    		m.setConstraints(answer2, con);
    		c.add(answer2);answer2.addActionListener(this);
    
    		con.anchor=GridBagConstraints.EAST;
    		con.gridy = 5; con.gridx = 50;
    		con.insets= new Insets(10,10,3,0);
    		m.setConstraints(answer3, con);
    		c.add(answer3);answer3.addActionListener(this);
    
    		con.anchor=GridBagConstraints.EAST;
    		con.gridy = 6; con.gridx = 50;
    		con.insets= new Insets(10,10,3,0);
    		m.setConstraints(answer4, con);
    		c.add(answer4);answer4.addActionListener(this);
    
    		con.anchor=GridBagConstraints.EAST;
    		con.gridy = 7; con.gridx = 50;
    		con.insets= new Insets(10,10,3,0);
    		m.setConstraints(answer5, con);
    		c.add(answer5);answer5.addActionListener(this);
    
    		con.anchor=GridBagConstraints.EAST;
    		con.gridy = 8; con.gridx = 50;
    		con.insets= new Insets(10,10,3,0);
    		m.setConstraints(answer6, con);
    		c.add(answer6);answer6.addActionListener(this);
    				
    		functionLoadInterface();
    		panel.add (); ------> I have no idea class/method to put on this bracket.
    
    		setSize(new Dimension(780,700));
    		setVisible(true);			
    	}
        
        public void functionLoadInterface () {
        	answer1.addActionListener(this); 
        	answer2.addActionListener(this); 
        	answer3.addActionListener(this); 
        	answer4.addActionListener(this); 
        	answer5.addActionListener(this); 
        	answer6.addActionListener(this);	
        	}
        
    	public void actionPerformed(ActionEvent e) {
    		fileNumber++;
    		this.readInputFile(files, totalFiles);
    		}
    
    	public void readInputFile(List<String> files, int totalFiles) {
    		File fil = new File(files.get(totalFiles));
    		FileReader inputFil;
    		try {		
    			try{
    				inputFil = new FileReader(fil);
    				BufferedReader in = new BufferedReader(inputFil);
    				String s = in.readLine();
    				i = 0;
    				TriaNum = 0;
    				while (s!=null){
    					StringTokenizer st = new StringTokenizer (s);
    					while (st.hasMoreTokens()){
    						x[i]=Integer.parseInt(st.nextToken());
    						i++;
    						}
    					s = in.readLine();
    						TriaNum++; // line numbers
    						}
    					}
    			catch (IOException e) {
    				System.err.println( e.getMessage () );
    				System.exit(1);
    				}
    			}
    			finally{
    				}
    			}
    
    	public void paintComponent(Graphics g){
    		super.paintComponents(g);
    		int z=0;
    		for (int j=0 ; j<TriaNum;j++){			
    			g.drawLine(x[z], x[z+1], x[z+2], x[z+3]);
    			z = z+4; //index of the array
    			}
    		}
    	
    	}

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

    Default

    I really have no idea, how to display the drawing on JPanel.
    Fortunately, Sun gives you guidance:
    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI with JFC/Swing)
    Trail: 2D Graphics (The Java™ Tutorials)

  14. #14
    cassysumandak is offline Member
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default

    Dear Fubarable,
    The drawing works fine, but I dont know the syntax to put on the JPanel bracket. I used to call different class before, but now since I only use one class, I have no idea which method should I put on the bracket.


    Java Code:
    	panel.add ( ); --> ??

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

    Default

    I'm not clear on this question. what do you mean by putting a method "on a bracket"? And how can the drawing work fine here when your trying to place a paintComponent override within a class that extends JFrame. JFrame doesn't have a paintComponent method, and so your method will never be called. I think that you instead will want to create an inner anonymous JPanel object that uses this paintComponent method override.

  16. #16
    cassysumandak is offline Member
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default

    Dear Fubarable,

    Is that mean I need to create another class ..extend JPanel?

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

    Default

    Yes in some way or fashion you will need to extend JPanel, but if you are required to do everything in one class (a silly requirement if you ask me, but you / we must do what our bosses / customers / instructors require of us, no matter how silly), then you can create an anonymous inner JPanel object:

    Java Code:
    public class mainFrame extends JFrame implements ActionListener
    {
    
      // this is crying out to be placed in some array
      // or another
      private JButton answer1 = new JButton();
      private JButton answer2 = new JButton();
      private JButton answer3 = new JButton();
      private JButton answer4 = new JButton();
      private JButton answer5 = new JButton();
      private JButton answer6 = new JButton();
    
      JPanel panel = new JPanel()
      {
        @Override
        protected void paintComponent(Graphics g)
        {
          super.paintComponent(g);
          // do my painting here'
          // sometimes I just place a method call here like
          // myPaint(g); and have a private void myPaint(Graphics g)...
          // method in the main class where I do my drawing.
        }
      };
    
      int[] x = new int[300];
      int i, j;
      int TriaNum;
      int fileNumber = 0;
      int totalFiles;

  18. #18
    cassysumandak is offline Member
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default

    The drawing didnt comes out....


    Java Code:
    First class
    
    public class mainPage {
    	public static void main(String[]args){
    		JFrame appFrame = new mainFrame();
    		appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		appFrame.show();
    	}
    }
    
    ***Second class:
    public class mainFrame extends JFrame implements ActionListener{
    	
    	private JButton answer1 = new JButton();
    	private JButton answer2 = new JButton();
    	private JButton answer3 = new JButton();
    	private JButton answer4 = new JButton();
    	private JButton answer5 = new JButton();
    	private JButton answer6 = new JButton();
    		
    	JPanel panel;
    	int fileNumber = 0;
            String[] a = {"image1.txt", "image2.txt","image3.txt","image4"};
            List<String> files = Arrays.asList(a);
    			
    	//CONSTRUCT THE FRAME
    	public mainFrame(){		
    	
    		Collections.shuffle(files);	
    
    		functionLoadInterface();
    
    		GridBagLayout m = new GridBagLayout();
    		Container c = (Container)getContentPane();
    		c.setLayout (m);
    		GridBagConstraints con;
    		
    		JLabel label1 = new JLabel("CALCULATE THE NUMBER OF TRIANGLE(S)");
    		con = new GridBagConstraints();
    		con.anchor=GridBagConstraints.NORTHWEST;
    		con.gridy = 0; con.gridx = 0;
    		con.insets= new Insets(0,0,10,0);
    		m.setConstraints(label1, con);
    		c.add(label1);
    
    		JLabel label2 = new JLabel("ANSWER");
    		con = new GridBagConstraints();
    		con.anchor=GridBagConstraints.CENTER;
    		con.gridy = 0; con.gridx = 50;
    		con.insets= new Insets(0,0,10,0);
    		m.setConstraints(label2, con);
    		c.add(label2);
    
    		panel = new JPanel();
    		panel.setPreferredSize(new Dimension(600,600));	
    		panel.setBackground(Color.white);
    		con = new GridBagConstraints();
    		con.gridy = 2; con.gridx = 0;
    		con.gridwidth = 50; con.gridheight = 10;		
    		con.fill = GridBagConstraints.BOTH;
    		m.setConstraints(panel, con);
    		c.add(panel);
    
    		//Button
    		con = new GridBagConstraints();
    		con.anchor=GridBagConstraints.EAST;
    		con.gridy = 3; con.gridx = 50;
    		con.insets= new Insets(0,10,3,0);
    		m.setConstraints(answer1, con);
    		c.add(answer1);
    
    		con.anchor=GridBagConstraints.EAST;
    		con.gridy = 4; con.gridx = 50;
    		con.insets= new Insets(10,10,3,0);
    		m.setConstraints(answer2, con);
    		c.add(answer2);
    
    		con.anchor=GridBagConstraints.EAST;
    		con.gridy = 5; con.gridx = 50;
    		con.insets= new Insets(10,10,3,0);
    		m.setConstraints(answer3, con);
    		c.add(answer3);
    
    		con.anchor=GridBagConstraints.EAST;
    		con.gridy = 6; con.gridx = 50;
    		con.insets= new Insets(10,10,3,0);
    		m.setConstraints(answer4, con);
    		c.add(answer4);
    
    		con.anchor=GridBagConstraints.EAST;
    		con.gridy = 7; con.gridx = 50;
    		con.insets= new Insets(10,10,3,0);
    		m.setConstraints(answer5, con);
    		c.add(answer5);
    
    		con.anchor=GridBagConstraints.EAST;
    		con.gridy = 8; con.gridx = 50;
    		con.insets= new Insets(10,10,3,0);
    		m.setConstraints(answer6, con);
    		c.add(answer6);
    						
    		setSize(new Dimension(780,700));
    		setVisible(true);			
    	}
        
        public void functionLoadInterface () {
        	answer1.addActionListener(this); 
        	answer2.addActionListener(this); 
        	answer3.addActionListener(this); 
        	answer4.addActionListener(this); 
        	answer5.addActionListener(this); 
        	answer6.addActionListener(this);	
        	}
        
    	public void actionPerformed(ActionEvent e) {
    		if (e.getSource() == answer1){
    			fileNumber++;
    			drawingPanel draw = new drawingPanel(files,fileNumber);			
    			panel.add(draw);
    			}
                           .................
    		}
    	}
    
    .....Third class
    public class drawingPanel extends JPanel{
    	
    	private static final int PANEL_WIDTH 	= 610;
    	private static final int PANEL_HEIGHT 	= 610;
    
    	int []x = new int[300];
    	int i, j;
        int TriaNum;
    	
        
    	public drawingPanel(List<String> files, int fileNumber){
    		setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));	
    		readInputFile(files, fileNumber);
    	}
    
    	private void readInputFile(List<String> files, int fileNumber) {
    		File fil = new File(files.get(fileNumber));
    		FileReader inputFil;
    		try {		
    			try{
    				inputFil = new FileReader(fil);
    				BufferedReader in = new BufferedReader(inputFil);
    				String s = in.readLine();
    				i = 0;
    				TriaNum = 0;
    				while (s!=null){
    					StringTokenizer st = new StringTokenizer (s);
    					while (st.hasMoreTokens()){
    						x[i]=Integer.parseInt(st.nextToken());
    						i++;
    						}
    					s = in.readLine();
    						TriaNum++; // line numbers
    						}
    					}
    			catch (IOException e) {
    				System.err.println( e.getMessage () );
    				System.exit(1);
    				}
    			}
    			finally{
    				}	
    			}
    
    	public void paintComponent(Graphics g){
    		super.paintComponents(g);
    		int z=0;
    		for (int j=0 ; j<TriaNum;j++){			
    			g.drawLine(x[z], x[z+1], x[z+2], x[z+3]);
    			z = z+4; //index of the array
    			}
    		}
    	
    
    	}

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

    Default

    what happens instead? I don't see any debug statements in your code.

    Also, your code has little meaning to us in that there are dependencies that we have no access to (text files, images, etc...). I strongly suggest that you try to figure out your problem in a simpler system, a program that just does the single that that you are trying to accomplish here: draw on a JPanel, show the drawing, and perhaps change it on button press. This small program would not read any text files or have any of your program logic, it would just try to solve your current problem and nothing else. And then if it doesn't, you can post that code here, and we can work out it on our boxes if necessary. this process is known as creating an SSCCE.
    Last edited by Fubarable; 03-21-2009 at 08:15 PM.

  20. #20
    cassysumandak is offline Member
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default

    nothing comes out in the JPanel...it shows empty JPanel..Im really in dead road now

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: 03-03-2009, 01:46 AM
  2. Controlling Page Scroll Position in Jsp....f there are fewer than 4 pages only that n
    By 82rathi.angara in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 07-05-2008, 03:11 PM
  3. Using SWT Button
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 10:05 AM
  4. Controlling method calls
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-04-2008, 02:14 PM
  5. controlling GC
    By ravian in forum Eclipse
    Replies: 2
    Last Post: 01-03-2008, 09:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •