JAVAswing问题,大神来解决下,详细点,有图
如果转正时间等于目前系统时间,那么等于这条转正记录就为红色背景,并且显示在最前方,这个应该怎么实现?我的javaswing,...
如果转正时间等于目前系统时间,那么等于这条转正记录就为红色背景,并且显示在最前方,这个应该怎么实现?我的java swing,
展开
1个回答
展开全部
package tool;
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.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 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 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);
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);
}
}
更多追问追答
追问
大神很厉害啊,但是作为新手,我还是不知道怎么弄,看看我的代码这要怎么弄么?
能留个Qq联系么,帮我解决下,非常感谢
追答
我毛有Q
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询