重写swing UI
http://stackoverflow.com/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.Vector;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;
import javax.swing.border.MatteBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
public class JTableMouseMovingEvent extends JFrame
{
/**
*
*/
private static final long serialVersionUID = 1L;
/** */
private JTable table;
/** */
private String[] columnName = new String[] {"title1", "name", "age"};
/** */
private DefaultTableModel model = null;
/** */
private int rowCount = -1;
/**
* Launch the application
*
* @param args
*/
public static void main(String args[])
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
JTableMouseMovingEvent frame = new JTableMouseMovingEvent();
frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public JTableMouseMovingEvent()
{
super();
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
getContentPane().add(panel, BorderLayout.CENTER);
final JScrollPane scrollPane = new JScrollPane();
panel.add(scrollPane, BorderLayout.CENTER);
table = new JTable();
//table.setBorder(new MatteBorder(2, 2, 2, 2, Color.GREEN));
table.setGridColor(Color.DARK_GRAY);
table.setRowHeight(30);
// table.setShowGrid(false);
// table.setShowVerticalLines(false);
// table.setShowHorizontalLines(false);
//table.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
table.addMouseMotionListener(new MouseMotionAdapter()
{
@Override
public void mouseMoved(final MouseEvent e)
{
rowCount = table.rowAtPoint(e.getPoint());
int row = table.rowAtPoint(e.getPoint());
int col = table.columnAtPoint(e.getPoint());
table.setRowSelectionInterval(row, row);
table.setColumnSelectionInterval(col, col);
//table.setForeground(new java.awt.Color(0, 255, 51));
//table.setSelectionBackground(Color.red);//设置选择行的颜色
}
});
table.addMouseListener(new java.awt.event.MouseAdapter()
{
@Override
public void mouseReleased(java.awt.event.MouseEvent e)
{
if (e.getClickCount() == 1 && SwingUtilities.isRightMouseButton(e))
{
int row = table.rowAtPoint(e.getPoint());
int col = table.columnAtPoint(e.getPoint());
table.setRowSelectionInterval(row, row);
table.setColumnSelectionInterval(col, col);
}
}
});
scrollPane.setViewportView(table);
model = new DefaultTableModel(null, columnName);
Vector<String> row = null;
for (int i = 0; i < 10; i++)
{
row = new Vector<String>();
row.add("" + i);
row.add("table" + i);
row.add("" + (20 + i));
model.addRow(row);
}
table.setModel(model);
table.setDefaultRenderer(Object.class, new TableCellRenderer());
//
}
class TableCellRenderer extends DefaultTableCellRenderer
{
/** */
private static final long serialVersionUID = -5172589582171358319L;
/** {@inheritDoc} */
@Override
public Component getTableCellRendererComponent(JTable jtable, Object value, boolean isSelected,
boolean hasFocus, int row, int column)
{
JComponent c =
(JComponent)super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
JTableHeader head = table.getTableHeader();
if (head != null)
{
head.setBackground(Color.gray);
head.setForeground(Color.white);
head.setPreferredSize(new Dimension(300, 40));
head.setBorder(new SoftBevelBorder(BevelBorder.RAISED));
//head.setFont(new Font("宋体",Font.PLAIN,14));
}
JLabel label =
(JLabel)super.getTableCellRendererComponent(jtable, value, isSelected, hasFocus, row, column);
label.setHorizontalAlignment(SwingConstants.LEFT);
//label.setBorder(new MatteBorder(1, 1, 1, 1, new Color(120,120,120)));
label.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
label.setText(" "+label.getText());
label.setFont(new Font("", Font.PLAIN, 16));
if (row == rowCount)
{
label.setBackground(Color.red);
//label.setFont(new Font("", Font.PLAIN, 15));
//label.setForeground(new java.awt.Color(0, 255, 51));
//label.setBorder(new MatteBorder(2, 2, 2, 2, Color.GREEN));
}
else
{
label.setBackground(new Color(255, 255, 255));
}
return label;
}
}
}