can't getting updated text of JTextField - java

i can't getting updated text of JTextField from other class.
change text of JTextField ( from Names ) and go to Main tabbedPane and click button. updated text not appears on JOptionPane.
this is Frame.java
import java.awt.event.ActionEvent;
public class Frame {
JFrame frame;
JPanel pnl;
JButton btn;
JTabbedPane tabbedPane;
Names n;
public static void main(String[] args) {
Frame x = new Frame();
}
public Frame() {
SwingUtilities.invokeLater(()->Window());
}
public void Window() {
n = new Names();
frame = new JFrame();
tabbedPane = new JTabbedPane();
pnl = new JPanel();
btn = new JButton("get Name");
btn.addActionListener(e->JOptionPane.showMessageDialog(null, n.getName()));
pnl.add(btn);
tabbedPane.addTab("main", pnl);
tabbedPane.addTab("name", new Names());
frame.add(tabbedPane);
frame.setBounds(360, 130, 900, 550);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
this one is Names.java
import java.awt.Dimension;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Names extends JPanel {
JTextField tf;
public Names() {
tf = new JTextField("test");
tf.setPreferredSize(new Dimension(150,30));
this.add(tf);
}
public String getName() {
return tf.getText();
}
}
thanks for efforts.

Just take a second and have a look at the following...
n = new Names();
//...
btn.addActionListener(e->JOptionPane.showMessageDialog(null, n.getName()));
//...
tabbedPane.addTab("name", new Names());
What do you think is going to happen here?
The instance of Names which the button is interacting with is NOT the instance of Names that the user is interacting with
Change it to...
n = new Names();
//...
btn.addActionListener(e->JOptionPane.showMessageDialog(null, n.getName()));
//...
tabbedPane.addTab("name", n);
On a side note, this...
tf.setPreferredSize(new Dimension(150,30));
is a really bad idea and will come back to haunt you. Instead make use of the setColumns method (or constructor) to set the number of displayed characters the text field should attempt to display

Related

formatting JTextfields using another class

this is the code of the Gui Design class and below is the Class that provides functionality to the program. Im trying to get user input from the textfields so i can remove the text using the clearAll method and also save user input using the saveit method.I tried using nameEntry.setText(""); in the clearAll method but it wont work can someone help me please!
//Import Statements
import javax.swing.*;
import java.awt.*;
import javax.swing.JOptionPane;
import java.awt.event.*;
//Class Name
public class Customer extends JFrame {
Function fun = new Function();
public static void main(String[]args){
Customer.setLookAndFeel();
Customer cust = new Customer();
}
public Customer(){
super("Resident Details");
setSize(500,500);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
FlowLayout two = new FlowLayout(FlowLayout.LEFT);
setLayout(two);
JPanel row1 = new JPanel();
JLabel name = new JLabel("First Name",JLabel.LEFT);
JTextField nameEntry = new JTextField("",20);
row1.add(name);
row1.add(nameEntry);
add(row1);
JPanel row2 = new JPanel();
JLabel surname = new JLabel("Surname ",JLabel.LEFT);
JTextField surnameEntry = new JTextField("",20);
row2.add(surname);
row2.add(surnameEntry);
add(row2);
JPanel row3 = new JPanel();
JLabel contact1 = new JLabel("Contact Details : Email ",JLabel.LEFT);
JTextField contact1Entry = new JTextField("",10);
FlowLayout newflow = new FlowLayout(FlowLayout.LEFT,10,30);
setLayout(newflow);
row3.add(contact1);
row3.add(contact1Entry);
add(row3);
JPanel row4 = new JPanel();
JLabel contact2 = new JLabel("Contact Details : Phone Number",JLabel.LEFT);
JTextField contact2Entry = new JTextField("",10);
row4.add(contact2);
row4.add(contact2Entry);
add(row4);
JPanel row5 = new JPanel();
JLabel time = new JLabel("Duration Of Stay ",JLabel.LEFT);
JTextField timeEntry = new JTextField("",10);
row5.add(time);
row5.add(timeEntry);
add(row5);
JPanel row6 = new JPanel();
JComboBox<String> type = new JComboBox<String>();
type.addItem("Type Of Room");
type.addItem("Single Room");
type.addItem("Double Room");
type.addItem("VIP Room");
row6.add(type);
add(row6);
JPanel row7 = new JPanel();
FlowLayout amt = new FlowLayout(FlowLayout.LEFT,100,10);
setLayout(amt);
JLabel amount = new JLabel("Amount Per Day ");
JTextField AmountField = new JTextField("\u20ac ",10);
row7.add(amount);
row7.add(AmountField);
add(row7);
JPanel row8 = new JPanel();
FlowLayout prc = new FlowLayout(FlowLayout.LEFT,100,10);
setLayout(prc);
JLabel price = new JLabel("Total Price ");
JTextField priceField = new JTextField("\u20ac ",10);
row8.add(price);
row8.add(priceField);
add(row8);
JPanel row9 = new JPanel();
JButton clear = new JButton("Clear");
row9.add(clear);
add(row9);
JPanel row10 = new JPanel();
JButton save = new JButton("Save");
save.addActionListener(fun);
row10.add(save);
add(row10);
//Adding ActionListners
nameEntry.addActionListener(fun);
clear.addActionListener(fun);
save.addActionListener(fun);
setVisible(true);
}
private static void setLookAndFeel() {
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
);
} catch (Exception exc) {
// ignore error
}
}
}
//Import Statements
import javax.swing.*;
import java.awt.*;
import java.awt.Color;
import javax.swing.JOptionPane;
import java.awt.event.*;
//Class Name
public class Function implements ActionListener {
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if(command.equals("Add Customer")) {
Login();
}
else if(command.equals("Register")){
Registration();
}
else if(command.equals("Exit")){
System.exit(0);
}
else if(command.equals("Clear")){
ClearAllFields();
}
else if(command.equals("Save")){
SaveIt();
}
}
public static void Login(){
Customer cust = new Customer();
}
public static void Registration(){
//Do Nothing
}
//This clears all the text from the JTextFields
public static void ClearAllFields(){
}
//This will save Info on to another Class
public static void SaveIt(){
}
}
Alternatively, you can make nameEntry known to the Function class by defining it before calling the constructor for Function and then passing it into the constructor, like:
JTextField nameEntry = new JTextField("",20);
Function fun = new Function(nameEntry);
Then, in Function, add nameEntry as a member variable of Function and make a constructor for Function which accepts nameEntry, (right after the "public class Function..." line), like:
JTextField nameEntry;
public Function(JTextField nameEntry) {
this.nameEntry = nameEntry;
}
Now, the following will compile:
public void ClearAllFields(){
nameEntry.setText("");
}
And, the Clear button will clear the name field.
Again as per comments, one simple way to solve this is to give the gui public methods that the controller (the listener) can call, and then pass the current displayed instance of the GUI into the listener, allowing it to call any public methods that the GUI might have. The code below is simpler than yours, having just one JTextField, but it serves to illustrate the point:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class GUI extends JPanel {
private JTextField textField = new JTextField(10);
private JButton clearButton = new JButton("Clear");
public GUI() {
// pass **this** into the listener class
MyListener myListener = new MyListener(this);
clearButton.addActionListener(myListener);
clearButton.setMnemonic(KeyEvent.VK_C);
add(textField);
add(clearButton);
}
// public method in GUI that will do the dirty work
public void clearAll() {
textField.setText("");
}
// other public methods here to get text from the JTextFields
// to set text, and do whatever else needs to be done
private static void createAndShowGui() {
GUI mainPanel = new GUI();
JFrame frame = new JFrame("GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
class MyListener implements ActionListener {
private GUI gui;
// use constructor parameter to set a field
public MyListener(GUI gui) {
this.gui = gui;
}
#Override
public void actionPerformed(ActionEvent e) {
gui.clearAll(); // call public method in field
}
}
A better and more robust solution is to structure your program in a Model-View-Controller fashion (look this up), but this would probably be overkill for this simple academic exercise that you're doing.

How can I get the contents of my GUI to look a certain way? (Java)

So, I'm brand spankin' new to programming, so thanks in advance for your help. I'm trying to put this base 2 to base 10/base 10 to base 2 calculator I have made into a GUI. For the life of me I can't figure out how to nicely format it. I'm trying to make it look like the following: The two radio buttons on top, the input textfield bellow those, the convert button bellow that, the output field bellow that, and the clear button bellow that. Any ideas on how I can accomplish this?
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.*;
#SuppressWarnings("serial")
public class GUI extends JFrame implements ActionListener
{
private JTextField input;
private JTextField output;
private JRadioButton base2Button;
private JRadioButton base10Button;
private JButton convert;
private JButton clear;
private Container canvas = getContentPane();
private Color GRAY;
public GUI()
{
this.setTitle("Base 10-2 calc");
this.setLayout(new FlowLayout(FlowLayout.LEFT));
//this.setLayout(new GridLayout(2,2));
base2Button = new JRadioButton( "Convert to base 2");
base10Button = new JRadioButton( "Convert to base 10");
ButtonGroup radioGroup = new ButtonGroup();
radioGroup.add(base2Button);
radioGroup.add(base10Button);
JPanel radioButtonsPanel = new JPanel();
radioButtonsPanel.setLayout( new FlowLayout(FlowLayout.LEFT) );
radioButtonsPanel.add(base2Button);
radioButtonsPanel.add(base10Button);
canvas.add(radioButtonsPanel);
base2Button.setSelected( true );
base10Button.setSelected( true );
input = new JTextField(18);
//input = new JFormattedTextField(20);
canvas.add(input);
output = new JTextField(18);
//output = new JFormattedTextField(20);
canvas.add(output);
convert = new JButton("Convert!");
convert.addActionListener(this);
canvas.add(convert);
clear = new JButton("Clear");
clear.addActionListener(this);
canvas.add(clear);
output.setBackground(GRAY);
output.setEditable(false);
this.setSize(300, 200);
this.setVisible(true);
this.setLocation(99, 101);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
GUI app = new GUI();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
#Override
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if(s.equals("Convert!"))
{
String numS = input.getText();
int numI = Integer.parseInt(numS);
if(base2Button.isSelected())
{
output.setText(Integer.toBinaryString(Integer.valueOf(numI)));
}
if(base10Button.isSelected())
{
output.setText("" + Integer.valueOf(numS,2));
}
}
if(s.equals("Clear"))
{
input.setText("");
output.setText("");
}
}
}
For a simple layout, you could use a GridLayout with one column and then use a bunch of child panels with FlowLayout which align the components based on the available space in a single row. If you want more control, I'd suggest learning about the GridBagLayout manager which is a more flexible version of GridLayout.
public class ExampleGUI {
public ExampleGUI() {
init();
}
private void init() {
JFrame frame = new JFrame();
// Set the frame's layout to a GridLayout with one column
frame.setLayout(new GridLayout(0, 1));
frame.setPreferredSize(new Dimension(300, 300));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Child panels, each with FlowLayout(), which aligns the components
// in a single row, until there's no more space
JPanel radioButtonPanel = new JPanel(new FlowLayout());
JRadioButton button1 = new JRadioButton("Option 1");
JRadioButton button2 = new JRadioButton("Option 2");
radioButtonPanel.add(button1);
radioButtonPanel.add(button2);
JPanel inputPanel = new JPanel(new FlowLayout());
JLabel inputLabel = new JLabel("Input: ");
JTextField textField1 = new JTextField(15);
inputPanel.add(inputLabel);
inputPanel.add(textField1);
JPanel convertPanel = new JPanel(new FlowLayout());
JButton convertButton = new JButton("Convert");
convertPanel.add(convertButton);
JPanel outputPanel = new JPanel(new FlowLayout());
JLabel outputLabel = new JLabel("Output: ");
JTextField textField2 = new JTextField(15);
outputPanel.add(outputLabel);
outputPanel.add(textField2);
// Add the child panels to the frame, in order, which all get placed
// in a single column
frame.add(radioButtonPanel);
frame.add(inputPanel);
frame.add(convertPanel);
frame.add(outputPanel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
ExampleGUI example = new ExampleGUI();
}
}
The end result:

Alignment of RadioButtons in Java GUI

Here is what my program is suppose to look like:
but I can't seem to get my radio buttons and my JLabel to be aligned properly. How do I align my radio buttons on the right and stacked? Also, how do I get my JLabel and JTextField to show stacked?
Here is my code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SeriesCalc extends JFrame {
private final int WIDTH = 300;
private final int HEIGHT = 300;
private JFrame frame = new JFrame("Recursion");
private JPanel panel = new JPanel();
private JPanel labels = new JPanel();
private JPanel buttons = new JPanel();
private JPanel radioButtonsPanel = new JPanel();
private JLabel inputLabel = new JLabel("Enter i:");
private JLabel resultLabel = new JLabel("Result:");
private JTextField inputField = new JTextField(15);
private JTextField resultField = new JTextField(15);
private JButton compute = new JButton("Compute");
private JRadioButton iterative, recursive;
public SeriesCalc() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
radioButtonsPanel.add(iterative = new JRadioButton("Iterative"));
radioButtonsPanel.add(recursive = new JRadioButton("Recursive"));
add(radioButtonsPanel);
ButtonGroup radioButtons = new ButtonGroup();
radioButtons.add(iterative);
radioButtons.add(recursive);
iterative.addActionListener(new Calculations());
recursive.addActionListener(new Calculations());
compute.addActionListener(new Calculations());
resultField.setEditable(false);
panel.setLayout(new GridLayout(4, 1));
labels.add(inputLabel);
labels.add(inputField);
labels.add(resultLabel);
labels.add(resultField);
buttons.add(compute);
panel.add(radioButtonsPanel);
panel.add(labels);
panel.add(buttons);
frame.getContentPane().add(panel);
}
public void display() {
frame.setSize(WIDTH, HEIGHT);
frame.setVisible(true);
}
public class Calculations implements ActionListener {
public void actionPerformed(ActionEvent e) {
Object calc = e.getSource();
try {
if (calc == compute) {
if (iterative.isSelected()) {
double n = Double.parseDouble(inputField.getText());
double product = 1;
for (int i = 3; i < n; i++) {
product *= i;
}
resultField.setText(Double.toString(product));
} else if (recursive.isSelected()) {
double i = Double.parseDouble(inputField.getText());
double y = 0;
if (i == 1) {
resultField.setText(Double.toString(i / (2. * i + 1)));
} else {
resultField.setText(Double.toString(i / (2. * i + 1)+ (i -1)));
}
}
}
} catch (NumberFormatException nfe) {
System.err.println(nfe.getMessage());
}
}
}
public static void main(String[] args) {
SeriesCalc calculator = new SeriesCalc();
calculator.display();
}
}
I see some errors in your program:
You're extending JFrame and creating an instance of it in the same program. Use one or the other (I recommend the latter), See Using extends vs calling it inside of class.
You're setting frame.setSize() while this isn't an error it's always better to call frame.pack() in your program. It will respect the minimum size where all the components are shown in their preferredSizes. If you need an exact size for your window override getPreferredSize() method instead.
You're not placing your program on the Event Dispatch Thread (EDT), this could cause threading issues in the future as Swing is not thread safe, this can be solved with:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
//Call your constructor here
}
});
}
but I can't seem to get my radio buttons and my JLabel to be aligned properly
This is because a JPanel's default layout manager is FlowLayout and thus it will place your components in a single row.
My idea to get to your desired GUI was to use a single GridLayout with 0 rows (it will add as many as needed) and 2 columns, and where you need a "blank" space you can add empty JLabels.
I didn't placed an ActionListener on my code as this question is about the GUI design not the logic inside it.
I think I'm not missing anything, this is the output that the below code creates:
import java.awt.GridLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class SeriesCalc {
private JFrame frame;
private JRadioButton iterative;
private JRadioButton recursive;
private ButtonGroup group;
private JLabel label;
private JLabel resultLabel;
private JTextField field;
private JTextField resultField;
private JButton computeButton;
private JPanel pane;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new SeriesCalc().createAndShowGui();
}
});
}
public void createAndShowGui() {
frame = new JFrame(getClass().getSimpleName());
pane = new JPanel();
pane.setLayout(new GridLayout(0, 2, 2, 5));
iterative = new JRadioButton("Iterative");
recursive = new JRadioButton("Recursive");
group = new ButtonGroup();
group.add(iterative);
group.add(recursive);
computeButton = new JButton("Compute");
label = new JLabel("Enter \"i\": ");
resultLabel = new JLabel("Result: ");
field = new JTextField(5);
resultField = new JTextField(5);
resultField.setEnabled(false);
pane.add(new JLabel(""));
pane.add(iterative);
pane.add(new JLabel(""));
pane.add(recursive);
pane.add(label);
pane.add(field);
pane.add(new JLabel(""));
pane.add(computeButton);
pane.add(resultLabel);
pane.add(resultField);
frame.add(pane);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
I donĀ“t know if you are using an IDE like Eclipse or NetBeans. In Eclipse for example, you can open this class with windowbuilder and you could visualize how your Frame will appear. Obviously you can move element in this view (window builder) and put all objects where you want and the size you want. In the next image you could see the view of an edit of a Frame if you open it with window builder in mode design in Eclipse. In this mode (design) you can put absolute layout to these objects and they can be put where you want with that layout. I recommend you to use an IDE like eclipse for Java Developing, it is very useful and it has a lot of facilities. I hope I could help you:
enter image description here

Display a window to obtain user input

I'm new to programming with Java.
I have these two small projects.
import javax.swing.*;
public class tutorial {
public static void main(String[] args){
JFrame frame = new JFrame("Test");
frame.setVisible(true);
frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("hello");
JPanel panel = new JPanel();
frame.add(panel);
panel.add(label);
JButton button = new JButton("Hello again");
panel.add(button);
}
}
and this one:
import java.util.*;
public class Test {
public static void main(String[] args){
int age;
Scanner keyboard = new Scanner(System.in);
System.out.println("How old are you?");
age = keyboard.nextInt();
if (age<18)
{
System.out.println("Hi youngster!");
}
else
{
System.out.println("Hello mature!");
}
}
}
How can I add the second code to the first one, so that the user will see a window that says 'How old are you' and they can type their age.
Thanks in advance!
I'm not sure of all the things you wanted because it was undefined, but as I far as I understand, you wanted a JFrame containing an input field, in which you will be able to input values and display the appropriate answer.
I also suggest you read tutorial , but don't hesitate if you have question.
I hope it's a bit close to what you wanted.
package example.tutorial;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Tutorial extends JPanel {
private static final String YOUNG_RESPONSE = "Hi youngster!";
private static final String ADULT_RESPONSE = "Hello mature!";
private static final String INVALID_AGE = "Invalid age!";
private static final int MIN_AGE = 0;
private static final int MAX_AGE = 100;
private static JTextField ageField;
private static JLabel res;
private Tutorial() {
setLayout(new BorderLayout());
JPanel northPanel = new JPanel();
northPanel.setLayout(new FlowLayout());
JLabel label = new JLabel("How old are you ? ");
northPanel.add(label);
ageField = new JTextField(15);
northPanel.add(ageField);
add(northPanel, BorderLayout.NORTH);
JPanel centerPanel = new JPanel();
JButton btn = new JButton("Hello again");
btn.addActionListener(new BtnListener());
centerPanel.add(btn);
res = new JLabel();
res.setVisible(false);
centerPanel.add(res);
add(centerPanel, BorderLayout.CENTER);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Test");
frame.add(new Tutorial());
frame.setVisible(true);
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private static class BtnListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
String content = ageField.getText();
int age = -1;
try{
age = Integer.parseInt(content);
if(isValid(age)) {
res.setText(age < 18 ? YOUNG_RESPONSE : ADULT_RESPONSE);
} else {
res.setText(INVALID_AGE);
}
if(!res.isVisible())
res.setVisible(true);
} catch(NumberFormatException ex) {
res.setText("Wrong input");
}
}
private boolean isValid(int age) {
return age > MIN_AGE && age < MAX_AGE;
}
}
}
so that the user will see a window that says 'How old are you' and they can type their age.
The easiest way to start would be to use a JOptionPane. Check out the section from the Swing tutorial on How to Use Dialogs for examples and explanation.
Don't forget to look at the table of contents for other Swing related topics for basic information about Swing.
You will need an input field to get the text and then add an ActionListener to the button which contains the logic that is performed when the button is pressed.
I modified your code to implement what you described:
import javax.swing.*;
import java.awt.event.*;
public class tutorial {
public static void main(String[] args) {
JFrame frame = new JFrame("Test");
frame.setVisible(true);
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("hello");
JPanel panel = new JPanel();
frame.add(panel);
panel.add(label);
final JTextField input = new JTextField(5); // The input field with a width of 5 columns
panel.add(input);
JButton button = new JButton("Hello again");
panel.add(button);
final JLabel output = new JLabel(); // A label for your output
panel.add(output);
button.addActionListener(new ActionListener() { // The action listener which notices when the button is pressed
public void actionPerformed(ActionEvent e) {
String inputText = input.getText();
int age = Integer.parseInt(inputText);
if (age < 18) {
output.setText("Hi youngster!");
} else {
output.setText("Hello mature!");
}
}
});
}
}
In that example we don't validate the input. So if the input is not an Integer Integer.parseInt will throw an exception. Also the components are not arranged nicely. But to keep it simple for the start that should do it. :)
it's not an easy question since you are working on command line in one example, and in a Swing GUI in the other.
Here's a working example, i've commented here and there.
This is no where near java best practises and it misses a lot of validation (just see what happens when you enter a few letters or nothing in the textfield.
Also please ignore the setLayout(null) and setBounds() statements, it's just a simple example not using any layout managers.
I hope my comments will help you discovering java!
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
//You'll need to implement the ActionListener to listen to buttonclicks
public class Age implements ActionListener{
//Declare class variables so you can use them in different functions
JLabel label;
JTextField textfield;
//Don't do al your code in the static main function, instead create an instance
public static void main(String[] args){
new Age();
}
// this constructor is called when you create a new Age(); like in the main function above.
public Age()
{
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(0,0,300,300);
panel.setLayout(null);
label = new JLabel("hello");
label.setBounds(5,5,100,20);
// a JTextfield allows the user to edit the text in the field.
textfield = new JTextField();
textfield.setBounds(5,30,100,20);
JButton button = new JButton("Hello again");
button.setBounds(130,30,100,20);
// Add this instance as the actionlistener, when the button is clicked, function actionPerformed will be called.
button.addActionListener(this);
panel.add(label);
panel.add(textfield);
panel.add(button);
frame.add(panel);
frame.setVisible(true);
}
//Required function actionPerformed for ActionListener. When the button is clicked, this function is called.
#Override
public void actionPerformed(ActionEvent e)
{
// get the text from the input.
String text = textfield.getText();
// parse the integer value from the string (! needs validation for wrong inputs !)
int age = Integer.parseInt(text);
if (age<18)
{
//instead of writing out, update the text of the label.
label.setText("Hi youngster!");
}
else
{
label.setText("Hello mature!");
}
}
}

What would be the best way of making this piece of code smaller?

I was wondering if we are adding the same components to java (in the main class)over and over again and writing separate code for each of them, would it be possible to make the code smaller? e.g. if we are adding buttons and labels many times, which each do different job, would it it possible to have them in less code or does it have to be like that e.g.
JLabel label = new JLabel("Text1");
label.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(label);
JTextField field = new JTextField();
panel.add(field);
JLabel label1 = new JLabel("Text2");
label1.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(label1);
JTextField field1 = new JTextField();
panel.add(field1);
field1.setEnabled(false);
JLabel label2 = new JLabel("Text3");
label2.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(label2);
JTextField field2 = new JTextField();
panel.add(field2);
field2.setEnabled(false);
In my code I have to add the same components over and over again like 10 times but each one is doing a different job, would it be possible to have them in less code?
Also I want to be able to store the values of each textbox in a different variable, e.g. store the value of field1 to int number;.
Create a method that you can reuse:
private void method createLabel(JPanel panel, String text) {
JLabel label = new JLabel(text);
label.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(label);
JTextField field = new JTextField();
panel.add(field);
}
If you need extra things like setEnabled() or whatever, just pass parameters in order to do it or not depending on requirements. If you need the Labels back just change void to JLabel and return it. Then you call it like this
createLabel(panel, "text1");
createLabel(panel, "text2");
...
1.Create ArrayList
2.Create method for adding JLabel to arraylist
3.Make it take a String name parameter
4.Profit???
"would it be possible to make the code smaller? "
Yes. One way, as #iberbue suggested is to use a method. You can have a method that return a JPanel and pass arguments to it
Have a look at this example I was working on for a different question. But it serves the same purpose. Look at the method createPanel that return a JPanel. Then I just use JPanel panel = createPanel(...);
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Test2 {
Map<String, JTextField> fields;
Map<String, JLabel> labels;
public Test2() {
fields = new HashMap<>();
labels = new HashMap<>();
JPanel mainPanel = new JPanel(new GridLayout(10, 1));
for (int i = 1; i <= 10; i++) {
JPanel panel = createPanel("Text Field " + i);
mainPanel.add(panel);
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new Test2();
}
});
}
private JPanel createPanel(String fieldName) {
JPanel panel = new JPanel();
JTextField field = new JTextField(15);
field.addActionListener(new FieldListener());
fields.put(fieldName, field);
JLabel label = new JLabel(fieldName);
label.addMouseListener(new MouseHandler());
labels.put(fieldName, label);
JButton button = new JButton(fieldName);
button.addActionListener(new ButtonListener());
panel.add(label);
panel.add(field);
panel.add(button);
return panel;
}
public class ButtonListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton)e.getSource();
String fieldName = button.getText();
JTextField field = fields.get(fieldName);
System.out.println(field.getText());
}
}
public class FieldListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
JTextField field = (JTextField)e.getSource();
System.out.println(field.getText());
}
}
public class MouseHandler extends MouseAdapter {
#Override
public void mouseClicked(MouseEvent e) {
JLabel label = (JLabel)e.getSource();
System.out.println(label.getText());
}
}
}
You can write a method, something like this.
private void add(JPanel panel, int i, boolean enabled){
JLabel label = new JLabel("Text" + i);
label.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(label);
JTextField field = new JTextField();
panel.add(field);
field.setEnabled(enabled);
}
And then you call it 3 times as needed.
You've noticed that most code is repeated. This is a good first step.
Then, notice what parts change (semantically) between iterations (that is, ignore the different variable names and other unimportant info): new JLabel(theLabelThatChanges)and .setEnabled(thisFieldEnabled).
Then, put all these in a loop:
for(int i = 0; i < 10; i++)
{
JLabel label2 = new JLabel(theLabelThatChanges);
label2.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(label2);
JTextField field2 = new JTextField();
panel.add(field2);
field1.setEnabled(thisFieldEnabled);
}
Make sure have a collection of values for the thing that changes:
String[] labels = new String[] { "Text1", "Text2", "Text3" };
boolean[] enabledBoxes = new boolean { true, false, true };
Be sure to grab a value in each iteration:
String theLabelThatChanges = labels[i];
boolean thisFieldEnabled = enabledBoxes[i];
Final code:
String[] labels = new String[] { "Text1", "Text2", "Text3" };
boolean[] enabledBoxes = new boolean { true, false, true };
for(int i = 0; i < 3; i++)
{
String theLabelThatChanges = labels[i];
boolean thisFieldEnabled = enabledBoxes[i];
JLabel label2 = new JLabel(theLabelThatChanges);
label2.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(label2);
JTextField field2 = new JTextField();
panel.add(field2);
field1.setEnabled(thisFieldEnabled);
}
As others have commented, you could then take the part that creates all this stuff and make it a separate procedure that takes the label and the enabled status as parameters:
private void addControls(String theLabelThatChanges, boolean thisFieldEnabled)
{
JLabel label2 = new JLabel(theLabelThatChanges);
label2.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(label2);
JTextField field2 = new JTextField();
panel.add(field2);
field1.setEnabled(thisFieldEnabled);
}
Then call it from the loop:
for(int i = 0; i < 3; i++)
{
addControls(labels[i], enabledBoxes[i]);
}
Complexer answer.
If you want to be OO.
Make class CustomJButton extends JButton
Set Everything you need in the contructor
You will be able to do this if you're not going to use the Button vars anymore
new CustomJButton("NAME");
You will be able to do this if you're going to use the Button vars
CustomJButton j = new CustomJButton("NAME");
I thought there already should be some fluent swing GUI API, but I did not find any on the spur. JavaFX GUI on Java 8 is nice.
Though Java 8 allows terse event listeners with Java 7 one can do a fluent API too.
JPanel panel = new FrameBuilder().panel()
.children()
.label("Text1")
.setHorizontalAlignment(SwingConstants.CENTER)
.textField()
.setId("field1")
.label("Text2")
.setHorizontalAlignment(SwingConstants.CENTER)
.textField()
.setId("field2")
.setEnabled(false)
.endChildren()
.create();
Where FrameBuilder.panel gives a PanelBuilder whose create returns the JPanel. Or so.

Categories

Resources