简体   繁体   English

如何在JInternalFrame中的ScrollPane中动态设置JTable的宽度

[英]How to set dynamically JTable's width in ScrollPane in JInternalFrame

I have a little doubt using JTable. 我有点怀疑使用JTable。

This is that I want: 这是我想要的:

  • To set a horizontal ScroollBar in my JTable with no maximized window. 在没有最大化窗口的JTable中设置水平的ScroollBar。
  • To see each column with the max width possible (or column header or text in cell). 要查看具有最大可能宽度的每一列(或列标题或单元格中的文本)。
  • Expand dinamically my window depending of JTable's width (I don't know if it is possible). 根据JTable的宽度,动态地扩展我的窗口(我不知道是否可能)。

This is that I have: 这是我有:

  • A Jtable with no horizontal ScrollBar (if i put "myTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);", my table doesn't occupy all width of JInternalFrame) 一个没有水平ScrollBar的Jtable(如果我放置“ myTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);”,则我的表不会占据JInternalFrame的所有宽度)
  • My Jtable doesn't show all datas in a cell if its size is long. 如果Jtable的大小过长,它不会在一个单元格中显示所有数据。

I will appreciate any help. 我将不胜感激。

AplicacionesInternalFrame: AplicacionesInternalFrame:

public AplicacionesJInternalFrame() 
{
      super("Aplicaciones", 
              true, //resizable
              true, //closable
              true, //maximizable
              true);//iconifiable

      //...Create the GUI and put it in the window...

      //...Then set the window size or call pack...
      //setSize(300,300);

      //Set the window's location.
      setLocation(xOffset*openFrameCount, yOffset*openFrameCount);
      getContentPane().add(createToolBar(),BorderLayout.NORTH);

      jTableAplicaciones = new JTable(new AplicacionesTableModel());        
      jTableAplicaciones.getColumnModel().getColumn(0).setCellRenderer(new AplicacionesRenderer());
      jTableAplicaciones.setPreferredScrollableViewportSize(new Dimension(500, 270));
      jTableAplicaciones.setFillsViewportHeight(true);

      JScrollPane scrollPane = new JScrollPane(jTableAplicaciones);
      scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); 
      scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

      initColumnSizes(jTableAplicaciones);
      //jTableAplicaciones.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

      add(scrollPane);        
      pack();
}


private void initColumnSizes(JTable table) {
    TableColumn column = null;
    Component comp = null;
    int headerWidth = 0;
    TableCellRenderer headerRenderer =
        table.getTableHeader().getDefaultRenderer();

    int width = 0;
    for (int i = 0; i < table.getColumnCount(); i++) 
    {           
        /* Tamaño dato de celda */
        TableCellRenderer renderer = table.getCellRenderer(0, i);
        Component componente = table.prepareRenderer(renderer, 0, i);
        int componenteWidth = componente.getPreferredSize().width;

        /* Tamaño cabecera columna */
        column = table.getColumnModel().getColumn(i);
        comp = headerRenderer.getTableCellRendererComponent(
                null, column.getHeaderValue(),
                false, false, 0, 0);            
        headerWidth = comp.getPreferredSize().width;
        column.setPreferredWidth(Math.max(headerWidth, componenteWidth)+2);
    }        
}

If you need any other data, please let me know it. 如果您需要其他任何数据,请告诉我。 Thanks in advance. 提前致谢。

My Jtable doesn't show all datas in a cell if its size is long. 如果Jtable的大小过长,它不会在一个单元格中显示所有数据。

You can use the Table Column Adjuster to make sure all columns are an appropriate. 您可以使用“ 表列调整器”来确保所有列都是合适的。 However, this will not force the table to fill the entire width. 但是,这不会强制表格填满整个宽度。

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

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