Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Trying to disable buttons when they are not applicable

$
0
0
The program cycles through a data file showing the contents as you click the next and previous buttons. I'm trying to disable the previousButton when the vector's position is 0 and disable the nextButton when it is at 5. I put in println statements after each JButton is clicked so that I can tell where it is and where the disabling should be happening.
This is the contents of the data file:
apple
banana
cherry
melon
orange
strawberry


import java.io.*;
import java.util.Vector;
import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import javax.swing.*;

public class choosing_fruit extends JFrame implements ActionListener
{
	private JTextField dataItemTextField;
	private JButton nextButton, previousButton, closeButton;
	private Vector<String> v; 
	private int current = 0; // index value of current item shown in text field
	public static void main(String[] args) throws FileNotFoundException
	{
		choosing_fruit f = new choosing_fruit();
	}
	
	public choosing_fruit()
	{
		v = new Vector<String>();
		nextButton = new JButton(">>");
		previousButton = new JButton("<<");
		closeButton = new JButton("Close");
		dataItemTextField = new JTextField(20);
		setLayout(new FlowLayout());
				
		this.add(dataItemTextField);
		this.add(previousButton);
		this.add(nextButton);
		this.add(closeButton);
			
		nextButton.addActionListener(this);
		previousButton.addActionListener(this);
		closeButton.addActionListener(this);
		
		try
		{  
			Scanner in = new Scanner(new File("fruit.dat"));
			String inputLine;
			while(in.hasNextLine()) 
			{
				inputLine = in.nextLine();
				v.add(inputLine);
			}
			
			in.close();	
		}
		catch(Exception e)		
		{		
			JOptionPane.showMessageDialog(null,"error");	
		}

		dataItemTextField.setText(v.get(current)); // show 1st item

		this.setTitle("Choosing Fruit");
		this.pack();
		this.setVisible(true);
	}
	public void actionPerformed(ActionEvent ae)
	{	
		if(current > 0)
		{
			previousButton.setEnabled(true);
		}
		else
		{
			previousButton.setEnabled(false);
		}
		if(current < 5)
		{
			nextButton.setEnabled(true);
		}
		else
		{
			nextButton.setEnabled(false);
		}
		
		if(ae.getSource() == nextButton) 
		{
			if(current >= 0)
			{
				current = current + 1;
				dataItemTextField.setText(v.get(current));
				System.out.println(current);
			}			
		}
		
		else if(ae.getSource() == previousButton) 
		{
			if(current <= 5)
			{
				current = current - 1;
				dataItemTextField.setText(v.get(current));
				System.out.println(current);
			}	
		}
		else if(ae.getSource() == closeButton)
		{
			shutDown();
		}
		
	}
		private void shutDown()		
		{
			this.dispose();
		}
		
}



Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>