14 Replies - 383 Views - Last Post: 18 May 2014 - 03:10 PM Rate Topic: -----

#1 jbauer1849  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-May 14

JButton cycling

Posted 17 May 2014 - 05:05 PM

		JButton btnNext = new JButton("Next");
		btnNext.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				for (int i = 0; i < bkSorted.length; i++)
				{
					textArea.append("\n" + bkSorted[i+1]);
				}
			}
		});




When I click the button, it displays the whole array, instead of just the next array position. What am I doing wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: JButton cycling

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1424
  • View blog
  • Posts: 3,166
  • Joined: 05-April 11

Re: JButton cycling

Posted 17 May 2014 - 05:26 PM

You wouldn't need a loop to just show the next value, would you :) ?
Was This Post Helpful? 0
  • +
  • -

#3 jbauer1849  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-May 14

Re: JButton cycling

Posted 17 May 2014 - 07:36 PM

How else would I do it that will keep cycling through the array every time I click the button (one position for each button click)?
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,996
  • Joined: 20-September 08

Re: JButton cycling

Posted 18 May 2014 - 03:08 AM

Keep an instance variable to mark the current position:

       JButton btnNext = new JButton("Next");
        btnNext.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    textArea.append(String.format("%n%s",
                            bkSorted[currentPosition])); // Use platform-apt linefeeds
                    currentPosition++;
                    currentPosition %= bkSorted.length; // Wrap if necessary
                }
            });


Was This Post Helpful? 1
  • +
  • -

#5 jbauer1849  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-May 14

Re: JButton cycling

Posted 18 May 2014 - 10:26 AM

How would I find the current position without a for loop, but still only show one position of the array?
Was This Post Helpful? 0
  • +
  • -

#6 jbauer1849  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-May 14

Re: JButton cycling

Posted 18 May 2014 - 11:45 AM

This is the current code I have, but it still not working. It goes to the same position every time.
		JButton btnNext = new JButton("Next");
		btnNext.addActionListener(new ActionListener()
		{
			
			public void actionPerformed(ActionEvent e)
			{
				int i = bkSorted.length - 1;
				textArea.setText(null);
				if (i != bkSorted.length)
				textArea.append(String.format("%n%s", bkSorted[i]));
				i++;
				i%= bkSorted.length;
			}});



Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,996
  • Joined: 20-September 08

Re: JButton cycling

Posted 18 May 2014 - 11:59 AM

Quote

This is the current code I have, but it still not working.
Have you considered the possibility that it might be something to do with the fact that you did not use the code i gave you?
Was This Post Helpful? 0
  • +
  • -

#8 jbauer1849  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-May 14

Re: JButton cycling

Posted 18 May 2014 - 12:14 PM

I thought currentPosition was a variable I had to make. Ops my fault. Guess I need hit the books more. Thanks for your help
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,996
  • Joined: 20-September 08

Re: JButton cycling

Posted 18 May 2014 - 12:21 PM

Quote

I thought currentPosition was a variable I had to make
It is. As per my instruction:

Quote

Keep an instance variable to mark the current position:
Which variable i meant i thought was too obvious to state ;)/>

You do know what an 'instance variable' is? If not, then please ask.

This post has been edited by g00se: 18 May 2014 - 12:22 PM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

#10 jbauer1849  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-May 14

Re: JButton cycling

Posted 18 May 2014 - 01:05 PM

I know what an instance variable is, just thought there might been more then one way to get the position. I have the following code at the beginning of my class.

int i = 0;



And this for my button.
JButton btnNext = new JButton("Next");
btnNext.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent e)
    {
	textArea.setText(null);
	textArea.append(String.format("%n%s", bkSorted[i]));
	i++;
	i %= bkSorted.length;
    }});



Eclipse is giving me an error saying I need make i a final variable, which won't work with the code for the Next button since finals can't be changed. On the other hand, if I declare and set the variable inside the ActionListener method, it will do me no good because it will always give me the same value of the array bkSorted. So still confused on how to make this work.
Was This Post Helpful? 0
  • +
  • -

