[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 393
  • Last Modified:

variable names and JTextField

i need to give a variable name to each of my JTextField and then add it to the panel how do i do about this without errors this is my code...please write comments right next to the code where it should be added

import java.awt.*;
import javax.swing.*;
public class Administration
{
            
      
      JTextField tittle, author, publisher, year, isbn, category, book_number, unit_price;
      JButton search, update, clear, add;
      JPanel Panel, tPanel;
      JPanel AdminPanel;
      Container c = getContentPane();

public Administration()
{
      makeGUI();
}


//CREATE THE USER INTERFACE
void makeGUI()
{
    c.setLayout(new BorderLayout());
      AdminPanel = new JPanel(new BorderLayout());

tPanel = new JPanel(new GridBagLayout());
tPanel.setBackground(Color.magenta);

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;



title = new JTextField(20),gbc);
gbc.gridx = 1;
gbc.gridy = 0;
tPanel.add(new JLabel("TITLE"),title,gbc);
gbc.gridx = 0;
gbc.gridy++;
author = new JTextField(20),gbc);
gbc.gridx = 1;
tPanel.add(new JLabel("AUTHOR"),author,gbc);
gbc.gridx = 0;
gbc.gridy++;
publisher = new JTextField(20),gbc);
gbc.gridx = 1;
tPanel.add(new JLabel("PUBLISHER"),publisher,gbc);
gbc.gridx = 0;
gbc.gridy++;
year = new JTextField(20),gbc);
gbc.gridx = 1;
tPanel.add(new JLabel("YEAR"),year,gbc);
gbc.gridx = 0;
gbc.gridy++;
isbn = new JTextField(20),gbc);
gbc.gridx = 1;
tPanel.add(new JLabel("ISBN"),isbn,gbc);
gbc.gridx = 0;
gbc.gridy++;
category = new JTextField(20),gbc);
gbc.gridx = 1;
tPanel.add(new JLabel("CATEGORY"),category,gbc);
gbc.gridx = 0;
gbc.gridy++;
book_number = new JTextField(20),gbc);
gbc.gridx = 1;
tPanel.add(new JLabel("NUMBER"),book_number,gbc);
gbc.gridx = 0;
gbc.gridy++;
unit_price = new JTextField(20),gbc);
gbc.gridx = 1;
tPanel.add(new JLabel("UNIT PRICE"),unit_price,gbc);
gbc.gridx = 0;
gbc.gridy++;

AdminPanel.add(tPanel, BorderLayout.NORTH)

c.setVisible(true);
      }
}
0
lucinda77
Asked:
lucinda77
  • 2
2 Solutions
 
esorfCommented:
I think the pattern you're looking for is:

            author = new JTextField(20); // Create author text field
            gbc.gridx = 0;  // Set X to the field column
            gbc.gridy++;  // Move down a row
            tPanel.add(author, gbc);  // Add it to the panel
            gbc.gridx = 1;  // Move over to the label column
            tPanel.add(new JLabel("AUTHOR"), gbc);  // Add the label

There should be a block like this for each text field, replacing the field member variable and label appropriately.

Cheers,

- Frank
0
 
DrWarezzCommented:
..to assign text from a variable to a JTextField, use this:

JTextField jtf = new JTextField();
pane.add( jtf);
// ...
String text = "something...";
jtf.setText( text );

gL,
[r.D]
0
 
DrWarezzCommented:
ta :-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now