Java Swing: JButton creates new JTextField(s) - java

Hello :) I am beginner in Java Swing and I can't google solution for my problem. I have a JPanel and want to add JTextField(s) dynamically after pressing a JButton. And how can I getText() from them later? My code, commented part isn't working properly.
Variable 'counter' counts how many fields I have in panel.
public class AppPanel extends JPanel {
private JTextField tfData[];
private JButton btAdd;
private int counter = 1;
public AppPanel() {
setLayout(null);
//tfData[counter] = new JTextField();
//tfData[counter-1].setBounds(20, 20, 250, 20);
//add(tfData[counter-1]);
btAdd = new JButton("Add field");
btAdd.setBounds(280, 20, 120, 20);
btAdd.addActionListener(new alAdd());
add(btAdd);
}
class alAdd implements ActionListener {
public void actionPerformed(ActionEvent e) {
//tfData[counter] = new JTextField();
//tfData[counter].setBounds(20, 20+20*counter, 250, 20);
//add(tfData[counter]);
++counter;
}
}
}

As you're already storing references to your text fields, just use this array to query the text of the text fields:
tfData[counter-1].getText();
will show you the text of the last added text field.
But you really should initialise your array before, otherwise you won't be able to add any items to it. I think that was your main problem as you commented out your adding-code.
// think about how many text fields you will need (here: 16)
private JTextField tfData[] = new tfData[16];
If you're using arrays, watch for not breaking over its bounds. But better use a list as proposed in the comments before as it grows dynamically and you won't have to deal with array bounds and can even skip counting (the list does that for you, too).

Related

How to call `setText` on multiple JTextfields?

I want to figure out a way to set the text for multiple JTextfields in just a few lines (preferable one line) of code rather than a new line for every textfield.
I am looking for a way to invoke the setText method on multiple JTextfield values. My code works correctly, but I do not want to write out someField.setText("0.00"); for each JTextfield.
Here is code with the repeated calls that I want to shorten:
JButton btnNewButton_1 = new JButton("Clear");
btnNewButton_1.setBounds(367, 533, 86, 32);
btnNewButton_1.setFont(new Font("Tahoma", Font.PLAIN, 11));
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textIncome.setText("0.00");
textGrossSalary.setText("0.00");
textGrossSalary.setText("0.00");
textHousehold.setText("0.00");
textFood.setText("0.00");
textChildren.setText("0.00");
textBills.setText("0.00");
textIncidentals.setText("0.00");
textCredit.setText("0.00");
textHome.setText("0.00");
textInvestings.setText("0.00");
textPets.setText("0.00");
textTransport.setText("0.00");
textLifestyle.setText("0.00");
textTax.setText("0.00");
textDisposable.setText("0.00");
textHealthFitness.setText("0.00");
textGiftsDonations.setText("0.00");
}
});
Put them in a List or an array and iterate over them
For example, create an instance field array which contains the fields which you want to set (to the same value)
private JTextField fields[] = new JTextField[]{
textIncome,
textGrossSalary,
textGrossSalary,
textHousehold,
textFood,
textChildren,
textBills,
textIncidentals,
textCredit,
textHome,
textInvestings,
textPets,
textTransport,
textLifestyle,
textTax,
textDisposable,
textHealthFitness,
textGiftsDonations};
Then when the ActionListener is triggered, iterate over it...
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (JTextField field : fields) {
field.setText("0.00");
}
}
});
Based on btnNewButton_1.setBounds(367, 533, 86, 32);, I'd advise avoiding using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify

jbuttons and a jtextfield that has an action listener

