展开全部
package tool;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
public class TableFrame extends JFrame
{
private static final long serialVersionUID = 1L;
private static final int COLNUM = 8;
private static final String[] NAMES = new String[COLNUM];
private static final String[][] DATA = new String[2 * COLNUM][COLNUM];
private static JTable tableView = null;
public TableFrame ( String title )
{
randomTester ();
this.setTitle (title);
}
private void randomTester ()
{
for ( int i = 0; i < NAMES.length; i++ )
{
NAMES[i] = "COL" + ( i + 1 );
}
for ( int i = 0; i < DATA.length; i++ )
{
for ( int j = 0; j < DATA[i].length; j++ )
{
DATA[i][j] = (int) ( Math.random () * 5 + 1 ) + "";
}
}
}
private static void createTable ( final TableFrame tc )
{
TableModel dataModel = new TableModel ()
{
public int getColumnCount ()
{
return NAMES.length;
}
public int getRowCount ()
{
return DATA.length;
}
public Object getValueAt ( int row, int col )
{
return DATA[row][col];
}
public String getColumnName ( int column )
{
return NAMES[column];
}
@Override
public Class<?> getColumnClass ( int columnIndex )
{
return Object.class;
}
@Override
public boolean isCellEditable ( int rowIndex, int columnIndex )
{
return true;
}
@Override
public void setValueAt ( Object aValue, int rowIndex, int columnIndex )
{}
@Override
public void addTableModelListener ( TableModelListener l )
{}
@Override
public void removeTableModelListener ( TableModelListener l )
{}
};
tableView = new JTable (dataModel);
tableView.setDefaultRenderer (Object.class, new TableCellRenderer ()
{
@Override
public Component getTableCellRendererComponent ( JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column )
{
Component render =
new DefaultTableCellRenderer ().getTableCellRendererComponent (table, value, isSelected,
hasFocus, row, column);
if ("1".equals (table.getValueAt (row, NAMES.length - 1)))
{
render.setBackground (Color.RED);
}
return render;
}
});
tableView.setRowSorter (new TableRowSorter<TableModel> (dataModel));
JScrollPane scrollpane = new JScrollPane (tableView);
scrollpane.setPreferredSize (new Dimension (500, 200));
tc.setLayout (new BorderLayout ());
tc.add (scrollpane, BorderLayout.CENTER);
JButton testbtn = new JButton ("test");
testbtn.addActionListener (new ActionListener ()
{
@Override
public void actionPerformed ( ActionEvent e )
{
int row = tableView.getSelectedRow ();
int col = tableView.getSelectedColumn ();
DATA[row][col] = "";
tableView.updateUI ();
}
});
JPanel panel = new JPanel ();
panel.setSize (500, 100);
panel.setLayout (new FlowLayout (FlowLayout.CENTER));
panel.add (testbtn);
tc.add (panel, BorderLayout.SOUTH);
}
private static void initFrame ( TableFrame tc )
{
tc.setSize (512, 300);
tc.setResizable (false);
tc.setLocationRelativeTo (null);
tc.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
tc.setVisible (true);
}
public static void main ( String[] args )
{
TableFrame tc = new TableFrame ("TableFrame");
createTable (tc);
initFrame (tc);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询