如何在JFrame中增加JTable大小

时间:2018-03-29 07:41:12

标签: java swing jtable jscrollpane layout-manager

大家好我有一个gui应用程序,它将显示我读过的csv的内容,该表将有4列和多行。我目前可以看到前三个,其余的将需要滚动浏览。

在我需要使用scrollPane之前,我需要至少显示10行。 我在这里的问题不是表的功能,而是它在我的gui页面上显示的大小。如下所示。 enter image description here

这是我的屏幕代码:

    public class Payment_import_v4 extends JFrame{
private final JTable table;
private PreparedStatement  ps;


/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable(){
        public void run()
        {
            createAndshowGUI();
        }
    });
}


private static void createAndshowGUI(){
            Payment_import_v4 form = new Payment_import_v4();
            form.setVisible(true);
} 

public Payment_import_v4(){
    //form frame
    super("Payment Import");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(700,600);
    setLocation(500,280);
    getContentPane().setLayout(null);

    //Label Result
    final JLabel lblResult = new JLabel("Result",JLabel.CENTER);
    lblResult.setBounds(150,22,370,14);
    getContentPane().add(lblResult);

    //Table
    table = new JTable();
    table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    getContentPane().add(table);


    //Table Model
    final DefaultTableModel model = (DefaultTableModel)table.getModel();
    model.addColumn("PayDate");
    model.addColumn("Ammount");
    model.addColumn("LinkId");
    model.addColumn("BranchNo");

    //ScrollPane
    JScrollPane scroll = new JScrollPane(table);
    scroll.setBounds(84,98,506,79);
    getContentPane().add(scroll);

    //Button Open
    JButton btnOpen = new JButton("Select File");
    btnOpen.setBounds(268,47,135,23);
    btnOpen.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent ae){
            JFileChooser fileOpen = new JFileChooser();
            FileFilter filter = new FileNameExtensionFilter("CSV file","csv");
            fileOpen.addChoosableFileFilter(filter);

            int ret = fileOpen.showDialog(null,"Choose file");

            if(ret == JFileChooser.APPROVE_OPTION){

                File file = fileOpen.getSelectedFile();//gets selectedFile.

                try {
                    BufferedReader br = new BufferedReader(new FileReader(file));
                    int row = 0;
                    String line;

                        br.readLine();    
                        while ((line = br.readLine()) != null) {
                          //  line = br.readLine();// br string variable
                            String[] rawRow = line.split(",");
                            String lastEntry = rawRow[rawRow.length - 1];//this contains the LinkId/branchNo
                            String[] properLastEntry = lastEntry.split("/");//this contains the LinkId/branchNo split into two columnms
                            String[] oneRow = new String[rawRow.length + 1];
                            System.arraycopy(rawRow, 0, oneRow, 0, rawRow.length - 1);
                            System.arraycopy(properLastEntry, 0, oneRow, oneRow.length - properLastEntry.length, properLastEntry.length);
                            oneRow[0] = oneRow[0].replaceAll("/","");

                            model.addRow(new Object[0]);
                            model.setValueAt(oneRow[0], row, 0);
                            model.setValueAt(oneRow[1], row, 1);
                            model.setValueAt(oneRow[2], row, 2);
                            model.setValueAt(oneRow[3], row, 3);
                            row++;
                        }
                        br.close();

                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                lblResult.setText(fileOpen.getSelectedFile().toString());
            }
        }
    });
    getContentPane().add(btnOpen);

    //btn Save
    JButton btnSave = new JButton("Save");
    btnSave.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent ea){
           SaveData();
       } 
    });
    btnSave.setBounds(292,228,89,23);
    getContentPane().add(btnSave);

    }

0 个答案:

没有答案
相关问题