7 Replies - 810 Views - Last Post: 27 January 2016 - 03:14 AM Rate Topic: -----

#1 daredavel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 26-January 16

Controlling the text field character limit using radio buttons?

Posted 26 January 2016 - 08:51 AM

Hello guys, I'm a newbie at Java programming and I'm trying to make my first printer application.

I've been having problem and error in setting up the event of my radio buttons as I would like to control the character limit of my "textField" to 8 and 13 digits respectively. I'm not really good at using listeners as I'm still new to programming. Any help is welcome for me to partially complete my first project.

Here's my script:
package Barcode;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.KeyStroke;
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.Event.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import javax.swing.JButton;
import javax.swing.text.PlainDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import java.awt.Color;
import javax.swing.JOptionPane;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.*;
import javax.swing.WindowConstants;
import javax.swing.SwingConstants;
import java.awt.print.*;
import java.io.*;
import javax.swing.JPanel;
import javax.comm.*;
import javax.comm.ParallelPort;

//import javax.swing.JTextField;
//import java.awt.BorderLayout;
//import javax.swing.JPanel;
//import javax.swing.border.EmptyBorder;
//import java.awt.window.Type;

public class Barcoder extends JFrame {
	private static final long serialVersionUID = 1L;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
		
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Barcoder f = new Barcoder();
					f.setVisible(true);
										
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	/*/
	 * text field limit
	 */
	class JTextFieldLimit extends PlainDocument {
		private static final long serialVersionUID = 1L;
		
		  private int limit;
		  JTextFieldLimit(int limit) {
		    super();
		    this.limit = limit;
		  }

		  JTextFieldLimit(int limit, boolean upper) {
		    super();
		    this.limit = limit;
		  }

		  public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
		    if (str == null)
		      return;

