0

I am quite new to Java and learning as a student. I hope you can understand me.

As you can see from the code below, I've only coded the frame and the label. The user should be able to write some song recommendations. There should be at least a few text fields created by an array. When the user makes changes, the new text should be displayed using JOptionPane.WARNING_MESSAGE.

I need some guidance on how I can create an array of JTextField and retrieve the text from an array of String. In addition, how can I use JOptionPane to display all of the text?

Thank you.

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;

class TextFrame extends JFrame 
{
    private final JLabel cLabel;

    public TextFrame() 
    {
        super("Hello there!");
        setLayout(new FlowLayout());

        cLabel = new JLabel("Please write some song recommendations.");
        cLabel.setToolTipText("Write below.");
        add(cLabel);
    }
}

public class TestFrame
{
  public static void main (String [] args)
    {
        TextFrame frame = new TextFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 400); 
        frame.setVisible(true); 
    }
}
1
  • array of textfields? JTextField[] NAME = new JTextField[size of array]; Commented Oct 30, 2019 at 22:36

2 Answers 2

1

You should make an actionListener that checks to see if you have typed anything. You could have another listener or Mnemonic so when you hit enter it will update everything. When you hit enter, you could get the text from the JTextField or JTextArea and then save that into an array of Strings (ie String[] stringArray = new String[<num of items>] This way you could just have one textField and you will be able to store everything in a String[] instead of an array of Text Fields? I hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

0
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class framearray2 extends JFrame implements ActionListener
{
JCheckBox c1[];
JTextField t1[];
int i;
framearray2(String p)
{
super(p);
c1=new JCheckBox[2];
t1=new JTextField[2];
for(i=0;i<2;i++)
{
t1[i]=new JTextField(40);
c1[0]=new JCheckBox("Singing");
c1[0].setBackground(Color.red);
c1[1]=new JCheckBox("Cricket",true);
}
for(i=0;i<2;i++)
{
add(t1[i]);
add(c1[i]);
t1[i].addActionListener(this);
}
setLayout(new FlowLayout());
setFont(new Font("Arial",Font.ITALIC,40));
}
public void actionPerformed(ActionEvent e)
{
for(i=0;i<2;i++)
{
if(e.getSource().equals(t1[0]))
{
t1[0].setBackground(Color.red);
}
if(e.getSource().equals(t1[1]))
{
t1[1].setBackground(Color.blue);
}
}
}
public static void main(String s[])
{
framearray2 f1=new framearray2("hello");
f1.setSize(600,500);
f1.setVisible(true);
}

}

1 Comment

@dr-varun-tiwari: please add some information explaining how your code solves the problem - this will help others in the future

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.