本文目录一览:
Java如何做动态表格?
一、动态加载表格
1.首先在html中为表格的添加位置设置id
即是在html的body标签内部写一个div标签表明表格要添加到此div的内部。如下
div id="tdl"div
2.在javascript中写添加表格的语句
若在当前html文件中,则写在script标签内部,如
代码如下:
script type="text/javascript"
document.getElementById("tbl").innerHTML="tabletrtd/td/tr/table" //此处添加的表格可根据自己需要创建
/script
若是通过引入js文件,则在js文件(假设是test.js)中直接写如下语句
代码如下:
document.getElementById("tbl").innerHTML="tabletrtd/td/tr/table"
然后再引入自己的html文件
代码如下:
script type="text/javascript" src="test.js"/script
二、 动态添加表格行
1.首先在html中为表格行的添加位置设置id,此位置必须是tbody内部(不是特别准确,但根据我的测试就得到此结论,有其他的方法请留言,谢谢),如下
代码如下:
table
thead/thead
tfoottfoot //tfoot与thead是与tbody配套使用,但我在写的时候,没用也可以。
tbody id="rows"/tbody
/table
[\s\S ]*\n
2.在javascript内容中,要先创建行和单元格,再在.tbody中添加行,如下
[code]
row=document.createElement("tr"); //创建行
td1=document.createElement("tr"); //创建单元格
td1.appendChild(document.createTextNode("content")); //为单元格添加内容
row.appendChild(td1); //将单元格添加到行内
document.getElementById("rows").append(row); //将行添加到tbody中
java 的表格模型是什么?
DefaultTableModel是常用的java表格模型。
参考代码:
package com.dusd;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Arrays;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumnModel;
/**
* 表格模型事件示例
* p
* liA component generally gains the focus when the user clicks it,
* or when the user tabs between components, or otherwise interacts
* with a component. A component can also be given the focus programmatically,
* such as when its containing frame or dialog-box is made visible.
* The snippet of the codes below shows how to give a particular component
* the focus every time the window gains the focus.
*
* @author HAN
*
*/
public class TableModel extends JFrame {
/**
*
*/
private static final long serialVersionUID = -8581492063632813033L;
public TableModel() {
// TODO Auto-generated constructor stub
final Container container = getContentPane();
VectorString tableColumnNames = new VectorString();
tableColumnNames.add("A");
tableColumnNames.add("B");
VectorVectorString tableValues = new VectorVectorString();
for (int i = 1; i 5; i++) {
VectorString vector = new VectorString();
vector.add("A" + i);
vector.add("B" + i);
tableValues.add(vector);
}
final DefaultTableModel defaultTableModel = new DefaultTableModel(
tableValues, tableColumnNames);
final JTable table = new JTable(defaultTableModel);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(table);
container.add(scrollPane, BorderLayout.CENTER);
JPanel panel = new JPanel();
container.add(panel, BorderLayout.SOUTH);
JLabel labelA = new JLabel("A: ");
final JTextField textFieldA = new JTextField(15);
JLabel labelB = new JLabel("B: ");
final JTextField textFieldB = new JTextField(15);
JButton buttonAdd = new JButton("添加");
JButton buttonDel = new JButton("删除");
JButton buttonDeselected = new JButton("取消选择");
panel.add(labelA);
panel.add(textFieldA);
panel.add(labelB);
panel.add(textFieldB);
panel.add(buttonAdd);
panel.add(buttonDel);
panel.add(buttonDeselected);
buttonAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int[] selectedRows = table.getSelectedRows(); // table
// 默认情况容许多行选择
VectorString rowData = new VectorString();
rowData.add(textFieldA.getText());
rowData.add(textFieldB.getText());
if (selectedRows.length == 0) {
defaultTableModel.addRow(rowData);
textFieldA.setText(null);
textFieldB.setText(null);
} else if (selectedRows.length == 1) {
// System.out.println(selectedRows[0]);
defaultTableModel.insertRow(selectedRows[0] + 1, rowData);
textFieldA.setText(null);
textFieldB.setText(null);
} else {
JOptionPane.showMessageDialog(container,
"Your operation is forbidden", "Warning",
JOptionPane.WARNING_MESSAGE);
}
}
});
buttonDel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int[] selectedRows = table.getSelectedRows(); // table
// 默认情况容许多行选择
for (int i = 0; i selectedRows.length; i++) {
// System.out.println(selectedRows[i]);
defaultTableModel.removeRow(selectedRows[i] - i);
}
}
});
buttonDeselected.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
table.clearSelection();
}
});
scrollPane.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// System.out.println("here");
if (e.getClickCount() == 1
e.getButton() == MouseEvent.BUTTON1) {
table.clearSelection();
}
}
});
// make the text field focused every time the window is activated
addWindowFocusListener(new WindowAdapter() {
@Override
public void windowGainedFocus(WindowEvent e) {
// TODO Auto-generated method stub
textFieldA.requestFocus();
}
});
// **************************************************************
// This is a standard snippet to realize the desired column selection as in
// Excel
// **************************************************************
// row selection mode
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
// column selection mode
TableColumnModel tableColumnModel = table.getColumnModel();
tableColumnModel.getSelectionModel().setSelectionMode(
ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
// allow the column selection (the row selection is allowed by default)
table.setColumnSelectionAllowed(true);
final JTableHeader tableHeader = table.getTableHeader();
tableHeader.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
// Extended modifiers represent the state of all modal keys,
// such as ALT, CTRL, META.
if (e.getClickCount() == 1
e.getButton() == MouseEvent.BUTTON1) {
// Point point = new Point(e.getX(), e.getY());
Point point = new Point(e.getPoint());
int columnNum = tableHeader.columnAtPoint(point);
// System.out.println(columnNum);
int[] selectedColumns = table.getSelectedColumns();
if (selectedColumns.length != 0) {
// System.out.println("here1");
// System.out.println(InputEvent.getModifiersExText(e.getModifiersEx()));
if (e.getModifiersEx() == (InputEvent.CTRL_DOWN_MASK)) {
// System.out.println("ctrl");
if (Arrays.binarySearch(selectedColumns, columnNum) = 0) {
table.removeColumnSelectionInterval(columnNum,
columnNum);
} else {
table.addColumnSelectionInterval(columnNum,
columnNum);
}
} else if (e.getModifiersEx() == (InputEvent.SHIFT_DOWN_MASK)) {
// System.out.println("shift");
table.setColumnSelectionInterval(
selectedColumns[0], columnNum);
} else {
table.setColumnSelectionInterval(columnNum,
columnNum);
}
} else {
// System.out.println("here2");
table.setColumnSelectionInterval(columnNum, columnNum);
}
table.setRowSelectionInterval(0, table.getRowCount() - 1);
}
}
});
defaultTableModel.addTableModelListener(new TableModelListener() {
public void tableChanged(TableModelEvent e) {
// TODO Auto-generated method stub
int type = e.getType();
int firstRow = e.getFirstRow();
// int lastRow = e.getLastRow(); // the last row seems to be always equal to the first row
int column = e.getColumn();
switch (type) {
case TableModelEvent.DELETE:
System.out.print("此次事件由 删除 行触发:");
System.out.println("此次删除的是第 " + firstRow + " 行");
break;
case TableModelEvent.INSERT:
System.out.print("此次事件由 插入 行触发:");
System.out.println("此次插入的是第 " + firstRow + " 行");
break;
case TableModelEvent.UPDATE:
System.out.print("此次事件由 更新 行触发:");
System.out.println("此次更新的是第 " + firstRow + " 行第 " + column + " 列");
break;
default:
System.out.println("此次事件由 其他原因 触发");
}
}
});
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TableModel frame = new TableModel();
frame.setTitle("表格模型事件示例");
frame.pack(); //Realize the components.
// frame.setBounds(100, 100, 600, 300);
// textFieldA.requestFocus();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true); //Display the window.
}
}
java表格代码怎么写
java表格就是java swing。
//创建表头
String[] columnNames = { "First Name", "Last Name", "Sport",
"# of Years", "Vegetarian" };
//创建显示数据
Object[][] data = {
{ "Kathy", "Smith", "Snowboarding", new Integer(5),
new Boolean(false) },
{ "John", "Doe", "Rowing", new Integer(3), new Boolean(true) },
{ "Sue", "Black", "Knitting", new Integer(2),
new Boolean(false) },
{ "Jane", "White", "Speed reading", new Integer(20),
new Boolean(true) },
{ "Joe", "Brown", "Pool", new Integer(10), new Boolean(false) } };
/*
* JTable还提供了一个重载的构造方法,传入两个Vector
* JTable(Vector rowData, Vector columnNames)
*
*/
final JTable table = new JTable(data, columnNames);
table.setBackground(Color.YELLOW);