		    if ((getLength() + str.length()) <= limit) {
		      super.insertString(offset, str, attr);
		    }
		  }
		}
	/**
	 * Create the frame.
	 */
	public Barcoder() {			
		getContentPane().setBackground(Color.ORANGE);
		getContentPane().setFont(new Font("SansSerif", Font.BOLD | Font.ITALIC, 13));
		setForeground(Color.DARK_GRAY);
		setFont(new Font("SansSerif", Font.BOLD, 14));
		setTitle("Printer");	
		getContentPane().setLayout(null);
		setSize(400, 400);
		setResizable(false);
		setLocationRelativeTo(null);
		setVisible(true);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

		
		
		
		/*/
		 * primary label to be printed
		 */
		JLabel lblNewLabel = new JLabel("Test Print Label");
		lblNewLabel.setFont(new Font("Arial Rounded MT Bold", Font.BOLD, 15));
		lblNewLabel.setForeground(Color.GRAY);
		lblNewLabel.setBounds(124, 11, 136, 14);
		getContentPane().add(lblNewLabel);
		
		/*/
		 * date
		 */
		JLabel lblNewLabel_1 = new JLabel("Date:");
		lblNewLabel_1.setForeground(Color.BLUE);
		lblNewLabel_1.setFont(new Font("Arial Rounded MT Bold", Font.BOLD, 11));
		lblNewLabel_1.setBounds(20, 48, 46, 14);
		getContentPane().add(lblNewLabel_1);
	
		JLabel lblNewLabel_2 = new JLabel("New label");
		lblNewLabel_2.setFont(new Font("Arial Black", Font.PLAIN, 12));
		lblNewLabel_2.setBounds(60, 48, 61, 14);
		getContentPane().add(lblNewLabel_2);
		
		Date now = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("MM/dd/yy"); 
		lblNewLabel_2.setText(sf.format(now));
        getContentPane().add(lblNewLabel_2);
     				
		/*/	
		 * 	group radio buttons
		 */
		JRadioButton rbutton1 = new JRadioButton("8 digits");
		rbutton1.setBackground(Color.ORANGE);
		rbutton1.setFont(new Font("Arial", Font.BOLD, 11));
		rbutton1.setBounds(279, 63, 109, 23);
		getContentPane().add(rbutton1);
		
		JRadioButton rbutton2 = new JRadioButton("13 digits");
		rbutton2.setBackground(Color.ORANGE);
		rbutton2.setFont(new Font("Arial", Font.BOLD, 11));
		rbutton2.setBounds(279, 82, 109, 23);
		getContentPane().add(rbutton2);
		
		ButtonGroup group = new ButtonGroup();
		group.add(rbutton1);
		group.add(rbutton2);
		rbutton1.setSelected(true);
		
		JLabel lblCodeLength = new JLabel("Code Length");
		lblCodeLength.setForeground(Color.BLUE);
		lblCodeLength.setFont(new Font("Arial Rounded MT Bold", Font.BOLD, 11));
		lblCodeLength.setBounds(279, 48, 91, 14);
		getContentPane().add(lblCodeLength);
		
		//rbutton1 = JTextFieldLimit(8);
		//rbutton2 = JTextFiledLimit(13);
		
		class RadioButtonHandler implements ActionListener {
			final int MAX_1 = 8;
		    final int MAX_2 = 13;
		    JRadioButton rbutton1;
						
			public void itemStateChanged(ItemEvent e) {
			
				 public MyRadioButtonClass() {
				 
				        rbutton1.setName("MyRadioButton1");
				        rbutton2.setName("MyRadioButton2");
				        pd.setDocumentFilter(new DocumentSizeFilter(MAX_1));

				        rbutton1.addActionListener(this);
				        rbutton2.addActionListener(this);
				
				        @Override
					public void actionPerformed(ActionEvent actionEvent) {
						AbstractButton aButton = (AbstractButton) actionEvent.getSource();
						textField.setText("");
				        
					    if(aButton.getName().equals("MyRadioBUtton1")) {
							pd.setDocumentFilter(new DocumentSizeFilter(MAX_1));
					    } else if(aButton.getName().equals("MyRadioButton2")) {
							pd.setDocumentFilter(new DocumentSizeFilter(MAX_2));
					    }
				    	}
				
				 
				
				
			}
		}
		//}
		/*/
		 * code digits input
		 */
		textField = new JTextField();
		textField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent arg0) {
				char c=arg0.getKeyChar();
				if(!(Character.isDigit(c) || (c==KeyEvent.VK_BACK_SPACE) || c==KeyEvent.VK_DELETE)){
					getToolkit().beep();
					arg0.consume();
									}
			}

		
				
			
			
		}
				
		);
		textField.setBounds(20, 101, 136, 20);
		getContentPane().add(textField);
		textField.setColumns(10);
		//textField.setDocument(new JTextFieldLimit(8)); //limit input to 8 digits
				
		JLabel lblCodeDigits = new JLabel("Product Code:");
		lblCodeDigits.setForeground(Color.BLUE);
		lblCodeDigits.setFont(new Font("Arial Rounded MT Bold", Font.BOLD, 11));
		lblCodeDigits.setBounds(20, 86, 115, 14);
		getContentPane().add(lblCodeDigits);
								
		
		/*/
		 * description input
		 */
		textField_1 = new JTextField();
		textField_1.setBounds(20, 159, 191, 20);
		getContentPane().add(textField_1);
		textField_1.setDocument(new JTextFieldLimit(20)); //limit input to 20 characters
		
		JLabel lblDescription = new JLabel("Description:");
		lblDescription.setForeground(Color.BLUE);
		lblDescription.setFont(new Font("Arial Rounded MT Bold", Font.BOLD, 11));
		lblDescription.setBounds(20, 143, 79, 14);
		getContentPane().add(lblDescription);
		
		/*/	
		 * 	number of copies input - double layer by default
		 */
		textField_2 = new JTextField();
		textField_2.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent arg0) {
				char c=arg0.getKeyChar();
				if(!(Character.isDigit(c) || (c==KeyEvent.VK_BACK_SPACE) || c==KeyEvent.VK_DELETE)){
					getToolkit().beep();
					arg0.consume();
				}
			}
		});
		textField_2.setBounds(20, 275, 46, 20);
		getContentPane().add(textField_2);
		textField_2.setColumns(10);
		textField_2.setDocument(new JTextFieldLimit(4)); //limit input digits
		
		/*/
		 * amount of price
		 */		
		JLabel lblRetailPrice = new JLabel("Retail Price:");
		lblRetailPrice.setForeground(Color.BLUE);
		lblRetailPrice.setFont(new Font("Arial Rounded MT Bold", Font.BOLD, 11));
		lblRetailPrice.setBounds(20, 205, 80, 14);
		getContentPane().add(lblRetailPrice);
				
		textField_3 = new JTextField();
		textField_3.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent arg0) {
				char c=arg0.getKeyChar();
				if(!(Character.isDigit(c) || (c==KeyEvent.VK_BACK_SPACE) || c==KeyEvent.VK_DELETE || c==KeyEvent.VK_PERIOD)){
					getToolkit().beep();
					arg0.consume();
				}
			}
		});
		textField_3.setBounds(20, 220, 86, 20);
		getContentPane().add(textField_3);
		textField_3.setColumns(10);
		textField_3.setDocument(new JTextFieldLimit(8)); //limit input digits
		
		
		JLabel lblNumberOfCopies = new JLabel("No. of Copies:");
		lblNumberOfCopies.setForeground(Color.BLUE);
		lblNumberOfCopies.setFont(new Font("Arial Rounded MT Bold", Font.BOLD, 11));
		lblNumberOfCopies.setBounds(20, 260, 115, 14);
		getContentPane().add(lblNumberOfCopies);
				
		/*/
		 * print button
		 */
		JButton btnPrint = new JButton("PRINT");		
		btnPrint.setBackground(Color.DARK_GRAY);
		btnPrint.setForeground(Color.CYAN);
		btnPrint.setFont(new Font("Orator Std", Font.BOLD, 16));
		btnPrint.setBounds(145, 326, 89, 23);
		getContentPane().add(btnPrint);
			
        btnPrint.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "pressed"); //binding enter key
        btnPrint.getInputMap().put(KeyStroke.getKeyStroke("released ENTER"), "released"); //binding enter key
						
		JFrame parent = new JFrame();			
		btnPrint.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	JOptionPane.showMessageDialog(parent, "Printing complete!");
            }
        });
		
		
			  }

	{
       
    }
	{
}
			
				
		
	{
}
	{
		{
			
		}
	}
}


