【问题标题】:formatting JTable with Grids java使用网格 java 格式化 JTable
【发布时间】:2023-03-08 12:49:01
【问题描述】:

我无法移动我的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

【问题讨论】:

    标签: java swing layout jtable gridbaglayout


    【解决方案1】:

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

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

    如果您想使用 GridBagLayout,那不是第 0 行和第 0 列。阅读教程以获取工作示例。

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

    【讨论】:

    • 感谢您的意见,但这对我来说并不能概括。我了解您是在告诉我阅读表格列调整器并实施它。你说网格需要设置为 0 0 或 0 1。不像它们是像素。我更改了所有网格,但 jtable 仍然没有像我期望的那样移动。你能详细说明这是为什么吗?我更新了我的代码。
    • @NichoDiaz, I updated my code. - 更新的代码没有帮助,因为我无法运行代码。我无权访问您的数据库。发布SSCCE,我也许可以快速浏览一下,但我不是 GridBagLayout 专家。我通常使用带有不同布局管理器的嵌套 JPanel 来实现我想要的布局。
    猜你喜欢
    • 2016-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 2011-08-27
    • 2015-07-26
    相关资源
    最近更新 更多