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
dlorde
October 20th, 2009, 05:01 PM
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
Bear35805
October 20th, 2009, 05:10 PM
I just added the validate() and it still didn't work.
dlorde
October 20th, 2009, 06:25 PM
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
Bear35805
October 21st, 2009, 09:03 AM
Here is the entire project.
Thanks.
dlorde
October 21st, 2009, 01:33 PM
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 (http://java.sun.com/docs/books/tutorial/uiswing/components/tabbedpane.html) tutorial.
There is nothing so useless as doing efficiently that which should not be done at all...
P. Drucker
Bear35805
October 21st, 2009, 01:57 PM
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.
/**
* 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();
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;
}
dlorde
October 21st, 2009, 03:10 PM
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
Bear35805
October 21st, 2009, 03:20 PM
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.
Bear35805
October 21st, 2009, 03:52 PM
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?
dlorde
October 21st, 2009, 04:16 PM
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 :thumb: 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
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.