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/

New Topic/Question
Reply


MultiQuote




|