如何使用 Swing 组件 JTable

发布网友 发布时间:2022-04-24 04:00

我来回答

1个回答

热心网友 时间:2023-10-26 12:33

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
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 final String[] NAMES = { "转正时间", "体检时间" };
private final String[][] DATA = { { "2014-11-08", "2013-11-08" }, { "2014-06-03", "2013-11-08" },
{ "2014-01-01", "2013-11-08" }, { "2014-12-02", "2013-11-08" }, { "2014-11-08", "2013-11-08" },
{ "2014-12-02", "2013-11-08" }, { "2014-10-08", "2013-11-08" }, { "2014-12-02", "2013-11-08" },
{ "2014-09-08", "2013-11-08" }, { "2014-11-08", "2013-11-08" } };
private JTable tableView = null;
private SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");

public TableFrame ( String title )
{
order ();
setTitle (title);
}

private void order ()
{
try
{
for ( int i = 0; i < DATA.length; i++ )
{
for ( int j = i + 1; j < DATA.length; j++ )
{
if (sdf.parse (DATA[i][0]).before (sdf.parse (DATA[j][0])))
{
String[] temp = DATA[i];
DATA[i] = DATA[j];
DATA[j] = temp;
}
}
}
}
catch (ParseException e)
{}
}

private void createTable ()
{
TableModel dataModel = new AbstractTableModel ()
{
private static final long serialVersionUID = 1L;

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];
}
};
tableView = new JTable (dataModel);
Date date = new Date ();
final String dt = sdf.format (date);
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 (dt.equals (table.getValueAt (row, 0)))
{
render.setBackground (Color.RED);
}
return render;
}
});
TableRowSorter<TableModel> tableRowSorter = new TableRowSorter<TableModel> (dataModel);
tableView.setRowSorter (tableRowSorter);
JScrollPane scrollpane = new JScrollPane (tableView);
scrollpane.setPreferredSize (new Dimension (500, 200));
setLayout (new BorderLayout ());
add (scrollpane, BorderLayout.CENTER);
JPanel panel = new JPanel ();
panel.setSize (500, 100);
panel.setLayout (new FlowLayout (FlowLayout.CENTER));
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");
tc.createTable ();
initFrame (tc);
}
}

热心网友 时间:2023-10-26 12:33

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
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 final String[] NAMES = { "转正时间", "体检时间" };
private final String[][] DATA = { { "2014-11-08", "2013-11-08" }, { "2014-06-03", "2013-11-08" },
{ "2014-01-01", "2013-11-08" }, { "2014-12-02", "2013-11-08" }, { "2014-11-08", "2013-11-08" },
{ "2014-12-02", "2013-11-08" }, { "2014-10-08", "2013-11-08" }, { "2014-12-02", "2013-11-08" },
{ "2014-09-08", "2013-11-08" }, { "2014-11-08", "2013-11-08" } };
private JTable tableView = null;
private SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");

public TableFrame ( String title )
{
order ();
setTitle (title);
}

private void order ()
{
try
{
for ( int i = 0; i < DATA.length; i++ )
{
for ( int j = i + 1; j < DATA.length; j++ )
{
if (sdf.parse (DATA[i][0]).before (sdf.parse (DATA[j][0])))
{
String[] temp = DATA[i];
DATA[i] = DATA[j];
DATA[j] = temp;
}
}
}
}
catch (ParseException e)
{}
}

private void createTable ()
{
TableModel dataModel = new AbstractTableModel ()
{
private static final long serialVersionUID = 1L;

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];
}
};
tableView = new JTable (dataModel);
Date date = new Date ();
final String dt = sdf.format (date);
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 (dt.equals (table.getValueAt (row, 0)))
{
render.setBackground (Color.RED);
}
return render;
}
});
TableRowSorter<TableModel> tableRowSorter = new TableRowSorter<TableModel> (dataModel);
tableView.setRowSorter (tableRowSorter);
JScrollPane scrollpane = new JScrollPane (tableView);
scrollpane.setPreferredSize (new Dimension (500, 200));
setLayout (new BorderLayout ());
add (scrollpane, BorderLayout.CENTER);
JPanel panel = new JPanel ();
panel.setSize (500, 100);
panel.setLayout (new FlowLayout (FlowLayout.CENTER));
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");
tc.createTable ();
initFrame (tc);
}
}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com