http://postimg.org/image/af716al6f/

Is This A Good Question/Topic? 0
  • +

Replies To: Controlling the text field character limit using radio buttons?

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Controlling the text field character limit using radio buttons?

Posted 26 January 2016 - 09:21 AM

Quote

I've been having problem and error


http://technojeeves....java1/17-errors
Was This Post Helpful? 0
  • +
  • -

#3 daredavel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 26-January 16

Re: Controlling the text field character limit using radio buttons?

Posted 26 January 2016 - 10:10 AM

View Postg00se, on 26 January 2016 - 09:21 AM, said:

Quote

I've been having problem and error


http://technojeeves....java1/17-errors


Sorry about that. My bad.

The problem is my implementation of the RadioButtonHandler at line 169. It's my first time to use such and got lost. Any help that could resolve it is much appreciated.
Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 870
  • View blog
  • Posts: 6,695
  • Joined: 25-December 13

Re: Controlling the text field character limit using radio buttons?

Posted 26 January 2016 - 10:21 AM

Can you copy the full text of the error messages and paste it here?
Was This Post Helpful? 0
  • +
  • -

#5 daredavel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 26-January 16

Re: Controlling the text field character limit using radio buttons?

Posted 26 January 2016 - 10:35 AM

View PostNormR, on 26 January 2016 - 10:21 AM, said:

Can you copy the full text of the error messages and paste it here?


This is the description of the problem that I'm getting at line 169:

"The type RadioButtonHandler must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent)"

additionally, some of the swing components from line 239 to 318 went missing, probably I placed this radio button listeners incorrectly.
Was This Post Helpful? 0
  • +
  • -

#6 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 870
  • View blog
  • Posts: 6,695
  • Joined: 25-December 13

Re: Controlling the text field character limit using radio buttons?

Posted 26 January 2016 - 10:46 AM

If you are getting errors from the compiler, you need to copy the full text of the error message and paste it here.

The message you posted:

Quote

RadioButtonHandler must implement the inherited abstract method ActionListener.actionPerformed (ActionEvent)"

says the compiler can not find a definition for the actionPerformet() method in the RadioButtonHandler class. That class implements the ActionListener interface which requires that the actionPerformed method be defined in the class.


NOTE: The {}s are all messed up. You need to fix their positions so that each method's code ends with a } before the next method's definition starts.

This post has been edited by NormR: 26 January 2016 - 10:49 AM

Was This Post Helpful? 0
  • +
  • -

#7 daredavel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 26-January 16

Re: Controlling the text field character limit using radio buttons?

Posted 26 January 2016 - 10:13 PM

View PostNormR, on 26 January 2016 - 10:46 AM, said:

If you are getting errors from the compiler, you need to copy the full text of the error message and paste it here.

The message you posted:

Quote

RadioButtonHandler must implement the inherited abstract method ActionListener.actionPerformed (ActionEvent)"

says the compiler can not find a definition for the actionPerformet() method in the RadioButtonHandler class. That class implements the ActionListener interface which requires that the actionPerformed method be defined in the class.


NOTE: The {}s are all messed up. You need to fix their positions so that each method's code ends with a } before the next method's definition starts.


Thank you for the note, NormR! Sounds easy but I still make the to work. I tried implementing but to no avail. :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#8 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Controlling the text field character limit using radio buttons?

Posted 27 January 2016 - 03:14 AM

Your level of coding ability is relatively high. One of your problems is that you're writing too much code at once without compiling it. You should write it in small increments, compiling each time.

If you have compilation errors now, you should do it in reverse. Remove code until it compiles
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1