#11 mike73  Icon User is offline

  • D.I.C Addict

Reputation: 164
  • View blog
  • Posts: 645
  • Joined: 24-April 10

Re: JButton cycling

Posted 18 May 2014 - 01:10 PM

It's because your action listener is an anonymous inner class. An anonymous class cannot access local variables in its enclosing scope that are not declared as final or effectively final. You could make it an inner class.

Further Reading

This post has been edited by mike73: 18 May 2014 - 01:12 PM

Was This Post Helpful? 0
  • +
  • -

#12 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,996
  • Joined: 20-September 08

Re: JButton cycling

Posted 18 May 2014 - 01:49 PM

Or make it an instance variable of the listener. Named inner classes are usually more flexible and readable
Was This Post Helpful? 0
  • +
  • -

#13 jbauer1849  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-May 14

Re: JButton cycling

Posted 18 May 2014 - 02:32 PM

This is now my code

		//setup Next button
		btnNext = new JButton("Next");
		btnNext.addActionListener(new ActionListener(this)
		GridBagConstraints g_Next = new GridBagConstraints();
		g_Next.insets = new Insets(5, 5, 5, 0);
		g_Next.gridx = 2;
		g_Next.gridy = 7;
		panel.add(btnNext, g_Next);



	public actionPerformed(ActionEvent e)
	{
		if (e.getSource() == btnNext)
		{
			textArea.append(String.format("%n%s", bkSorted[i]));
			i++;
			i %= bkSorted.length;
		}



I have the class extending JFrame and implenting ActionListener, but its giving me the following error
Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
	Cannot use this in a static context
	Syntax error, insert ")" to complete MethodInvocation
	Syntax error, insert ";" to complete Statement
	Cannot use this in a static context
	Syntax error, insert ")" to complete MethodInvocation
	Syntax error, insert ";" to complete Statement
	Cannot use this in a static context
	Syntax error, insert ")" to complete MethodInvocation
	Syntax error, insert ";" to complete Statement
	Cannot use this in a static context
	Syntax error, insert ")" to complete MethodInvocation
	Syntax error, insert ";" to complete Statement
	void is an invalid type for the variable actionPerformed
	Syntax error on token "(", ; expected
	Syntax error on token ")", ; expected

	at Bookstore.main(Bookstore.java:120)



Line 120 is this line
		JButton btnNext = new JButton("Next");



It now will not even compile right. What did I do wrong?
Was This Post Helpful? 0
  • +
  • -

#14 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,996
  • Joined: 20-September 08

Re: JButton cycling

Posted 18 May 2014 - 03:03 PM

Quote

btnNext.addActionListener(new ActionListener(this)
should be

btnNext.addActionListener(this);


but that line should not appear in a static method. It should appear in a non-static method of the class that implements ActionListener
Was This Post Helpful? 0
  • +
  • -

#15 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,996
  • Joined: 20-September 08

Re: JButton cycling

Posted 18 May 2014 - 03:10 PM

Here's a working example to compile, run and study:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class FB extends JFrame implements ActionListener {
    private int currentPosition;
    private JTextArea textArea;
    private final String[] bkSorted = { "Alpha", "Beta", "Gamma" };
    private JButton btnNext = new JButton("Next");

    public void actionPerformed(java.awt.event.ActionEvent e) {
        textArea.setText(null);
        textArea.append(String.format("%n%s", bkSorted[currentPosition]));
        currentPosition++;
        currentPosition %= bkSorted.length;
    }

    private void setGui() {
        try {
            setLocation(0, 100);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            Container cp = getContentPane();
            textArea = new JTextArea();
            cp.add(textArea, BorderLayout.CENTER);

            JPanel bPanel = new JPanel();

            btnNext.addActionListener(this);

            bPanel.add(btnNext);
            cp.add(bPanel, BorderLayout.SOUTH);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                    public void run() {
                        FB f = new FB();
                        f.setGui();
                        f.setSize(200, 200);
                        f.setVisible(true);
                    }
                });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1