CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Oct 2009
    Posts
    10

    Trying to add JTextField to JTabbed Panel

    I have a Tabbed panel defined at design time. It has two tabs, which are also added at design time.

    What I am trying to do is add a JTextField during run time. Here is the beginning of the code that adds the JTextField.

    int numFields = Integer.parseInt(jTextField2.getText());
    int x = jTextField2.getX();
    int y = jTextField2.getY();

    fieldNames = new JTextField[numFields];
    tedsLabels = new JLabel[numFields * 3];
    beginBit = new JTextField[numFields];
    fieldLength = new JTextField[numFields];

    fieldNames[0] = new JTextField();
    //fieldNames[0].setLocation(x, y + 30);
    fieldNames[0].setText(" ");
    jPanel3.add(fieldNames[0]);


    The JTextField does not display when I run the program. Eventually this will be put into a for loop, and I want to set the location using a calculation I will work out later.

    Kevin

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Trying to add JTextField to JTabbed Panel

    Did you call validate() on the panel to get it to layout all its components? validate() should be called whenever you add or remove components.

    The important thing in science is not so much to obtain new facts as to discover new ways of think about them...
    W. Bragg
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Oct 2009
    Posts
    10

    Re: Trying to add JTextField to JTabbed Panel

    I just added the validate() and it still didn't work.

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Trying to add JTextField to JTabbed Panel

    I think we need to see the full context of this code. If you can post up all the relevant code, or a simple, minimal, complete example of the problem, we may be able to make a better judgement of what's wrong.

    Simplicity is prerequisite for reliability...
    E. Dijkstra
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    Oct 2009
    Posts
    10

    Re: Trying to add JTextField to JTabbed Panel

    Here is the entire project.

    Thanks.
    Attached Files Attached Files
    Last edited by Bear35805; October 21st, 2009 at 10:14 AM. Reason: Added Zip file

  6. #6
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Trying to add JTextField to JTabbed Panel

    OK, just dump the whole project on us. Against my better judgement, I did download the whole thing, but I don't know which Java file contains the relevant code and I'm not going to plough through the whole project looking for it. It would help if you just posted the relevant source code when asked for the relevant code. Most of us aren't interested in your project libraries, DLLs, compiled classes, etc.

    But I re-read your original post, because it seemed a bit strange - you talked about a Tabbed panel, and Swing doesn't have a JTabbedPanel, it has a JTabbedPane, and it also has a JPanel. I assumed you were using a JTabbedPane with a JPanel. If this isn't the case, maybe you need to read up on the JTabbedPane API Javadocs and the How To Use Tabbed Panes tutorial.

    There is nothing so useless as doing efficiently that which should not be done at all...
    P. Drucker
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  7. #7
    Join Date
    Oct 2009
    Posts
    10

    Re: Trying to add JTextField to JTabbed Panel

    I apologize. Since I had put the entire method in my original post, I mistakenly assumed you needed the entire project. Here is just the source code from a project that I am using to test the concept. The code that adds the jTextFields resides in jButton1ActionPerformed method.

    /*
    * TestDynamicTextFieldsView.java
    */

    package testdynamictextfields;

    import org.jdesktop.application.Action;
    import org.jdesktop.application.ResourceMap;
    import org.jdesktop.application.SingleFrameApplication;
    import org.jdesktop.application.FrameView;
    import org.jdesktop.application.TaskMonitor;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.Timer;
    import javax.swing.Icon;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JTextField;

    /**
    * The application's main frame.
    */
    public class TestDynamicTextFieldsView extends FrameView {

    public TestDynamicTextFieldsView(SingleFrameApplication app) {
    super(app);

    initComponents();

    // status bar initialization - message timeout, idle icon and busy animation, etc
    ResourceMap resourceMap = getResourceMap();
    int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
    messageTimer = new Timer(messageTimeout, new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    statusMessageLabel.setText("");
    }
    });
    messageTimer.setRepeats(false);
    int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
    for (int i = 0; i < busyIcons.length; i++) {
    busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
    }
    busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
    statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
    }
    });
    idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
    statusAnimationLabel.setIcon(idleIcon);
    progressBar.setVisible(false);

    // connecting action tasks to status bar via TaskMonitor
    TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
    taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
    public void propertyChange(java.beans.PropertyChangeEvent evt) {
    String propertyName = evt.getPropertyName();
    if ("started".equals(propertyName)) {
    if (!busyIconTimer.isRunning()) {
    statusAnimationLabel.setIcon(busyIcons[0]);
    busyIconIndex = 0;
    busyIconTimer.start();
    }
    progressBar.setVisible(true);
    progressBar.setIndeterminate(true);
    } else if ("done".equals(propertyName)) {
    busyIconTimer.stop();
    statusAnimationLabel.setIcon(idleIcon);
    progressBar.setVisible(false);
    progressBar.setValue(0);
    } else if ("message".equals(propertyName)) {
    String text = (String)(evt.getNewValue());
    statusMessageLabel.setText((text == null) ? "" : text);
    messageTimer.restart();
    } else if ("progress".equals(propertyName)) {
    int value = (Integer)(evt.getNewValue());
    progressBar.setVisible(true);
    progressBar.setIndeterminate(false);
    progressBar.setValue(value);
    }
    }
    });
    }

    @Action
    public void showAboutBox() {
    if (aboutBox == null) {
    JFrame mainFrame = TestDynamicTextFieldsApp.getApplication().getMainFrame();
    aboutBox = new TestDynamicTextFieldsAboutBox(mainFrame);
    aboutBox.setLocationRelativeTo(mainFrame);
    }
    TestDynamicTextFieldsApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    mainPanel = new javax.swing.JPanel();
    jTabbedPane1 = new javax.swing.JTabbedPane();
    jPanel1 = new javax.swing.JPanel();
    jPanel2 = new javax.swing.JPanel();
    jButton1 = new javax.swing.JButton();
    menuBar = new javax.swing.JMenuBar();
    javax.swing.JMenu fileMenu = new javax.swing.JMenu();
    javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
    javax.swing.JMenu helpMenu = new javax.swing.JMenu();
    javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
    statusPanel = new javax.swing.JPanel();
    javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
    statusMessageLabel = new javax.swing.JLabel();
    statusAnimationLabel = new javax.swing.JLabel();
    progressBar = new javax.swing.JProgressBar();

    mainPanel.setName("mainPanel"); // NOI18N

    jTabbedPane1.setName("jTabbedPane1"); // NOI18N

    jPanel1.setName("jPanel1"); // NOI18N

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGap(0, 687, Short.MAX_VALUE)
    );
    jPanel1Layout.setVerticalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGap(0, 223, Short.MAX_VALUE)
    );

    org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(testdynamictextfields.TestDynamicTextFieldsApp.class).getContext().getResourceMap(TestDynamicTextFieldsView.class);
    jTabbedPane1.addTab(resourceMap.getString("jPanel1.TabConstraints.tabTitle"), jPanel1); // NOI18N

    jPanel2.setName("jPanel2"); // NOI18N

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
    jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGap(0, 687, Short.MAX_VALUE)
    );
    jPanel2Layout.setVerticalGroup(
    jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGap(0, 223, Short.MAX_VALUE)
    );

    jTabbedPane1.addTab("tab2", jPanel2);

    jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
    jButton1.setName("jButton1"); // NOI18N
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton1ActionPerformed(evt);
    }
    });

    javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
    mainPanel.setLayout(mainPanelLayout);
    mainPanelLayout.setHorizontalGroup(
    mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(mainPanelLayout.createSequentialGroup()
    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(mainPanelLayout.createSequentialGroup()
    .addContainerGap()
    .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 692, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGroup(mainPanelLayout.createSequentialGroup()
    .addGap(37, 37, 37)
    .addComponent(jButton1)))
    .addContainerGap(18, Short.MAX_VALUE))
    );
    mainPanelLayout.setVerticalGroup(
    mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
    .addGap(33, 33, 33)
    .addComponent(jButton1)
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 138, Short.MAX_VALUE)
    .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap())
    );

    menuBar.setName("menuBar"); // NOI18N

    fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
    fileMenu.setName("fileMenu"); // NOI18N

    javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(testdynamictextfields.TestDynamicTextFieldsApp.class).getContext().getActionMap(TestDynamicTextFieldsView.class, this);
    exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
    exitMenuItem.setName("exitMenuItem"); // NOI18N
    fileMenu.add(exitMenuItem);

    menuBar.add(fileMenu);

    helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
    helpMenu.setName("helpMenu"); // NOI18N

    aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
    aboutMenuItem.setName("aboutMenuItem"); // NOI18N
    helpMenu.add(aboutMenuItem);

    menuBar.add(helpMenu);

    statusPanel.setName("statusPanel"); // NOI18N

    statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

    statusMessageLabel.setName("statusMessageLabel"); // NOI18N

    statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
    statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

    progressBar.setName("progressBar"); // NOI18N

    javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
    statusPanel.setLayout(statusPanelLayout);
    statusPanelLayout.setHorizontalGroup(
    statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 720, Short.MAX_VALUE)
    .addGroup(statusPanelLayout.createSequentialGroup()
    .addContainerGap()
    .addComponent(statusMessageLabel)
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 550, Short.MAX_VALUE)
    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
    .addComponent(statusAnimationLabel)
    .addContainerGap())
    );
    statusPanelLayout.setVerticalGroup(
    statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(statusPanelLayout.createSequentialGroup()
    .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(statusMessageLabel)
    .addComponent(statusAnimationLabel)
    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(3, 3, 3))
    );

    setComponent(mainPanel);
    setMenuBar(menuBar);
    setStatusBar(statusPanel);
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    txtFields = new JTextField[5];

    for(int i = 0; i < 5; i++){
    txtFields[i] = new JTextField(i);
    jPanel1.add(txtFields[i]);
    txtFields[i].setLocation(25, i * 5);
    txtFields[i].setText("Test");
    txtFields[i].validate();
    }
    jPanel1.validate();
    jTabbedPane1.validate();
    mainPanel.validate();
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;
    private JTextField txtFields[];

    private JDialog aboutBox;
    }

  8. #8
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Trying to add JTextField to JTabbed Panel

    Quote Originally Posted by Bear35805 View Post
    I apologize. Since I had put the entire method in my original post, I mistakenly assumed you needed the entire project.
    Unfortunately you only put the method contents last time - there was no way to tell where the code came from or what the declarations were.

    Here is just the source code from a project that I am using to test the concept. The code that adds the jTextFields resides in jButton1ActionPerformed method.
    Thanks. Please post code using the CODE tags, so it stays formatted.

    I extracted the relevant code and ran it locally, and it works if the jPanel1 layout stuff is removed, so there's something wrong with your layout code.

    Everything should be made as simple as possible, but not simpler...
    A. Einstein
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  9. #9
    Join Date
    Oct 2009
    Posts
    10

    Unhappy Re: Trying to add JTextField to JTabbed Panel

    If you are talking about the code inside initComponents method, that code was generated by the Form Editor and it will not let me remove it.

  10. #10
    Join Date
    Oct 2009
    Posts
    10

    Thumbs up Re: Trying to add JTextField to JTabbed Panel

    Thanks for all you help and patience. Instead of using the Form Editor to add the tabs and the Panels to the tabbedPane, I wrote the code to do it manually. It now works.

    For future reference what is the syntax for the "Code" tags you mentioned?

  11. #11
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Trying to add JTextField to JTabbed Panel

    Quote Originally Posted by Bear35805 View Post
    Thanks for all you help and patience. Instead of using the Form Editor to add the tabs and the Panels to the tabbedPane, I wrote the code to do it manually. It now works.
    Good This is one of the reasons many Java programmers don't use the clever form generators - they tend to generate horrible code, and stop you changing it. They're fine for prototyping, and I have seen the odd decent one, but it usually pays in the long run to do it by hand.

    For future reference what is the syntax for the "Code" tags you mentioned?
    See my sig.

    Out of clutter, find simplicity. From discord, find harmony. In the middle of difficulty, lies opportunity...
    A. Einstein
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured