我有一个代码用Excel文件中的数据填充JTable。问题是在将数据设置为JTable之后,我无法使用新数据重新绘制表。 信息收费正常但没有刷新表。 在选择文件后,我通过“Procesar”按钮对数据进行充电。我的代码是:
`
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JFileChooser;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.awt.Color;
import javax.swing.JTextPane;
import java.awt.TextArea;
import javax.swing.border.MatteBorder;
public class Principal
{
private File file;
Vector header = new Vector();
Vector data = new Vector();
DefaultTableModel model = new DefaultTableModel(data,header);
private JFrame frame;
private JTextField txtRuta;
private JButton btnBuscar;
private JButton btnProcesar;
private MouseAdapter mouseAdapterBtnBuscar;
private MouseAdapter mouseAdapterBtnProcesar;
private JTextPane txtPane = new JTextPane();
private TextArea textArea;
private JTable table;
/**
* Método principal que lanza la aplicación
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable() {
public void run()
{
try
{
Principal window = new Principal();
window.frame.setVisible(true);
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Constructor de la clase.
*/
public Principal() {
initialize();
}
/**
* Inicializa el contenido del Frame visual.
*/
private void initialize()
{
inicializeHandlers();
frame = new JFrame();
frame.setBounds( 100, 100, 613, 592 );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.getContentPane().setLayout( null );
btnBuscar = new JButton( "Buscar" );
btnBuscar.addMouseListener( mouseAdapterBtnBuscar );
btnBuscar.setBounds(498, 11, 89, 23);
frame.getContentPane().add( btnBuscar );
btnProcesar = new JButton( "Procesar" );
btnProcesar.addMouseListener( mouseAdapterBtnProcesar );
btnProcesar.setBounds( 498, 40, 89, 23 );
frame.getContentPane().add( btnProcesar );
txtRuta = new JTextField();
txtRuta.setBounds( 10, 12, 478, 20 );
frame.getContentPane().add( txtRuta );
txtRuta.setColumns( 10 );
textArea = new TextArea();
textArea.setBounds(10, 88, 577, 190);
frame.getContentPane().add(textArea);
table = new JTable();
table.createDefaultColumnsFromModel();
table.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
table.setSurrendersFocusOnKeystroke(true);
table.setColumnSelectionAllowed(true);
table.setCellSelectionEnabled(true);
table.setBounds(10, 321, 577, 190);
frame.getContentPane().add(table);
}
/*Método para controlar los listeners de los componentes.*/
private void inicializeHandlers()
{
mouseAdapterBtnBuscar = new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent arg0)
{
JFileChooser flsBuscador = new JFileChooser();
int result = flsBuscador.showOpenDialog(null);
if ( result == JFileChooser.APPROVE_OPTION )
{
file = flsBuscador.getSelectedFile();
txtRuta.setText( file.getAbsolutePath() );
}
}
};
mouseAdapterBtnProcesar = new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent arg0)
{
if ( txtRuta.getText() == null || txtRuta.getText() == "" || txtRuta.getText().endsWith(".xlsx") == false )
{
JOptionPane.showMessageDialog( frame, "La ruta no es correcta o el archivo no es soportado.", "Información", JOptionPane.WARNING_MESSAGE );
}
else
{
try
{
procesarArchivo();
}
catch( Exception ex )
{
ex.printStackTrace();
}
}
}
};
}
private void procesarArchivo() throws Exception
{
try
{
if ( file != null )
{
String text = "Inciando lectura...\n";
FileInputStream fis = new FileInputStream( file );
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFRow row;
textArea.setText( text );
for ( int i = 6; i < sheet.getPhysicalNumberOfRows(); i ++ )
{
Vector d = new Vector();
row = sheet.getRow( i );
for ( int j = 0; j < row.getPhysicalNumberOfCells(); j++ )
{
XSSFCell cell = row.getCell( j );
if ( cell != null )
{
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC)
{
if ( HSSFDateUtil.isCellDateFormatted(cell) )
{
d.add( cell.getDateCellValue().toString().trim() );
text = text + cell.getDateCellValue().toString().trim() + "\n";
textArea.setText( text );
}
else
{
d.add( Double.toString( cell.getNumericCellValue() ).trim() );
text = text + Double.toString( cell.getNumericCellValue() ).trim() + "\n";
textArea.setText( text );
}
}
else
{
d.add( cell.getStringCellValue().trim() );
text = text + cell.getStringCellValue().trim() + "\n";
textArea.setText( text );
}
if ( i == 6 )
{
header.add(d);
}
}
else
{
d.add("NULL");
}
}
d.add( "\n" );
data.add( d );
}
model = new DefaultTableModel(data, header);
table.setModel(model);
JScrollPane scroll = new JScrollPane(table);
frame.getContentPane().add( scroll );
}
}
catch( Exception ex )
{
ex.printStackTrace();
}
}
}
`
感谢您的帮助;
答案 0 :(得分:1)
在procesarArchivo()
方法中...您想将一个表添加到框架的内容窗格中多少次?
JScrollPane scroll = new JScrollPane(table);
frame.getContentPane().add( scroll );
每次按下btnProcesar
时,在当前代码中,新的JTable
会添加到框架的内容窗格中。您无法看到它,因为您没有调用revalidate()方法:
frame.getContentPane().revalidate();
表格Container.add() javadoc:
此方法更改与布局相关的信息,因此, 使组件层次结构无效。 如果容器已经存在 显示后,必须在此后验证层次结构 显示添加的组件。
如果你打电话给revalidate()
方法,你会看到2个表,然后是3个,依此类推,这不是你想要的。不要添加新的JTable
。只需添加一个JTable
并刷新其TableModel
。
其他一些提示:
请勿设置null
布局:frame.getContentPane().setLayout( null );
。改为使用LayoutManager
:Using Layout Managers。它将处理组件大小调整及其定位。
按下按钮时,请勿使用MouseListener
来处理事件。请改为使用ActionListener
:How to Use Buttons, Check Boxes, and Radio Buttons
仔细阅读本教程:How to Use Tables