Reputation: 4907
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...
Upvotes: 0
Views: 1224
Reputation: 54725
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 JPanel
s.
Another suggestion: You might want to move your GUI initialisation code from init()
to the JApplet
constructor. This allows you to make your JTextField
s final; I typically use init() to start any threads the application uses.
Upvotes: 1
Reputation: 8766
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();
}
Upvotes: 1
Reputation: 328780
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.
Upvotes: 0