Creating a super simple table applet in java
I'm trying to create an applet which displays a simple table with no headers or other decoration. Could anyone be so kind as to show me the code for this? All the examples I've found haven't compiled or have included extra features which I don't need. A simple 2 x 2 table with empty cells and no headers is what I'm looking for. Thanks to all in advance...
Code for skaffman:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class danTeamProject extends Applet implements ActionListener
{
char[][] charValues = new char[10][10];
danTable aTable;
boolean allowUserInput = false;
public void init()
{
Button BtnStart = new Button("Start");
BtnStart.addActionListener((ActionListener)this); //cast
this.add(BtnStart); //add action listener to button
aTable = new danTable();
aTable.setVisible(true);
}
public void paint(Graphics g)
{
g.set开发者_开发百科Color(Color.black);
aTable.draw(g);
}
public void actionPerformed(ActionEvent arg0)
{
}
}
and
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class danTable extends JPanel
{
public danTable()
{
// Create with initial data
Object[][] cellData = {
{"row1-col1", "row1-col2"},
{"row2-col1", "row2-col2"}};
String[] columnNames = {"col1", "col2"};
JTable table = new JTable(cellData, columnNames);
}
}
I have modified the code you posted.
Read it as many times as needed until you understand what it does. See also the coding conventions ( the brackets and the naming of the variables )
I didn't change too much though, I just make it run.
Pay special attention to the difference between your code and this one ( they are not too much though ) Feel free to ask in case of doubts
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DanTeamProject extends Applet {
char[][] charValues = new char[10][10];
DanTable aTable;
boolean allowUserInput = false;
public void init() {
Button btnStart = new Button("Start");
this.add(btnStart);
aTable = new DanTable();
this.add( aTable );
}
}
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) ) ;
}
}
Here's the HTML used to view it
<applet code="DanTeamProject.class" width=100 height=140></applet>
Here's the output:
Create a JTable and add the table to a JPanel (instead of a JScrollPane) and the header will not appear. Read the JTable API and follow the link to the Swing tutorial on "How to Use Tables" for working examples.
精彩评论