I have a keypad made up of jbuttons and a jtextfield that has an action listener. When I press the button the number shows in the textfield but the next number overwrites it. Could anyone tell me how to append the text to the length 13 numbers and when it get's there to carriage return.
If I use the keyboard to enter numbers I can enter a String of numbers but not from the buttons.
I am using:
JButton buttonNo2 = new JButton("2");
buttonNo2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textfield.setText("2");
}
buttonNo1.setBounds(11, 18, 50, 50);
keyboardPanel.add(buttonNo2);
buttonNo1.setForeground(Color.BLUE);
buttonNo1.setFont(new Font("Perpetua", Font.BOLD, 20));
Try using something like
textfield.setText(textfield.getText() + "2");
instead of textfield.setText("2");
setText does just that, sets the text of the text field to the value you specified.
Also buttonNo1.setBounds(11, 18, 50, 50); looks like you're trying to do without a layout manager. Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify
You could also make it much simpler for yourself, and use the Action API instead, which would save you a not lot of repeated typing...
public class NumberAction extends AbstractAction {
private JTextField field;
private int number;
public NumberAction(JTextField field, int number) {
this.field = field;
this.number = number;
putValue(NAME, Integer.toString(number));
}
#Override
public void actionPerformed(ActionEvent e) {
Document doc = field.getDocument();
try {
doc.insertString(doc.getLength(), Integer.toString(number), null);
} catch (BadLocationException ex) {
ex.printStackTrace();
}
}
}
Then you would just need to add each button as required...
add(new JButton(new NumberAction(textfield, 1)));
add(new JButton(new NumberAction(textfield, 2)));
add(new JButton(new NumberAction(textfield, 3)));
See How to Use Actions for more details
You have to get the text first and set the text by appending the previous and current text.
public void actionPerformed(ActionEvent e) {
String str = textfield.getText();
textfield.setText(str+the number printed);
}
As #MadProgrammer already posted How to achieve it with JTextField ,
You can opt for JTextArea#append method
public void append(String str)
Appends the given text to the end of the document. Does nothing if the
model is null or the string is null or empty.
Parameters:
str - the text to insert

JButtons in GUI

I'm a beginner in GUI.
Is there a quick way of setting the same JButton/Image to multiple locations within the GUI? For better clarification, if I want to use this JButton 10 times at different locations in my GUI, would I have to create a new JButton(new ImageIcon...) 10 times?
The buttons don't have to lead to anything, this is just for show.
JButton jb = new JButton(new ImageIcon("myImage.png"));
jb.setLocation(10,10);
jb.setSize(40, 40);
getContentPane().add(jb);
The short answer is, yes, you will need multiple instances of JButton.
You can use an Action which can be applied to multiple instance of a button (the same instance of Action). The Action class carries properties which will be used to configure the buttons, such as text and icon properties.
A component (like JButton) can only reside within in a single container, therefore, you will need multiple instances of JButton.
Take a look at How to Use Actions and How to Use Buttons, Check Boxes, and Radio Buttons for more details...
Generally, you should avoid using setLocation and setSize and rely more on the use of layout managers, but you've not provided enough context to say if this useful to you or not.
Yes, you need to create a Jbutton object for each desired instance.
Since you have so many JButton that are all similar, I suggest that you declare an array JButton[] buttons = new JButton[10]; and use a for loop to create each individual button and set their attributes.
If it is just for a show, I would do the following to show the 10 button in a row:
int buttonHeight = 10;
int buttonWidth = 10;
for (int i = 0; i < 10; i++) {
JButton button = new Button("Button " + i);
button.setSize(buttonWidth, buttonHeight);
button.setLocation(10 + i * buttonWidth, 10);
getContentPane().add(button);
}
import java.util.Scanner;
import javax.swing.*;
import java.awt.*;
class PROB4_CHAL1 extends JFrame
{
JButton b[]=new JButton[10];
public PROB4_CHAL1()
{
setLayout(null);
setVisible(true);
setSize(100,100);
for(int i=0;i<10;i++)
{
b[i]=new JButton(""+i);// or b[i]=new JButton(new ImageIcon("path"));
b[i].setBounds(i*10,i*20,50,20);
add(b[i]);
}
}
public static void main(String[] args)
{
new PROB4_CHAL1();
}
}
You can Create array of 'JButton [10]' .

Display an ArrayList<Object> in JTextArea of another class

