Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

java - Auto Populate a jTextField with NetBeans

I have created a jFrame using NetBeans that opens when a button on the main GUI is clicked to add a new entry. I want to know if there is a way to supply a unique id for each new entry that shows when the jFrame form is displayed. Along with a unique id I want to also have a text field createdOn that is auto populated with the current date.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For the life of the JVM executing the program, hashCode() may serve as a a unique ID; UUID is an alternative. The example shows a new Date each time the button is pressed.

Addendum: On closer scrutiny, the hashCode() method of java.util.Date may not be unique. In particular, "It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results." You may be able to use the long from getTime(), subject to the one millisecond resolution.

alt text

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/** @see http://stackoverflow.com/questions/4128432 */
public class AddTest extends JPanel {

    private static final DateFormat format =
        new SimpleDateFormat("yyyy-MMM-dd hh:mm:ss.SSS");
    private final List<TestPanel> panels = new ArrayList<TestPanel>();

    public AddTest() {
        this.setLayout(new GridLayout(0, 1));
        TestPanel tp = new TestPanel();
        panels.add(tp);
        this.add(tp);
        this.validate();
        Dimension d = tp.getPreferredSize();
        this.setPreferredSize(new Dimension(d.width, d.height * 8));
    }

    private static class TestPanel extends JPanel {

        public TestPanel() {
            Date date = new Date();
            this.add(new JLabel(String.valueOf(date.hashCode())));
            this.add(new JTextField(format.format(date)));
        }
    }

    private void display() {
        JFrame f = new JFrame("AddTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this, BorderLayout.CENTER);
        JButton button = new JButton(new AbstractAction("New") {

            @Override
            public void actionPerformed(ActionEvent e) {
                TestPanel tp = new TestPanel();
                panels.add(tp);
                AddTest.this.add(tp);
                AddTest.this.revalidate();
                AddTest.this.repaint(); // may be required
            }
        });
        f.add(button, BorderLayout.SOUTH);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new AddTest().display();
            }
        });
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...