我遇到JTable问题。需要你的帮助
这是我的代码
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.table.*;
import javax.swing.border.*;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
import java.io.FileOutputStream;
import java.awt.Graphics2D;
import javax.swing.table.TableColumn;
class tt_show extends JFrame implements ItemListener,ActionListener
{
JLabel l1,l2;
Choice c1,c2,c3;
JButton b1;
Checkbox cb1,cb2;
CheckboxGroup cbg;
Container c=this.getContentPane();
Graphics g;
tt_show()
{
setLayout(null);
l1=new JLabel("Course");
l2=new JLabel("Semester");
b1=new JButton("OK");
cbg=new CheckboxGroup();
cb1 = new Checkbox("full",cbg,false);
cb2 = new Checkbox("specific day",cbg,false);
c1 = new Choice();
c2 = new Choice();
c3 = new Choice();
c3.add("Monday"); c3.add("Tuesday"); c3.add("Wednesday"); c3.add("Thursday"); c3.add("Friday");
c1.add("MCA");
c.add(l1); c.add(l2); c.add(b1); c.add(l1); c.add(cb1); c.add(cb2); c.add(c1); c.add(c2); //c.add(c3);
l1.setBounds(500,160,100,100);
c1.setBounds(700,200,100,100);
l2.setBounds(500,260,100,100);
c2.setBounds(700,300,100,100);
cb1.setBounds(500,350,200,100);
cb2.setBounds(700,350,200,100);
b1.setBounds(600,500,100,30);
b1.addActionListener(this);
c1.addItemListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void itemStateChanged(ItemEvent ie)
{
if(ie.getSource()==c1)
{
for(int i=0;i<5;i++)
{
c2.add(String.valueOf(i+1));
}
}
}
public void actionPerformed(ActionEvent ae)
{
String str1="",str2="",str3="",str4="";
str1=c1.getSelectedItem();
str2=c2.getSelectedItem();
str3=cbg.getSelectedCheckbox().getLabel();
str4="select * from ";
int count=0;
Vector<Vector> data = new Vector<Vector>();
Vector<String> row = new Vector<String>();
Vector<String> cols = new Vector<String>();
cols.add("Day / Lecture -->>"); cols.add("First"); cols.add("Second"); cols.add("Third"); cols.add("Fourth"); cols.add("Fifth");
cols.add("Sixth"); cols.add("Seventh"); cols.add("Eighth");
str4=str4+str1+str2;
try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hr","hr");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(str4);
while(rs.next())
{
count++;
switch(count)
{
case 1 : row.add("Monday");
//row=new Vector<String>();
break;
case 2 : row.add("Tuesday");
// row=new Vector<String>();
break;
case 3 : row.add("Wednesday");
//row=new Vector<String>();
break;
case 4 : row.add("Thursday");
// row=new Vector<String>();
break;
case 5 : row.add("Friday");
//row=new Vector<String>();
break;
}
for(int i=1;i<=8;i++)
{
row.add(rs.getString(i));
}
data.add(row);
row=new Vector<String>();
}
}
catch(Exception e)
{
//System.out.println(e.printStackTrace());
}
time_table ob1 =new time_table();
ob1.setTitle("TIME TABLE ->> "+str1+"-"+str2);
ob1.setExtendedState(JFrame.MAXIMIZED_BOTH);
ob1.show(data,cols,str1,str2);
ob1.setVisible(true);
}
public static void main(String args[]) throws Exception
{
tt_show obj = new tt_show();
obj.setExtendedState(JFrame.MAXIMIZED_BOTH);
obj.setTitle("TIME TABLE");
obj.setVisible(true);
}
}
class time_table extends JFrame implements ActionListener
{
String course,sem;
JTable tab;
time_table()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton b1 = new JButton("Save As PDF");
Container c=getContentPane();
c.add(b1);
b1.setBounds(700,700,200,30);
b1.addActionListener(this);
}
public void show(Vector<Vector> data1,Vector<String> cols1,String s1,String s2)
{
course=s1;
sem=s2+".pdf";
tab = new JTable(data1,cols1);
TableColumn column = null;
for(int i=1;i<=8;i++)
{
column = tab.getColumnModel().getColumn(i);
column.setPreferredWidth(5);
}
tab.setEnabled(false);
tab.setBorder(BorderFactory.createLineBorder(Color.green,3));
tab.setFont(new Font("Arial",Font.PLAIN,15));
tab.setRowHeight(30);
tab.setGridColor(Color.red);
JTableHeader head = tab.getTableHeader();
head.setFont(new Font("SansSerif", Font.ITALIC, 15));
head.setForeground(Color.magenta);
Container c = this.getContentPane();
c.setLayout(new BorderLayout());
c.add("North",head);
c.add("Center",tab);
DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment( SwingConstants.CENTER );
tab.setDefaultRenderer(String.class, centerRenderer);
}
private void print() {
Document document = new Document(PageSize.A4.rotate());
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(course+sem));
document.open();
PdfContentByte cb = writer.getDirectContent();
cb.saveState();
Graphics2D g2 = cb.createGraphicsShapes(1600, 500);
Shape oldClip = g2.getClip();
g2.clipRect(0, 0, 1600, 1000);
tab.print(g2);
g2.setClip(oldClip);
g2.dispose();
cb.restoreState();
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
public void actionPerformed(ActionEvent ae)
{
print();
}
}
答案 0 :(得分:2)
我希望减少每列的大小(大小应该相同)
创建表格并设置模型后,您可以执行以下操作:
JTable table = new JTable(...);
TableColumnModel tcm = table.getColumnModel();
for (int i = 0; i < tcm.getColumnCount(); i++)
{
tcm.getColumn(i).setPreferredWidth(...);
}
另一种选择可能是使用Table Column Adjuster。此类将根据列中的数据设置列的大小。因此,它将确保所有数据都可见。
答案 1 :(得分:2)