使用Grids java格式化JTable

时间:2014-03-14 19:11:08

标签: java swing layout jtable gridbaglayout

我无法移动我的JTable以及使行宽足够长以查看其中显示的所有数据。

当我尝试移动JTable时,无论grid我将其更改为其他字段,它都会保持接近其他字段。

如何使表格的行更宽,以便正确显示数据以及将JTable置于屏幕的左上角。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.*;

import java.sql.*;

import net.proteanit.sql.DbUtils;

public class Driver {

    private JFrame f;

    private JPanel p;

    private JTextField fieldBN;
    private JTextField fieldFN;
    private JTextField fieldLN;
    private JTextField fieldP;
    private JTextField fieldE;
    private JTextField fieldA;
    private JTextField aLine2;
    private JTextField fieldW;

    private JLabel labelBN;
    private JLabel labelFN;
    private JLabel labelLN;
    private JLabel labelP;
    private JLabel labelE;
    private JLabel labelA;
    private JLabel labelW;

    private JComboBox relationship;

    private JButton buttonS;

    private JTable tableDisplay;

    String[] relationshipValues = { "Business", "Friend", "Family", "Professional" };

    Connection conn = null;
    ResultSet rs = null;
    PreparedStatement pst = null;

    // Constructor:

    public Driver() {       
        gui();      
        conn = DbConnect.ConnectDb();
        UpdateTable();
    }

    public void gui() { 
        f = new JFrame("Contact Book");


        GridBagConstraints c = new GridBagConstraints();  

        p = new JPanel(new GridBagLayout());
        f.getContentPane().add(p, BorderLayout.NORTH);

    c.gridx = 0;
    c.gridy = 0;
    labelBN = new JLabel ("Business Name:");
    p.add(labelBN, c);  

    c.gridx = 1;
    c.gridy = 0;
    fieldBN = new JTextField(10);
    p.add(fieldBN, c);

    c.gridx = 0;
    c.gridy = 1;
    labelFN= new JLabel ("First Name:");
    p.add(labelFN, c);      

    c.gridx = 1;
    c.gridy = 1;
    fieldFN = new JTextField (10);
    p.add(fieldFN, c);

    c.gridx = 0;
    c.gridy = 2;
    labelLN= new JLabel ("Last Name:");
    p.add(labelLN, c);      

    c.gridx = 1;
    c.gridy = 2;
    fieldLN = new JTextField (10);
    p.add(fieldLN, c);

    c.gridx = 0;
    c.gridy = 3;
    labelP = new JLabel ("Phone Number:");
    p.add(labelP, c);

    c.gridx = 1;
    c.gridy = 3;
    fieldP = new JTextField (10);
    p.add(fieldP, c);

    c.gridx = 0;
    c.gridy = 4;
    labelE = new JLabel ("Email:");
    p.add(labelE, c);

    c.gridx = 1;
    c.gridy = 4;
    fieldE = new JTextField (10);
    p.add(fieldE, c);

    c.gridx = 0;
    c.gridy = 5;                            
    labelA = new JLabel ("Address:");
    p.add(labelA, c);

    c.gridx = 1;
    c.gridy = 5;
    fieldA = new JTextField (10);
    p.add(fieldA, c);
    c.gridx = 1;
    c.gridy = 6;
    aLine2 = new JTextField (10);
    p.add(aLine2, c);

    c.gridx = 0;
    c.gridy = 7;
    labelW = new JLabel ("Website:");
    p.add(labelW, c);

    c.gridx = 1;
    c.gridy = 7;
    fieldW = new JTextField (10);
    p.add(fieldW, c);

    c.gridx = 0;
    c.gridy = 8;
    labelW = new JLabel ("Relationship:");
    p.add(labelW, c);

    c.gridx = 1;
    c.gridy = 8;
    relationship = new JComboBox(relationshipValues);
    p.add(relationship, c);

    c.gridx = 1;
    c.gridy = 9;
    buttonS = new JButton("Save:");
    p.add(buttonS, c);

    c.gridx = 0;
    c.gridy = 10;
    tableDisplay = new JTable();
    p.add(tableDisplay, c);


        // pack the frame for better cross platform support
        f.pack();
        // Make it visible
        f.setVisible(true);
        f.setSize(1400,900); // default size is 0,0
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    } // End of Gui Method

    private void UpdateTable() {
        try {
        String sql = "SELECT * FROM contact";
        pst = conn.prepareStatement(sql);
        rs = pst.executeQuery();
        tableDisplay.setModel(DbUtils.resultSetToTableModel(rs));
    }

    catch(Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }
    }


    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
           public void run() {
                new Driver();

           }
        });
    } // End main Method

       } // End class Driver

1 个答案:

答案 0 :(得分:2)

使用适当的Layout Managers按您希望的方式布置组件。如果您查看Using a GridBagLayout上的部分,您会看到您正在使用gridx / gridy约束(它们表示行/列位置而不是像素位置)。此外,您会发现填充约束可以为组件之间提供空间。

  

如何使表格的行更宽,以便正确显示数据以及将JTable置于屏幕的左上角。

如果要使用GridBagLayout,那么不是第0行和第0列吗?阅读教程了解工作示例。

您可以使用Table Column Adjuster自动调整每列的宽度。