0

I have a JTable with user data distributed in rows and columns. 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. 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: enter image description here

Original table: enter image description here

camickr
  • 321,443
  • 19
  • 166
  • 288
SrCantabri
  • 37
  • 5
  • it seems that you not using any layout manager. Is it on purpose ? because by using a layout, you won't have to do any computation by yourself anymore. See layouts: https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html – spi May 19 '21 at 11:12
  • @spi I'm not using any layout. Is it necessary? – SrCantabri May 19 '21 at 11:14
  • no, it is not strictly necessary. But it would be much much easier if you were using one. It implements all the necessary computation to adapt the size of your components to the available space. It will react to the resizing of the frame, hidding of components, etc. – spi May 19 '21 at 11:16
  • @spi And what would be a possible solution without using layouts? And with layout? I would appreciate a brief explanation with both options to see the difference. – SrCantabri May 19 '21 at 11:20
  • see the example below. – spi May 19 '21 at 11:43
  • (1-) 1) this is still not even close to an [mre]. The OP have been given examples in his last two question on how to create an MRE. 2) The OP was even given the code to properly size the table but has ignored that code and as a result still has a problem. – camickr May 19 '21 at 14:44

1 Answers1

0

You should use LayoutManagers: https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html This makes positioning of the components much easier. 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).

spi
  • 1,673
  • 13
  • 19
  • I am a beginner in Swing, so is it "obligatory" to use layouts in any `JFrame` or `JDialog`? So... would using a layout in my code be enough? `setLayout(new BorderLayout());` – SrCantabri May 19 '21 at 11:51
  • As previously said, it will be harder to manage that yourself. So, even if not "mandatory", you will gain a lot by learning layouts (and this is even not very hard to understand, at least for some layouts - GridBagLayout is another story...). I would advise you to look at the link I've provided, run the example, play with them and get a basic understanding of your options. It shouldn't take more than a few hours to get started. In case of doubt, come back to ask and I'll try to answer as accurately as I can. – spi May 19 '21 at 12:02
  • I understood your explanation and got it by inserting a `panel_medio.setLayout(new FlowLayout());` – SrCantabri May 19 '21 at 12:27
  • @SrCantabri, you were given a complete working example in your previous question that uses layout managers: https://stackoverflow.com/questions/67506793/reset-jtable-depending-on-content-number-of-rows. You continue to waste people time by 1) not posting an MRE and 2) not following working solutions. – camickr May 19 '21 at 14:46