What is this parameter used in making a window in java? - java

The following is some code which makes multiple fields (items). I know that the parameter after new JTextField is the content of the text box, and I understand how item2, 3, and passwordField work, but do not understand item1. In the line of code:
item1 = new JTextField(10);
What does the (10) mean? I would expect there to be a default number inside that text box, but that is not the case. Any help would be appreciated.
public eventhandling(){
super("The title");
setLayout(new FlowLayout());
item1 = new JTextField(10);
add(item1); //Adds item to window
item2 = new JTextField ("Enter text here"); //Making a text box that has the words "enter text here" in it
add(item2);
item3 = new JTextField ("uneditable", 20);
item3.setEditable(false); //This text field cannot be changed now
add(item3);
passwordField = new JPasswordField ("mypass"); //Setting the password field with a default password : "mypass"
add(passwordField);

This is what i found from the Oracle Site
JTextField(int columns):
Constructs a new empty TextField with the specified number of columns.

First you need to understand that there are different ways to initialize a component. Then you also need to notice that there is a difference between "10" and 10. The first is a string, the second is an integer. That being said, if you want the number 10 to show in the JTextField box then you need to pass "10" in the constructor. If you pass 10 that is telling the constructor to set the 10 columns in the JTextField, that is, it restricts the number of characters that can be entered to 10. Check the API ...
http://docs.oracle.com/javase/7/docs/api/javax/swing/JTextField.html

Related

Limit number of character on every JTextArea line

I have a JTextArea in a JDialog. JDialog uses a GridLayout.
I would like to have 7 digits on each line of the JTextArea (each line will be a 7 int registration number). For user experience reasons, I would like the JTextArea to either add a new line or stop expanding when it reaches 7 characters on a line.
Things that didn't work :
- Specifying the number of columns in the JTextArea constructor
- matriculesText.setLineWrap(true); and matriculesText.setWrapStyleWord(true);
I'm afraid uploadDialogManager.setHgap(20); might be breaking the code. I'm wondering if the JDialog should rather have a fixed size.
This is how I construct my JDialog :
// initialization
Dialog uploadParent = null;
JDialog uploadDialog = new JDialog(uploadParent);
GridLayout uploadDialogManager = new GridLayout(UPLOAD_DIALOG_ROWS,
UPLOAD_DIALOG_COLUMNS);
// uploadDialog properties
uploadDialog.setSize(new Dimension(UPLOAD_DIALOG_WIDTH, UPLOAD_DIALOG_HEIGHT));
uploadDialog.setLayout(uploadDialogManager);
uploadDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
//Set up the horizontal gap value
uploadDialogManager.setHgap(20);
//Set up the vertical gap value
uploadDialogManager.setVgap(20);
//Set up the layout of the buttons
//uploadDialogManager.layoutContainer();
// components initialization
JLabel exerciceLabel = new JLabel("exercice number : ");
JComboBox<Integer> exerciceNumbers = new JComboBox<Integer>
(EXERCICE_NUMBERS);
JLabel matriculeLabel = new JLabel("please enter your matricules, one per
line : ");
JTextArea matriculesText = new JTextArea(1, 1);
JButton confirm = new JButton("confirm");
JButton cancel = new JButton("cancel");
matriculesText.setLineWrap(true);
matriculesText.setWrapStyleWord(true);
I have made another solution that fills my requirements. Instead of going to a new line when 7 characters are reached, I use MATRICULE_REGEX to check if every line contains 7 digits (1\\d{6}). If it doesn't, I reject the JTextArea content.
private static final String MATRICULE_REGEX = "1\\d{6}(\\n1\\d{6})*";
Pattern matriculePattern = Pattern.compile(MATRICULE_REGEX);
Matcher matriculeMatcher = null;
matriculeMatcher = matriculePattern.matcher(text);
isValidMatricule = matriculeMatcher.matches();

Java, using input from JOptionPane for a double variable?

Thanks in advance for any help. So I've used input from JOptionPane many times before but this is confusing me. I only want to use the input from the paymentField which takes in ($0.00) a value from the user. Do I use an action listener for this and if so how or is there another way?
At the minute Im using showInputDialog but it brings up an extra unwanted text field. Also tried showConfirmDialog but couldnt seem to use what was entered.
//method used to display dialog box required add funds to wallet
public void getWalletBox() {
String[] cTypes = {"Maestro", "Visa Debit", "Mastercard", "American Express"};
JComboBox cardType = new JComboBox(cTypes);
cardType.setFont(new Font("Serifs", Font.BOLD, 16));
JTextField cardNumField = new JTextField(20); //width of text fields = 25
cardNumField.setFont(new Font("Serifs", Font.BOLD, 16));
//formats payment field using dollar prefix
NumberFormat paymentFormat = NumberFormat.getCurrencyInstance(Locale.US);
paymentField = new JFormattedTextField(paymentFormat);
paymentField.setValue(new Double(0.00));
paymentField.setFont(new Font("Serifs", Font.BOLD, 25));
paymentField.setEditable(true);
paymentField.setForeground(Color.green);
//array of objects used to add labels and fields to joptionpane
Object[] fields = {
"Card Type: ", cardType,
"Card Number: ", cardNumField,
"Add: ", paymentField
};
JOptionPane.showInputDialog(null, fields, "Wallet", JOptionPane.OK_CANCEL_OPTION);
}`
You have to define your own JDialog for that as that is much simple as compared to cutomize it using this method.

java double input panel

I have the following code:
String firstName;
firstName = JOptionPane.showInputDialog("First Name");
String familyName;
familyName = JOptionPane.showInputDialog("Family Name");
With this code you will have 2 boxes popping up to fill in your name. However, I would like to know if there is a way of putting those two input boxes both on one popup.
Just create a panel containing two text fields and then just add the panel to the dialog :
JPanel p = new JPanel();
JTextField familyName = new JTextField(10);
JTextField firstName = new JTextField(10);
p.add(new JLabel("Family name :"));
p.add(familyName);
p.add(new JLabel("First name : "));
p.add(firstName);
JOptionPane.showConfirmDialog(null, p, "Family and first name : ", JOptionPane.OK_CANCEL_OPTION);
Here's what it looks like :
Add both of these JOptionPanel boxes to a single JFrame.
This way both of the input boxes will be on the same window.
JFrame frame = new JFrame.add(JOptionPanel);
No, You can't do this using JOptionPane library. you can write your own class using java.swing package that provide multiple input on single popup.

Set the font of texts retrieved from JTextField

I have the user enter some words through a JTextField. I want to set the font of the string. Here is what I have so far.
Font f;
f = new Font(input.getText(), Font.ITALIC, 32);
word = new JLabel(f, SwingConstants.CENTER);
Unfortunately, Java is throwing me a compiler error because JLabel doesn't accept Font as a parameter. Is it possible to set the font of a string retrieved from a text field and have it displayed on a JFrame?
See JComponent.setFont(Font).
Sets the font for this component.
But better than a text field to set a font name, see this answer for a (styled) combo or this answer that uses a list:

Dynamic text box in Java

how do I make a text box which the user can insert text into, then that text can be saved to some variable?
JTextField is probably the class you are looking for.
JTextField textField = new JTextField();
yourPanel.add(textField);
This will add the textField into your JPanel. Then at any point in your code where you have a handle to your textField, call getText(); of your JTextField.
String s = textField.getText();
See this tutorial for a better reference:
http://download.oracle.com/javase/tutorial/uiswing/index.html
A JTextField or JTextArea will do what you are asking for, but you'll need either a button or a listener to actually know when to save this to a String.
javax.swing is Event based, which means that you cannot extract the text like this:
JTextField myField = new JTextField();
//wait for user input
String s = myField.getText(); //not guaranteed to work!
Instead, you may want to make a "Submit" button that will send the text to your program when it is clicked:
http://download.oracle.com/javase/tutorial/uiswing/components/button.html

Categories