java web 怎么在jtable中添加按钮?

java web 怎么在jtable中添加按钮?,第1张

java web在jtable中添加按钮的示例如下:

import java.awt.BorderLayout

import java.awt.Color

import java.awt.Component

import java.awt.Dimension

import java.awt.event.MouseAdapter

import java.awt.event.MouseEvent

import java.io.File

import javax.swing.BorderFactory

import javax.swing.ImageIcon

import javax.swing.JButton

import javax.swing.JFrame

import javax.swing.JPanel

import javax.swing.JScrollPane

import javax.swing.JTable

import javax.swing.UIManager

import javax.swing.border.Border

import javax.swing.border.EmptyBorder

import javax.swing.table.AbstractTableModel

import javax.swing.table.TableCellRenderer

public class JTableButton extends JPanel {

 private JTable table

 private JScrollPane scrollPane

 private JButton[] buttons

 private String path = System.getProperty("user.dir") + File.separator

 + "images" + File.separator

 

 public JTableButton() {

  setBorder(BorderFactory.createLineBorder(Color.red, 1))

  init()

 }

 private void init() {

  String headName[] = { "Name", "age", "sex", "adress", "image" }

  

  buttons = new JButton[5]

  for(int i=0i<buttons.lengthi++){

   buttons[i] = new JButton(""+i)

  }

  Object obj[][] = {

    { "LiMing", 23, Boolean.TRUE, buttons[0],

      new ImageIcon(path + "icon.png") },

    { "ZhangSan", 25, Boolean.TRUE,buttons[1],

      new ImageIcon(path + "icon.png") },

    { "WangWu", 21, Boolean.FALSE, buttons[2],

      new ImageIcon(path + "icon.png") },

    { "LiSi", 28, Boolean.TRUE, buttons[3],

      new ImageIcon(path + "icon.png") },

    { "LuBo", 20, Boolean.FALSE, buttons[4],

      new ImageIcon(path + "icon.png") }, }

  

  table = new JTable(new MyTableModel(headName,obj))

  table.setDefaultRenderer(JButton.class, new ComboBoxCellRenderer())

  scrollPane = new JScrollPane(table)

  setLayout(new BorderLayout())

  add(scrollPane, BorderLayout.CENTER)

  addHandler()

 }

 private void addHandler(){

  //添加事件

  table.addMouseListener(new MouseAdapter(){

   public void mouseClicked(MouseEvent e) {

    System.out.println("table")

    int row = table.getSelectedRow()

    int column = table.getSelectedColumn()

    System.out.println("row="+row+":"+"column="+column)

    if(column==3){

     //处理button事件写在这里...

     System.out.println(((JButton)table.getValueAt(row, column)).getText())

    }

   }

  })

 }

 public static void main(String[] args) {

  JFrame frame = new JFrame()

  frame.add(new JTableButton())

  frame.setSize(new Dimension(800, 400))

  frame.setVisible(true)

  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

 }

 class MyTableModel extends AbstractTableModel {

  private String headName[]

  private Object obj[][]

  

  public MyTableModel() {

   super()

  }

  

  public MyTableModel(String[] headName, Object[][] obj) {

   this()

   this.headName = headName

   this.obj = obj

  }

  public int getColumnCount() {

   return headName.length

  }

  public int getRowCount() {

   return obj.length

  }

  public Object getValueAt(int r, int c) {

   return obj[r][c]

  }

  public String getColumnName(int c) {

   return headName[c]

  }

  public Class<?> getColumnClass(int columnIndex) {

   return obj[0][columnIndex].getClass()

  }

  @Override

  public boolean isCellEditable(int rowIndex, int columnIndex) {

   if (columnIndex == 3 || columnIndex == 4) {

    return false

   }

   return true

  }

 }

}

class ComboBoxCellRenderer implements TableCellRenderer {

 public Component getTableCellRendererComponent(JTable table, Object value,

   boolean isSelected, boolean hasFocus, int row, int column) {

  JButton cmb = (JButton) value

  if (isSelected) {

   cmb.setForeground(table.getSelectionForeground())

   cmb.setBackground(table.getSelectionBackground())

  } else {

   cmb

     .setForeground((unselectedForeground != null) ? unselectedForeground

       : table.getForeground())

   cmb

     .setBackground((unselectedBackground != null) ? unselectedBackground

       : table.getBackground())

  }

  cmb.setFont(table.getFont())

  if (hasFocus) {

   cmb

     .setBorder(UIManager

       .getBorder("Table.focusCellHighlightBorder"))

   if (!isSelected && table.isCellEditable(row, column)) {

    Color col

    col = UIManager.getColor("Table.focusCellForeground")

    if (col != null) {

     cmb.setForeground(col)

    }

    col = UIManager.getColor("Table.focusCellBackground")

    if (col != null) {

     cmb.setBackground(col)

    }

   }

  } else {

   cmb.setBorder(noFocusBorder)

  }

  return cmb

 }

 protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1)

 private Color unselectedForeground

 private Color unselectedBackground

}

1、打开Sublime text3,新建一个HTML文档,并且建立好框架。

2、输入代码:

<table>

      <tr>

          <td>Student</td>

          <td><input type="text" name="student"></td>

      </tr>

      <tr>

          <td>Height</td>

          <td><input type="text" name="height"></td>

      </tr>

      <tr>

          <td colspan=2>

              <input type="button" name="add" value="add">

          </td>

      </tr>

  </table>

设立两行文本输入框,并且提示可以增加的按钮。

3、<table id="mainTable">

为标签加上id方便定位。

4、加上监听事件的函数。

onclick="adding()"

然后连接js文档。

<script src="test.js"></script>

5、function adding(){

  var table = document.getElementById("mainTable")

  var addTr = table.insertRow(2)

}

创建函数,然后设定两个变量。一个用于定位标签位置,另一个增加行数在第二行。测试一下,多次点击确实会往下移动。

6、var addTd = addTr.insertCell()

  addTd.innerHTML = "新增"

除了要增加tr还要增加td,并且增加文本提示。

7、稍微修改一下变量名字。再增加文本输入框。

function adding(){

  var table = document.getElementById("mainTable")

  var addTr = table.insertRow(2)

  var td1 = addTr.insertCell()

  td1.innerHTML = "新增"

  var td2 = addTr.insertCell()

  td2.innerHTML = "<input type='text 'name='newnew' >"

}

8、最后就可以看到按钮了。

前台用Ajax,后台可以用PHP、JSP等

前台:JavaScrip部分: var btn=document.getElementById("btn")

btn.addEventListener("click",function(){

var xhr=new XMLHttpRequest()

xhr.open("POST","index.php",true)

var data=xxxx//你要传入后台的数据

xhr.setRequestHeader("content-Type","application/x-www-form-urlencoded")

xhr.send(data)

},false)后台:<?php

$data=$_POST[]

var con=mysql_connect($host,$user,$pass)

mysql_select_db("")

mysql_query("set names utf8")

mysql_query("insert into table(id,name,class) values(12,"何怡",2)";//你对数据库进行的 *** 作

mysql_close($con)

?>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11795122.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存