I have spent all day on the Web and on this site looking for an answer to my problem, and hope you guys can help. First of all, I am trying to display the contents of an ArrayList to a JTextArea when I select the 'report' JButton. The array list is in another class separate from the text area. My problem stems from the fact that the array list is an array of objects, so that when I try to display it I get the error:
The method append(String) in the type JTextArea is not applicable
for the arguments (ArrayList.Account.TransactionObject>)
I can display the array list just fine in the console window but am stumped when it comes to displaying it in the text area. I'm under the assumption that there must be some kind of issue converting the Object to a String, because I have been unable to cast it to a String or call a toString method with the array list. Here is the relevant parts of my code.....
This is the portion in the AccountUI class where I created the JTextArea:
private JPanel get_ReportPane()
{
JPanel JP_reportPane = new JPanel(new BorderLayout());
Border blackline = BorderFactory.createLineBorder(Color.BLACK);
TitledBorder title = BorderFactory.createTitledBorder(blackline, "Transaction Report");
title.setTitleJustification(TitledBorder.CENTER);
JP_reportPane.setBorder(title);
/* Create 'labels' grid and JLabels */
JPanel report_labels = new JPanel(new GridLayout(2, 1, 5, 5));
report_labels.add(new JLabel("Current Account Balance: ", SwingConstants.RIGHT));
report_labels.add(new JLabel("Account Creation Date: ", SwingConstants.RIGHT));
JP_reportPane.add(report_labels, BorderLayout.WEST);
/* Create 'data' grid and text fields */
JPanel JP_data = new JPanel(new GridLayout(2, 1, 5, 5));
JP_data.add(TF_balance2 = new JTextField(10));
TF_balance2.setBackground(Color.WHITE);
TF_balance2.setEditable(false);
JP_data.add(TF_created = new JTextField(10));
TF_created.setBackground(Color.WHITE);
TF_created.setEditable(false);
JP_reportPane.add(JP_data, BorderLayout.CENTER);
/* Create 'buttons' grid and buttons */
JPanel JP_buttons = new JPanel(new GridLayout(2, 1, 5, 5));
JButton JB_report = new JButton("Report");
JB_report.setBackground(Color.GRAY);
JB_report.setMargin(new Insets(3, 3, 3, 3));
JB_report.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
reportAccount();
}
});
JP_buttons.add(JB_report);
JButton JB_close = new JButton("Close");
JB_close.setBackground(Color.GRAY);
JB_close.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
System.exit(0);
}
});
JP_buttons.add(JB_close);
JP_reportPane.add(JP_buttons, BorderLayout.EAST);
/* Create text area and scroll pane */
reportArea.setBorder(blackline);
reportArea.setForeground(Color.BLUE);
reportArea.setLineWrap(true);
reportArea.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane(reportArea);
reportArea.setEditable(false);
JP_reportPane.add(scrollPane, BorderLayout.SOUTH);
return JP_reportPane;
}
This is the method (called from JB_reportAction listener class shown above) where I try to display the array list in the text area (also in AccountUI class):
/**
* Method used to display account transaction history in the text field.
*/
protected void reportAccount()
{
reportArea.append(A.getTransactions());
}
And this is the method in the Account class that I am able to display the Array contents in a console output, but have been unable to figure out how to pass the Array contents to the AccountUI class as a String to display in the text area:
public ArrayList<TransactionObject> getTransactions()
{
for (int i = 0; i < transactionList.size(); i++)
{
System.out.println(transactionList.get(i));
System.out.println("\n");
}
return transactionList;
}
I hope I have clarified my issue without confusing anyone. Any insight would be much appreciated.
Call toString() on the list:
reportArea.append(A.getTransactions().toString());
Or, if you want to display the elements of the list in a different format, loop over the elements:
for (TransactionObject transaction : A.getTransactions()) {
reportArea.append(transaction.toString());
reportArea.append("\n");
}
Loops and types are an essential part of programming. You shouldn't use Swing if you don't understand loops and types.
Also, please respect the Java naming conventions. Variables start with a lower-case letter, and don't contain underscore. They're camelCased.
If you want to append content of objects in ArrayList to JTextArea you can use this :
for (Object obj : arrayList) {
textArea.append(obj.toString() + "");
}
You have to implement and override toString for TransactionObject.

Adding text next to a textfield to describe what data the user is expected to enter into it

