views:

240

answers:

3

Hello all,

Extremely new to Java an just playing around with it. I'm trying to add text fields to below a table and for some reason I can't see them. Code is below:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class danTeamProject extends JApplet implements ActionListener
{

danTable aTable;
//private JLabel rowLabel, colLabel;

private JTextField rowNum;
private JTextField colNum;


public void init()  {



    JButton btnStart = new JButton("Start");
    this.add(btnStart);
    aTable = new danTable();
    this.add( aTable );

    //rowLabel = new JLabel( "Enter number of rows:" );
    rowNum = new JTextField( 1 );

    //colLabel = new JLabel( "Enter number of columns:" );
    colNum = new JTextField( 1 );

    //this.add (rowLabel);
    this.add (rowNum);

    //this.add (colLabel);
    this.add (colNum);

    rowNum.addActionListener(this);
    colNum.addActionListener(this);


}

public void actionPerformed(ActionEvent event) {
    String s = rowNum.getText();
    String sUp = s.toUpperCase();
    rowNum.setText(sUp);
}

}





import javax.swing.*;

class danTable extends JPanel {
public danTable() {
    Object[][] cellData = {
        {"row1-col1", "row1-col2"},
        {"row2-col1", "row2-col2"}};
    String[] columnNames = {"col1", "col2"};
    add(  new JTable(cellData, columnNames) ) ;
   }
}




`<!DOCTYPE HTML>
<HTML>
<HEAD>
</HEAD>
<BODY>
  <div>
    <APPLET
      CODE="danTeamProject"
      CODEBASE="."
      width=900 height=900>
      <PARAM name="boxbgcolor" value="cyan">
    </APPLET>
  </div>
</BODY>
</HTML>`

Any ideas where the problem is coming from? All the other post I've looked at recommend checking the applet size, but 900x900 should hold everything, right? Thanks in advance...

Also, I know I recently posted a question about this same program. This is simply the best forum to get fast, reliable information...

A: 

Which layouter are you using? As I see it, you're using the default layout which allows only a single child (i.e. you can add more but they will be ignored).

Try to add MiG Layout to the applet, then the other children should show up.

Aaron Digulla
I assumed it was a flow layout which would fit the all the components in the window. I'd really like a non-third party solution, but I'll definitely check out MiG. Thanks
danwoods
Changed it to BorderLayout and still can't see the textfields...
danwoods
+1  A: 

You cannot mix Applet and Swing Components:

For instance you cannot place a JTextField on an Applet contentpane. If you are extending to an Applet not JApplet use TextField instead of JTextField.

Button, Label, TextField, Panel  are for Applet
JButton, JLabel, JTextField, JPanel are for JApplet

Please take note of the prefix.

Since you are using most of the Swing Components, extend JApplet not Applet

public class danTeamProject extends JApplet implements ActionListener
{    ...
     JButton button = new JButton();
}
jerjer
Thanks. Switched everything over to JApplet and but no luck...
danwoods
One more thing though, after you have compile your applet, make sure that you have emptied browser's temporary internet files as well as that of the java(control-panel->java icon).
jerjer
+1  A: 

The default layout for JApplet is BorderLayout. Therefore, try adding your components using the permitted BorderLayout constraints; e.g.

this.add(rowNum, BorderLayout.NORTH);

If BorderLayout is too inflexible for you and you don't want to rely on a third party layout manager I suggest either looking at GridBagLayout or else composing your applet from a number of nested JPanels.

Another suggestion: You might want to move your GUI initialisation code from init() to the JApplet constructor. This allows you to make your JTextFields final; I typically use init() to start any threads the application uses.

Adamski
changed code to: this.add (rowNum, BorderLayout.EAST); and this.add (colNum, BorderLayout.WEST); And still no luck. Thanks for the input, though
danwoods
Try adding a call to revalidate() at the end of your init() method.
Adamski