JComboBox cBox = new JComboBox();
cBox.addItem("Food"); String x = "Food";
cBox.addItem("Shirt");
cBox.addItem("Computer");
cBox.setSelectedItem(null);
cBox.setBounds(56, 127, 336, 27);
contentPane.add(cBox);
tPName = new JTextArea();
tPName.setBounds(38, 227, 130, 26);
contentPane.add(tPName);
tPName.setColumns(10);
tSName = new JTextArea();
tSName.setBounds(262, 227, 130, 26);
contentPane.add(tSName);
tSName.setColumns(10);
JButton btn = new JButton("Further Details");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = (String) cBox.getSelectedItem(); //gets value of combo box
tPName.setText(name); //text area displays value
String x = (String) tPName.getText();
name = "Food"; tSName.setText(F);
name = "Shirt"; tSName.setText(S);
name ="Computer"; tSName.setText(C);
}
});
btn.setBounds(162, 166, 117, 29);
contentPane.add(btn);
how would I make tSName display a text corresponding to a specific value of tPName which is copied from a combobox where F, S and C strings stand for those corresponding values I want to use.
I'm not sure if I understand you correctly. You try to make one textarea display something, when the text of another textarea changes, am I right? If yes, you should look for an Event of the first Textbox (something like tPNameKeyTyped) in this Event you can check the tPName.getText() simply with
if(tPName.getText().equals("whatever")){
tSName.setText("whatever u want");
}
Is that what you are looking for?
I right now dont have pc so I cant check errors but.
this should work
tPName.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tSName.setText(tPName.getText()); // put the value of tPName to tSName.
// i think this is all you want. i actually cant get your question.
// you can put a switch case to put coreesponding values in tSName. like:
switch(tPName.getText()){
case "a" : tSName.setText("1");
break;
//..... so on
default:
tSName.setText("stack overflow is great");
break;
}
}
});
EDIT:- Onece you do this try to refresh and validate the textareas.
tPName.validate();
tSName.validate();
tPName.repaint(); // refresh still not sure as i dont have pc if this not works try <your jframe>.repaint(); this should work
Related
Ok, so I am stuck with these loops. I have a tabbedPane and I add tabs to it, via data from database which I get with the line con.getKat() - (list of strings that represent categories). Then every tab should contain a JTable with contents from my database with all the records that contain the category(String) from the selected tab. The problem is that every tab triggers the ChangeListener() (which i check with System.out.println(kategorija)) but only the last tab displays data (no matter how i add or remove tabs it's always the last tab). Might be something silly here but any help appreciated.
ArrayList<String> ls = con.getKat();
for(String s : ls){
pane = new JScrollPane();
tabbedPane.addTab(s, null, pane, null);
}
tabbedPane.addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent e) {
kategorija = tabbedPane.getTitleAt(tabbedPane.getSelectedIndex());
System.out.println(kategorija);
ArrayList<Ceni> artikli = con.getHuroCeni(kategorija);
dtm.setRowCount(0);
for(Ceni c : artikli){
dtm.addRow(new Object[]{c.sifra, c.name, String.valueOf(c.cena1), c.tarifa});
table = new JTable();
table.setFont(new Font("Times New Roman", Font.PLAIN, 16));
table.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(128, 128, 128), null, null, null));
pane.setViewportView(table);
table.setModel(dtm);
}
}
});
I want to add two numbers and put the result into a JTextFields (textboxes). Why doesn't this code work?
public class Window extends JFrame implements ActionListener {
private JButton plus;
private JLabel text;
private JTextField textbox1;
private JTextField textbox2;
public Okno(){
this.setLayout(new FlowLayout());
this.setBounds(400,400,400,400);
plus = new JButton("+");
text = new JLabel("");
plus.addActionListener(this);
textbox1 = new JTextField(" ");
textbox2 = new JTextField(" ");
this.add(text);
this.add(textbox1);
this.add(textbox2);
this.add(plus);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(plus)){
int result = Integer.valueOf(textbox1.getText()) + Integer.valueOf(textbox2.getText());
text.setText(Integer.toString(result)); //gtregergregergergreg
}
}
}
Thank you for your help.
It works, if you remove spaces while putting numbers to the text fields, otherwise you get NumberFormatException. By the way don't use spaces for aligning the text fields. You can use setColumns or different layout manager. Also you should validate input to be sure there are just numbers, if you want to add them together.
Delete spaces from:
textbox1 = new JTextField(" ");
textbox2 = new JTextField(" ");
cause while parsing to Integer it fails.
Set prefered size of textbox1 and textbox2:
textbox1 = new JTextField();
textbox1.setPreferredSize(new Dimension(20,20));
textbox2 = new JTextField();
textbox2.setPreferredSize(new Dimension(20,20));
Hope I helped :)
My need is to display a tab in a JDialog (confirmDialog or inputDialog). The tab contains 2 JTextField per row. The display works fine :
but I don't know how to get the values of the JTextFields.
Here is the display code :
int size = model.getCheckedApplications().size();
// une ligne par application sélectionnée
layout = new GridLayout(size + 1, 3, 5, 5);
myPanel = new JPanel(layout);
myPanel.add(new JLabel("Application"));
myPanel.add(new JLabel("Version cadre"));
myPanel.add(new JLabel("Nouvelles natures"));
for (Application app : model.getCheckedApplications()) {
myPanel.add(new JLabel(app.getCode88()));
JTextField versionActuelleField = new JTextField(30);
versionActuelleField.setName("versionActuelle"
+ app.getCode88());
versionActuelleField.setText(app
.getVersionCadreActuelle());
JTextField nouvellesNaturesField = new JTextField(
30);
nouvellesNaturesField.setName("nouvellesNatures"
+ app.getCode88());
myPanel.add(versionActuelleField);
myPanel.add(nouvellesNaturesField);
}
result = JOptionPane.showConfirmDialog(null, myPanel,
"Valeurs de cette version",
JOptionPane.OK_CANCEL_OPTION);
Then I don't know how to get the values when the user clicks on the OK Button :
if (result == 0) { // The user clicks on the ok button
You need to add them to some list that you store, so you can get at them again. Since you are adding them in reference to an application, I would suggest a Map
private Map<Application, JTextField> nouvellesNaturesFields = new ArrayListMultimap<Application, JTextField>(); //Or Hashmap, if the key is unique
private Map<Application, JTextField> versionActuelleFields = new ArrayListMultiMap<Application, JTextField>();
public List<JTextField> getNouvellesNaturesFields() {
return nouvellesNaturesFields ;
}
public List<JTextField> getVersionActuelleFields () {
return versionActuelleFields ;
}
//class code
for (Application app : model.getCheckedApplications()) {
//Other code
JTextField nouvellesNaturesField = new JTextField(
30);
nouvellesNaturesField.setName("nouvellesNatures"
+ app.getCode88());
nouvellesNaturesFields.put(app, nouvellesNaturesField);
//Other code and same for your new nature fields
}
result = JOptionPane.showConfirmDialog(null, myPanel,
"Valeurs de cette version",
JOptionPane.OK_CANCEL_OPTION);
Then when the user clicks the confirm button, using the property accessor getNouvellesNaturesFields()or getVersionActuelleFields() you can iterate all the fields created, like so:
for (Map.Entry<Application, JTextField> entry: myMap.entries()) {
//Do something here
}
Or you could also get them via:
for (Application app : model.getCheckedApplications()) {
List<JTextField> data = myMap.get(app);
for(JTextField field : data) {
field.getText();
}
}
Since the key value probably won't be unique, I used an ArrayListMultiMap, but if it would be unique, then a HashMap should suffice
You assign the Jtextfield value to a string using the getText() method e.g below
String texfield = JTextField.getText();
Subsequently you use the String textfield wherever you want. And to get the right jtextfield you have to get text from the textfield you want for example you have four Jtexfield. Assuming they are JTextField1, JTextField2, JTextField3 and JTextField4. To get the value of JTextField3 you have
String texfield = JTextField3.getText();
The values should be in the JTextFields you created:
versionActuelleField
nouvellesNaturesField
Also, you might want to look at ParamDialog, which I implemented to be a generic solution to this question.
EDIT
Yes I see now that you are creating these JTextFields in a loop. So you need to create a Collection, I'd suggest a Map<String, JTextField> where you could map all of your application names to the matching JTextField, as well as iterate over the collection to get all application names / JTextFields.
I am currently constructing a GUI which allows me to add new cruises to the system. I want to write an actionListner which once the user clicks on "ok" then the form input will be displayed as output on the console.
I currently have the following draft to complete this task:
ok = new JButton("Add Cruise");
ok.setToolTipText("To add the Cruise to the system");
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event){
int selected = typeList2.getSelectedIndex();
String tText = typeList2[selected];
Boolean addtheCruise = false;
addtheCruise = fleet.addCruise();
fleet.printCruise();
if (addtheCruise)
{
frame.setVisible(false);
}
else
{ // Report error, and allow form to be re-used
JOptionPane.showMessageDialog(frame,
"That Cruise already exists!", "Error",
JOptionPane.ERROR_MESSAGE);
}
}
});
buttonPanel.add(ok);
Here is the rest of the code to the Form input frame:
contentPane2 = new JPanel (new GridLayout(18, 1)); //row, col
nameInput = new JLabel ("Input Cruise Name:");
nameInput.setForeground(normalText);
contentPane2.add(nameInput);
Frame2.add(contentPane2);
Cruisename = new JTextField("",10);
contentPane2.add(Cruisename);
Frame2.add(contentPane2, BorderLayout.CENTER);
Frame2.setVisible(true);
confirmPanel = new JPanel ();
confirmPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
confirmPanel.setBorder(BorderFactory.createLineBorder(Color.PINK));
addItinerary = new JButton("Add Itinerary");
confirmPanel.add(Add);
confirmPanel.add(addItinerary );
Frame2.add(confirmPanel, BorderLayout.PAGE_END);
Frame2.setVisible(true);
contentPane2.add(Cruisename);
Frame2.add(contentPane2);
// add spacing between comboboxes
contentPane2.add(Box.createRigidArea(new Dimension(5,0)));
contentPane2.setBorder(BorderFactory.createLineBorder(Color.black));
//Label for start and end location jcombobox
CruiseMessage = new JLabel ("Enter Start and End Location of new Cruise:");
CruiseMessage.setForeground(normalText);
contentPane2.add(CruiseMessage);
Frame2.add(contentPane2);
/**
* creating start location JComboBox
*/
startL = new JComboBox();
final JComboBox typeList2;
final String[] typeStrings2 = {
"Select Start Location", "Tobermory","Oban", "Isle of Mull", "Isle of Harris",
"Lewis and Harris", "Stornoway", "Skye", "Portree"};
startL = new JComboBox(typeStrings2);
contentPane2.add(startL);
Frame2.add(contentPane2, BorderLayout.CENTER);
Frame2.setVisible(true);
// add spacing between comboboxes
contentPane2.add(Box.createRigidArea(new Dimension(5,0)));
/**
* creating end location JComboBox
*/
endL = new JComboBox();
final JComboBox typeList3;
final String[] typeStrings3 = {
"Select End Location", "Tobermory","Oban", "Isle of Mull", "Isle of Harris",
"Lewis and Harris", "Stornoway", "Skye", "Portree"};
endL = new JComboBox(typeStrings3);
contentPane2.add(endL);
Frame2.add(contentPane2, BorderLayout.CENTER);
Frame2.setVisible(true);
// add spacing between comboboxes
contentPane2.add(Box.createRigidArea(new Dimension(5,0)));
//Label for select ship jcombobox
selectShipM = new JLabel ("Select Ship to assign Cruise:");
selectShipM.setForeground(normalText);
contentPane2.add(selectShipM);
Frame2.add(contentPane2);
/**
* creating select ship JCombobox
* select ship to assign cruise
*/
selectShip = new JComboBox();
final JComboBox typeList4;
final String[] typeStrings4 = {
"Select Ship", "Dalton Princess", "Stafford Princess" };
selectShip = new JComboBox(typeStrings4);
contentPane2.add(selectShip);
Frame2.add(contentPane2, BorderLayout.CENTER);
Frame2.setVisible(true);
I need all form inputs from the code above to be displayed on the console upon completion.
Summary:
1. I have two ships in one fleet
2. To add new cruise, all fields (Name, start date, end date, ship) must be selected.
The Problem:
1. I keep coming up with errors when creating " fleet = new Fleet();" in my constructor. Even though I have declared it in my class.
2. In the draft code below, line 5 states "typeList2", however, I have two JComboBox's - two different type Strings for both drop down menu's (Shown in the rest of the code). How do I input both typeLists to the output rather than just one?
Thank you.
Can you help me with this?
Here is the code first:
public RegistrationForm(){
super("Registration Form (Assignment One)");
setLayout(new GridLayout(6,2));
l[0] = new JLabel("Name: ");
l[0].setFont(new Font("Calibri Head",Font.BOLD,12));
add(l[0]);
tf[0] = new JTextField();
tf[0].setToolTipText("Enter Your Full Name");
add(tf[0]);
l[1] = new JLabel("Age: ");
l[1].setFont(new Font("Calibri Head",Font.BOLD,12));
add(l[1]);
tf[1] = new JTextField();
tf[1].setToolTipText("Enter Your Age");
add(tf[1]);
l[2] = new JLabel("Birthday: ");
l[2].setFont(new Font("Calibri Head",Font.BOLD,12));
add(l[2]);
tf[2] = new JTextField();
tf[2].setToolTipText("Enter Your Birthday");
add(tf[2]);
l[3] = new JLabel("Address: ");
l[3].setFont(new Font("Calibri Head",Font.BOLD,12));
add(l[3]);
tf[3] = new JTextField();
tf[3].setToolTipText("Enter Your Address");
add(tf[3]);
l[4] = new JLabel("Contact Number: ");
l[4].setFont(new Font("Calibri Head",Font.BOLD,12));
add(l[4]);
tf[4] = new JTextField();
tf[4].setToolTipText("Enter Your Contact Number");
add(tf[4]);
b[0] = new JButton("Submit");
b[0].addActionListener(this);
add(b[0]);
b[1] = new JButton("Clear");
b[1].addActionListener(this);
add(b[1]);
}
So When I input a value to all and press "Submit" the previous class will close and another class will open and there it will show the value of the things I inputted from the previous class. . .
There is no default value to JTextfields, I'm going to enter the value myself.
How can i throw(I mean pass) a value to the other class?
Here is the code i have so far:
This is my method:
public String name(){
return tf[0].getText();
}
This is from my Other class:
public Form{
RegistrationForm form = new RegistrationForm();
JTextField name = form.name();
add(name);
}
You don't need to throw anything. Whatever class that displays this dialog will hold a reference to the instance of this class and can simply query the state of the fields once the dialog returns. This is much easier if the dialog window is a modal dialog such as a modal JDialog or a JOptionPane.
For instance, please look at my code in this example.
Edit
Also, this confuses me:
public Form{
RegistrationForm form = new RegistrationForm();
JTextField name = form.name();
add(name);
}
Does this code display the RegistrationForm object? Is RegistrationForm in fact a modal JDialog? It is very unusual to extract a JTextField from one GUI and add it to another, and I'm pretty sure that you don't want to do this. Again, what you want to do is:
Display your RegistrationForm as a modal JDialog.
After it returns, call getter methods on the RegistrationForm object that extracts the Strings held by the text fields of the object.
For more details, you'll still need to tell us a lot more about your code and your problem.