我正在测试表模型和渲染。但是我在调整列宽方面遇到了一些问题,我无法调整列宽,因为它是内容文本。我使用了JWindow并隐藏了JFrame。我想调整这个表的列宽,因为我喜欢........ 但我不能使用这段代码.......
for(int i=0;i<header.length;i++){
TableColumn col=table.getColumnModel().getColumn(i);
if(i==0)
col.setWidth(106);
if(i==1)
col.setWidth(157);
if(i==2)
col.setWidth(103);
if(i==3)
col.setWidth(103);
}
这是我的全部代码....检查一下......请............
import java.awt.AWTEvent;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.SQLException;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JWindow;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
public class ReportDamage extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private static JFrame f = new JFrame();
private static JTable table = new JTable();
private String[] header = { "Damage_Id", "Supplier_Stock_Id", "Quantity",
"Date" };
private String[][] data = null;
private DefaultTableModel table_model = new TableModel();
private static JLabel move;
private JScrollPane scroll = new JScrollPane(table);
private static JButton ok = new JButton("OK");
private JPanel bpanel=new JPanel();
static Window container = f;
static JWindow window = new JWindow(container);
private JPanel bar=new JPanel(new GridBagLayout());
private JLabel title=new JLabel("Damage Table");
static Point windowp, mouse, movep;
private boolean flag=false;
public ReportDamage() throws SQLException {
super(new GridBagLayout());
table.setModel(table_model);
table.setRowHeight(25);
table.getTableHeader().setFont(
new Font("Times New Roman", Font.BOLD, 16));
table.getTableHeader().setBackground(new Color(24, 36, 233));
table.getTableHeader().setForeground(new Color(255, 255, 255));
//table.getTableHeader().setEnabled(false);
table.setFont(new Font("Zawgyi-One", Font.PLAIN, 14));
table.setDefaultRenderer(Object.class,
(TableCellRenderer) new Renderer());
for(int i=0;i<header.length;i++){
TableColumn col=table.getColumnModel().getColumn(i);
if(i==0)
col.setWidth(106);
if(i==1)
col.setWidth(157);
if(i==2)
col.setWidth(103);
if(i==3)
col.setWidth(103);
}
scroll.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(Color.white,3),
BorderFactory.createEmptyBorder(5, 5, 5, 5)));
scroll.setBackground(Color.white);
Toolkit.getDefaultToolkit().addAWTEventListener(
new listener(),
AWTEvent.MOUSE_MOTION_EVENT_MASK
| AWTEvent.MOUSE_EVENT_MASK);
move = new JLabel("+");
move.setFont(new Font("Times New Roman", Font.BOLD, 19));
move.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
flag=false;
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
flag=true;
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
});
ok.setForeground(Color.black);
ok.setFont(new Font("Times New Roman", Font.BOLD, 17));
ok.setBackground(new Color(33, 33, 250));
ok.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createRaisedBevelBorder(),
BorderFactory.createEmptyBorder(5, 18, 5, 18)));
ok.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
f.dispose();
for(int i=0;i<header.length;i++){
TableColumn col=table.getColumnModel().getColumn(i);
System.out.println(col.getWidth()+ " width of col "+ i);
}
}
});
ok.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
ok.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createRaisedBevelBorder(),
BorderFactory.createEmptyBorder(5, 18, 5, 18)));
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
ok.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createBevelBorder(1),
BorderFactory.createEmptyBorder(5, 18, 5, 18)));
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
ok.setForeground(Color.black);
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
ok.setForeground(Color.white);
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
});
GridBagLayout gbb = (GridBagLayout) getLayout();
GridBagConstraints cb = new GridBagConstraints();
cb.fill=0;cb.gridwidth=1;
gbb.setConstraints(move, cb);
add(move);
gbb.setConstraints(title, cb);
add(title);
title.setFont(new Font("Times New Roman",Font.BOLD,18));
//fillTable();
GridBagLayout gb = (GridBagLayout) getLayout();
GridBagConstraints c = new GridBagConstraints();
bpanel.add(ok);
c.fill = 0;
c.gridwidth = 0;
gb.setConstraints(bar, c);
add(bar);
gb.setConstraints(scroll, c);
add(scroll);
gb.setConstraints(bpanel,c);
add(bpanel);
title.setForeground(Color.white);
title.setBorder(BorderFactory.createEmptyBorder(4,9,15,0));
move.setBorder(BorderFactory.createEmptyBorder(-5,0,35,15));
move.setForeground(Color.white);
bpanel.setBackground(Color.black);
ok.setEnabled(true);
setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.gray,3),BorderFactory.createBevelBorder(1)));
setBackground(Color.black);
}
private void fillTable() throws SQLException {
// TODO Auto-generated method stub
Statement st = (Statement) sqlCloseOrOpen.connect().createStatement();
ResultSet rs = (ResultSet) st.executeQuery("Select * from damage");
DefaultTableModel tm = (DefaultTableModel) table.getModel();
String[] array = new String[4];
while (rs.next()) {
array[0] = rs.getInt(1) + "";
array[1] = rs.getInt(2) + "";
array[2] = rs.getInt(3) + "";
array[3] = rs.getString(4);
tm.addRow(array);
}
}
public static void showGUI() throws SQLException {
window.getContentPane().removeAll();
window.getContentPane().add(new ReportDamage());
// f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
container.pack();
// f.setContentPane(new ReportDamage());
window.setOpacity(.8f);
window.setVisible(true);
window.setLocation(140, 150);
window.pack();
}
public static void main(String args[]) throws SQLException {
showGUI();
}
public class Renderer extends DefaultTableCellRenderer {
/**
*
*/
private static final long serialVersionUID = 1L;
Renderer() {
super();
setOpaque(true);
setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
}
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
int r = table.getSelectedRow();
if (row == r) {
setBackground(new Color(110, 40, 40));
setForeground(new Color(255, 255, 255));
setFont(new Font("Times New Roman", Font.BOLD, 16));
} else {
setForeground(Color.black);
setFont(new Font("Zawgyi-One", Font.PLAIN, 14));
if (row % 2 == 0) {
setBackground(new Color(223, 223, 223));
} else {
setBackground(new Color(255, 255, 255));
}
}
setHorizontalAlignment(CENTER);
return this;
}
}
public class listener implements AWTEventListener {
@Override
public void eventDispatched(AWTEvent arg0) {
// TODO Auto-generated method stub
if (arg0.paramString().startsWith("MOUSE_DRAGGED")&& flag==true) {
windowp = window.getLocation();
movep = move.getLocation();
mouse = MouseInfo.getPointerInfo().getLocation();
window.setLocation((mouse.x - movep.x) - 5,
(mouse.y - movep.y) - 10);
}
}
}
public class TableModel extends DefaultTableModel{
TableModel(){
super(data, header);
}
public boolean isCellEditable(int row ,int column){
return false;
}
}
}
`