简体   繁体   English

调整 JTable 大小

[英]Resize JTable size

I have a JTable with user data distributed in rows and columns .我有一个JTable用户数据分布rowscolumns中。 I want when, I look for a user using the ID in a JTextField , resize the size ( height , with the same width ) depending on the number of rows found.我想要什么时候,我在JTextField中寻找使用 ID 的用户,根据找到的rows调整大小height宽度相同)。 Why is the width of the table reduced?为什么桌子的宽度变小了?

Code:代码:

    tabla_clientes.setPreferredScrollableViewportSize(
            new Dimension(tabla_clientes.getPreferredSize().width, tabla_clientes.getRowHeight()*20)
    );

    //...

private void resizer() {
    revalidate();
    int w = jScrollPane.getPreferredSize().width; //width scroll pane
    int h1 = jScrollPane.getViewport().getViewSize().height; //height viewport
    int h2 = tabla_clientes.getPreferredScrollableViewportSize().height; //table height

    if(h1<h2)
      jScrollPane.setSize(new Dimension(w,h1));
    else
      jScrollPane.setSize(tabla_clientes.getPreferredScrollableViewportSize());
}

public void filtrar_dni() {
    int columna = 0;
    TRSFiltro.setRowFilter(RowFilter.regexFilter(textfield_buscar.getText(), columna));
}


private void textfield_buscarKeyTyped(java.awt.event.KeyEvent evt) {                                          
    textfield_buscar.addKeyListener(new KeyAdapter(){
    public void keyReleased(final KeyEvent e){
        String texto = (textfield_buscar.getText());
        textfield_buscar.setText(texto);
        filtrar_dni();
        resizer();
    }
    });
    
    TRSFiltro = new TableRowSorter<DefaultTableModel>((DefaultTableModel) tabla_clientes.getModel());
    tabla_clientes.setRowSorter(TRSFiltro);
}

Unexpected result:意外结果: 在此处输入图像描述

Original table:原表: 在此处输入图像描述

You should use LayoutManagers: https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html This makes positioning of the components much easier.您应该使用 LayoutManagers: https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html这使得组件的定位更加容易。 An easy to understand example could be:一个易于理解的示例可能是:

import javax.swing.*;
import java.awt.*;

public class Test {

    public static void main(String[] args) {
        JLabel bigLabel = new JLabel("VERY BIG");
        bigLabel.setPreferredSize(new Dimension(100, 1000));

        JPanel north = new JPanel();
        north.setLayout(new FlowLayout());
        north.add(new JLabel("NORTH CONTENT"));

        JScrollPane p = new JScrollPane();
        p.setViewportView(bigLabel);

        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        f.setPreferredSize(new Dimension(200, 300));
        f.getContentPane().setLayout(new BorderLayout());
        f.getContentPane().add(north, BorderLayout.NORTH);
        f.getContentPane().add(p, BorderLayout.CENTER);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}

I'm not going to show you the version without layout managers, because 1) I'm not even sure to be able to write it correctly and 2) you should not do it anyway, except when you have a very good reason to (eg. no Layout Manager exist for your needs - which is, btw, very rare).我不会向您展示没有布局管理器的版本,因为 1)我什至不确定是否能够正确编写它,并且 2)您无论如何都不应该这样做,除非您充分的理由(例如,不存在满足您需求的布局管理器 - 顺便说一句,非常罕见)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM