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.
Related
I am trying to implement a recurrent addition calculation. This should get the current quantity (from the Quantity textfield) and the saved quantity (total quantity to be saved), outputting the total result below the 'Results' heading. View of tabbed page for calculation.
I have tried the following code, but it contains errors and uses a Dialog to output the result (which is not what I want).
addItem.addComponent(selectItem);
TextField quantity = new TextField("", "Quantity (ml or g)", 4, TextArea.NUMERIC);
addItem.addComponent(quantity);
Button add = new Button("Add");
addItem.addComponent(add);
TextArea results = new TextArea("Results");
addItem.addComponent(results);
//TextArea total = new TextArea("Add Item");
//addItem.addComponent(total);
//--------------------------------------------------------------
add.addActionListener((ActionEvent ev) -> {
Storage s = Storage.getInstance();
Integer addition = 0;
// Read my "Hello World" string back from storage
addition = (Integer)s.readObject("total");
int d = Int.parseInt(quantity.getText());
Integer total = addition + quantity;
// Save the "Hello World" string to storage
s.writeObject("total", total);
Dialog.show("", total, "OK", "Cancel");
});
//--------------------------------------------------------------
return addItem;
I would therefore appreciate any advice and guidance on how this could be implemented in my code.
To present it in a table , you can use easily GridLayout in this way :
Container ctrResultTable = new Container(new GridLayout(1,3));
//Define a new container associated to a gridlayout with 1 row and 1 column
ctrResult.addComponent(new Label(d.toString()));
ctrResult.addComponent(new Label(total.toString()));
ctrResult.addComponent(new Label((total/target*100).toString()));
//Because you use a value inside a function, addItem should be initialized as final
addItem.addComponent(ctrResult);
But i can't do nothing for your code error because i can't see the full code here
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
I have to write a Java code where I should get input from 2 different combo boxes. The input, that I will get, has to be display in a text field. I have written a part of my code but I can't get the input.
This is what I have written so far:
package main;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ComboI extends JFrame implements ItemListener{
JComboBox dita = new JComboBox();
JComboBox ora = new JComboBox();
JLabel dita1 = new JLabel("Zgjidhni diten:");
JLabel ora1 = new JLabel("Zgjidhni oren");
JTextArea pergjigje = new JTextArea(2, 10);
public ComboI(){
super("Orari mesimor IE102");
setSize(600, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
Container content = getContentPane();
FlowLayout lay = new FlowLayout(FlowLayout.LEFT);
content.setLayout(lay);
content.add(dita);
content.add(dita1);
content.add(ora1);
content.add(ora);
content.add(pergjigje);
setContentPane(content);
dita.addItem("E Hene");
dita.addItem("E Marte");
dita.addItem("E Merkure");
dita.addItem("E Enjte");
dita.addItem("E Premte");
dita.addItemListener(this);
ora.addItem("08:30 - 09:25");
ora.addItem("09:30 - 10:25");
ora.addItem("10:30 - 11:25");
ora.addItem("11:30 - 12:25");
ora.addItem("12:30 - 13:25");
ora.addItem("13:30 - 14:25");
ora.addItemListener(this);
}
public void itemStateChanged(ItemEvent event){
String choice1 = event.getItem().toString();
String choice2 = event.getItem().toString();
if (choice1.equals("E Marte") && choice2.equals("E Marte")){
String a = "hi";
pergjigje.setText(a);
}
}
}
String choice1 = event.getItem().toString();
String choice2 = event.getItem().toString();
You can only generate an event for one combo box at a time, so if you want the values from the combo boxes you need to access the combo box, not the event.
The code would be something like:
String choice1 = dita.getSelectedItem().toString();
String choice2 = ora.getSelectedItem().toString();
You can use the getSelectedItem() method, with a cast to String. Or, you can use getItemAt with getSelectedIndex and a generic JComboBox<String> for your fields - this has the advantage of compile time type safety, and not needing a cast.
String sd = (String)dita.getSelectedItem();
String so = (String)ora.getSelectedItem();
or
String sd = dita.getItemAt(dita.getSelectedIndex());
String so = ora.getItemAt(ora.getSelectedIndex());
The other advantage of the second approach is that you can instead use getSelectedIndex to get a day of the week, or a time slot, from an array without having to parse text. For example:
// using java 8 for the java.time.DayOfWeek enum
DayOfWeek day = DayOfWeek.of(dita.getSelectedIndex());
// simply storing the hour of the time selection
int hour = ora.getSelectedIndex();
// using joda time for time without dates
// with org.joda.time.LocalTime
LocalTime time = new LocalTime(ora.getSelectedIndex() + 7, 30);
Detecting when the user hasn't made a selection
You should also be aware that, for your code, even if the user has not yet selected an item it will return the first item in the combobox. You may therefore want to add a "choose an item ..." string before any of the other entries. For example:
dita.addItem("Select a day ...");
dita.addItem("E Hene");
...
// inside the listener
if (dita.getSelectedIndex() == 1) { // no choice made yet }
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.
I'm trying to work to display a number of jtextfield according to one of the given values in a combobox.
So, I will have a drop down menu with let's say 1 to 4. If the user selects number 3, 3 textfields will be displayed. I've created the jcombobox with a selection of numbers. But I'm not sure how to implement this. If I'm not mistaken I need to use
ItemEvent.SELECTED
I think I need to create a reference to the JTextField object that will be available to the JComboBox's itemListener object.
Any help would be greatly appreciated.
I've added this to my class :
// aOption is the combobox I declared
aOptionComboBox.setModel(new DefaultComboBoxModel(new String[]{"1","2","3"}));
public void itemStateChanged(ItemEvent event) {
String num = (String)aOptionComboBox.getSelectedItem();
int num1 = Integer.parseInt(num);
JTextField[] textfields = new JTextField[num1];
for (int i = 0; i < num1; i++)
{
textfields[i] = new JTextField("Field");
getContentPane().add(textfields[i]);
textfields[i].setBounds(200, 90, 100, 25);
}
}
am I on a right track?
use the getSelectedItem() on the combobox. This will either yield a string or an integer (depending on how you implemented it). Next use a for-loop to determine the amount of JTextField's and store them in an array.
int amount = myJComboBox.getSelectedItem();
JTextField[] textfields = new JTextField[amount];
for (int i = 0; i < amount; i++) {
textfields[i] = new JTextField("awesome");
this.add(textfields[i]);
}
this way you can easily store the textfields and add them to your panel.
Some added information.
The textfield-array must be accesible outside the eventListener, so you must implement it in your class. that way the whole class can use it.