I am trying to create a simple GUI that simulates a record store. I am still in the beginning stages.
I am running into trouble when I try to add text to describe what the user is expected to enter in the text field.
In addition, I am also having trouble positioning every textfield on its own line. In other words if there is space for two textfields in one line, then it displays in one line, and I am trying to display every text field on its own line.
This is what I tried so far:
item2 = new JTextField("sample text");
However the code above just adds default text within the text field, which is not what I need :/
I appreciate all the help in advance.
public class MyClass extends JFrame{
private JTextField item1;
private JTextField item2;
public MyClass(){
super("Matt's World of Music");
setLayout(new FlowLayout());
item1 = new JTextField();
item2 = new JTextField();
add(item1);
add(item2);
thehandler handler = new thehandler();
item1.addActionListener(handler);
item2.addActionListener(handler);
}
}
For your first problem, you need to use a JLabel to display your text. The constructor is like this:
JLabel label = new JLabel("Your text here");
Works really well in GUI.
As for getting things on their own lines, I recommend a GridLayout. Easy to use.
In your constructor, before adding anything, you do:
setLayout(new GridLayout(rows,columns,x_spacing,y_spacing));
x_spacing and y_spacing are both integers that determine the space between elements horizontally and vertically.
Then add like you have done. Fiddle around with it and you'll get it worked out.
So your final would look like:
setLayout(new GridLayout(2,2,10,10));
add(new JLabel("Text 1"));
add(text1);
add(new JLabel("text 2"));
add(text2);
You could just use a JLabel to label your textfields.
JLabel label1 = new JLabel("Item 1: ");
add(label1);
add(item1);
If you really want text inside the fields, you could set the text in the field with the constructor, and then add a MouseListener to clear the text on click:
item1 = new JTextField("Text");
item1.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (item1.getText().equals("Text")) // User has not entered text yet
item1.setText("");
}
});
Or, (probably better) use a FocusListener:
item1 = new JTextField("Text");
item1.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
if (item1.getText().equals("Text")) // User has not entered text yet
item1.setText("");
}
public void focusLost(FocusEvent e) {
if (item1.getText().equals("")) // User did not enter text
item1.setText("Text");
}
});
As for layout, to force a separate line, you use use a Box.
Box itemBox = Box.createVerticalBox();
itemBox.add(item1);
itemBox.add(item2);
add(itemBox);
Make:
item1 = new JTextField(10);
item2 = new JTextField(10);
that should solve problem with width of JTextField.
For beginning use GridLayout to display JTextField in one line. After that I strongly recomend using of MIG Layout http://www.migcalendar.com/miglayout/whitepaper.html.
put JLabel next to JTextField to describe what the user is expected to enter in the text field.
JLabel lbl = new JLabel("Description");
or you could also consider using of toolTipText:
item1.setToolTipText("This is description");
For making a form in Java Swing, I always recommend the FormLayout of JGoodies, which is designed to ... create forms. The links contains an example code snippet, which I just copy-pasted here to illustrate how easy it is:
public JComponent buildContent() {
FormLayout layout = new FormLayout(
"$label, $label-component-gap, [100dlu, pref]",
"p, $lg, p, $lg, p");
PanelBuilder builder = new PanelBuilder(layout);
builder.addLabel("&Title:", CC.xy(1, 1));
builder.add(titleField, CC.xy(3, 1));
builder.addLabel("&Author:", CC.xy(1, 3));
builder.add(auhtorField, CC.xy(3, 3));
builder.addLabel("&Price:", CC.xy(1, 5));
builder.add(priceField, CC.xy(3, 5));
return builder.getPanel();
}
Now for the description:
Use a label in front of the textfield to give a very short description
You can put a longer description in the textfield as suggested by #Alden. However, if the textfield is for short input, nobody will be able to read the description
You can use a tooltip (JComponent#setTooltipText) to put a longer description. Those tooltips also accept basic html which allows some formatting. Drawback of the tooltips is that the user of your application has to 'discover' that feature as there is no clear indication those are available
You can put a "help-icon" (like e.g. a question mark) after each text field (use a JButton with only an icon) where on click you show a dialog with a description (e.g. by using the JOptionPane class)
You can put one "help-icon" on each form which shows a dialog with a description for all fields.
Note for the dialog suggestion: I wouldn't make it a model one, allowing users to open the dialog and leave it open until they are finished filling in the form

Categories

Resources