Reputation: 1
I wrote this after seeing videos of thenewboston and then, when I ran, only the last item added was visible and all other textFields [textField1, textField2 and textField3]
are not visible. It worked perfectly in his videos but when I tried, only the passwordField was visible. I'm a beginner and I was unable to find what's the problem. Please help me out what is the problem and what should I do to mitigate this error. One small request, please explain a bit detail because I a newbie to Java and GUI.
package learningPackage;
import java.awt.FlowLayout;
//gives the layout
import java.awt.event.ActionListener;
//this makes the field wait for an event.
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JTextField;
//creates a field where we can type text.
import javax.swing.JPasswordField;
//creates a text field where the text typed is hidden with asterics.
class tuna extends JFrame
{
private JTextField textField1;
private JTextField textField2;
private JTextField textField3;
private JPasswordField passwordField;
public tuna()
{
super("Title Text");
textField1 = new JTextField(10);
//sets a the default value of 10.
add(textField1);
textField2 = new JTextField("Enter a Text");
//sets default text of "Enter a Text" without the quotes
add(textField2);
textField3 = new JTextField("Uneditable", 20);
//Displays Uneditable with a default value of 20.
//To make this Uneditable, you must do this...
textField3.setEditable(false);
//this makes the textField uneditable.
add(textField3);
passwordField = new JPasswordField("myPassword");
add(passwordField);
thehandler Handler = new thehandler();
textField1.addActionListener(Handler);
textField2.addActionListener(Handler);
textField3.addActionListener(Handler);
passwordField.addActionListener(Handler);
/*
* The addActionListener method takes an object of Event Handler class.
*
* Therefore, we must create an object of Event Handler Class.
*
*
* The addActionListener method takes an object because, sometimes, we might
* have different classes with different code to execute. So, we pass the object to
* identify which class code is to be executed.
*/
}
class thehandler implements ActionListener
{
/*
* In order to handle events in Java, you need Event handler Class.
* and, that Event Handler Class must implement ActionListener.
*
* What the ActionListener does is that
*
* It will wait for some Event to happen and after that particular event happens,
* it will implement some piece of code.
*/
public void actionPerformed(ActionEvent event)
{
String string = "";
if(event.getSource()==textField1)
string = String.format("Text Field 1 : %s", event.getActionCommand());
else if(event.getSource()==textField2)
string = String.format("Text Field 2 : %s", event.getActionCommand());
else if(event.getSource()==textField3)
string = String.format("Text Field 3 : %s", event.getActionCommand());
else if(event.getSource()==passwordField)
string = String.format("Password Field : %s", event.getActionCommand());
//when the user presses enter key after entering text, this actually stores the
//thing entered into the String.
JOptionPane.showConfirmDialog(null, string);
}
}
}
public class Apples {
public static void main(String[] args)
{
tuna Srivathsan = new tuna();
Srivathsan.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//this would close the window when X button is pressed.
Srivathsan.setSize(500, 500);
Srivathsan.setVisible(true);
Srivathsan.setLocationRelativeTo(null);
}
}
Here is the image of the window :
Upvotes: 0
Views: 85
Reputation: 168825
import java.awt.FlowLayout;
//gives the layout
That statement imports the layout & makes it available to the code, but does not set the layout on any container.
While a JPanel
has a default layout of FlowLayout
, the default layout of the (content pane of the) JFrame
is BorderLayout
. A BorderLayout
accepts up to 5 components or containers (like JPanel
) in each of 5 constraints. If no constraint is given, it defaults to the CENTER
. If more than one component is added to the CENTER
(or any other area of a BorderLayout
), all but one of those components will fail to appear. I cannot recall if it is the first or last that appears, but it's a moot point, because the code should not do that.
My advice would be as follows:
JFrame
(or JPanel
).JPanel
to the JFrame
.JPanel
.Here is an example implementing points 2 & 3. Point 1 is not implemented (batteries not included).
import javax.swing.*;
public class SingleComponentLayoutProblem extends JFrame {
private final JTextField textField1;
private final JTextField textField2;
private final JTextField textField3;
private final JPasswordField passwordField;
public SingleComponentLayoutProblem() {
super("Title Text");
JPanel panel = new JPanel(); // uses FlowLayout be DEFAULT
add(panel);
textField1 = new JTextField(10);
//sets a the default value of 10.
panel.add(textField1);
textField2 = new JTextField("Enter a Text");
//sets default text of "Enter a Text" without the quotes
panel.add(textField2);
textField3 = new JTextField("Uneditable", 20);
//Displays Uneditable with a default value of 20.
//To make this Uneditable, you must do this...
textField3.setEditable(false);
//this makes the textField uneditable.
panel.add(textField3);
passwordField = new JPasswordField("myPassword");
panel.add(passwordField);
}
public static void main(String[] args) {
Runnable r = () -> {
SingleComponentLayoutProblem sclp =
new SingleComponentLayoutProblem();
sclp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//this would close the window when X button is pressed.
// don't guess sizes! ..
//sclp.setSize(500, 500);
// .. instead ..
sclp.pack();
sclp.setVisible(true);
sclp.setLocationRelativeTo(null);
};
SwingUtilities.invokeLater(r);
}
}
Upvotes: 2