簡體   English   中英

JComponents的運行時對齊+鏈接到RowFilters

[英]Runtime alignment of JComponents + chaining to RowFilters

我目前正在開發一個相當復雜的應用程序。 我的工作是構建GUI的一部分。
主要區域是為JTable派生的,並包含所有與應用程序相關的數據。 表格上方有一些元素,允許用戶控制數據在表格中的顯示方式。
與手頭任務相關的選項有:

  • 改變列數,
  • 獨立更改列的寬度(不通過JTableHeader)和
  • 每列輸入一個過濾條件,以選擇數據的特定行。

此方案的主要目標是為當前視圖設置中的每一列創建一個組件(可能是JTextField),該組件准確地與該列對齊(盡管它會在運行時更改大小)。

第一個問題:

對齊不起作用。 我無法獲取文本字段的寬度以匹配列的寬度。
我該如何工作?

第二個問題:

我希望將各個過濾器鏈接在一起。 也就是說,如果用戶決定輸入多個過濾器字符串,則應針對它們各自的列評估所有字符串,並且僅顯示與所有過濾器匹配的行。 到目前為止,第二個TextField中的輸入將刪除第一個過濾器(使用RowFilter.regexFilter可以正常工作)。
我該如何工作?

請讓我知道,哪些代碼片段對您可能有用,我很樂意將其發布。

在此先感謝您提供的任何幫助。

問候,DK

我無法獲取文本字段的寬度以匹配列的寬度

這個例子應該使您入門:

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

public class TableFilterRow extends JFrame implements TableColumnModelListener
{
    private JTable table;
    private JPanel filterRow;

    public TableFilterRow()
    {
        table = new JTable(3, 5);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane( table );
        getContentPane().add( scrollPane );
        table.getColumnModel().addColumnModelListener( this );

        //  Panel for text fields

        filterRow = new JPanel( new FlowLayout(FlowLayout.CENTER, 0 , 0) );

        for (int i = 0; i < table.getColumnCount(); i ++)
            filterRow.add( new JTextField("" + i) );

        columnMarginChanged( new ChangeEvent(table.getColumnModel()) );
        getContentPane().add(filterRow, BorderLayout.NORTH);
    }

    //  Implement TableColumnModelListener methods
    //  (Note: instead of implementing a listener you should be able to
    //  override the columnMarginChanged and columMoved methods of JTable)

    public void columnMarginChanged(ChangeEvent e)
    {
        TableColumnModel tcm = table.getColumnModel();
        int columns = tcm.getColumnCount();

        for (int i = 0; i < columns; i ++)
        {
            JTextField textField = (JTextField)filterRow.getComponent( i );
            Dimension d = textField.getPreferredSize();
            d.width = tcm.getColumn(i).getWidth();
            textField.setPreferredSize( d );
        }

        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                filterRow.revalidate();
            }
        });
    }

    public void columnMoved(TableColumnModelEvent e)
    {
        Component moved = filterRow.getComponent(e.getFromIndex());
        filterRow.remove(e.getFromIndex());
        filterRow.add(moved, e.getToIndex());
        filterRow.validate();
    }

    public void columnAdded(TableColumnModelEvent e) {}
    public void columnRemoved(TableColumnModelEvent e) {}
    public void columnSelectionChanged(ListSelectionEvent e) {}

    public static void main(String[] args)
    {
        JFrame frame = new TableFilterRow();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }
}

僅顯示與所有過濾器匹配的行

閱讀JTable API並單擊“如何使用表”上的Swing教程鏈接,您將在其中找到TableFilterDemo。 您可以輕松地修改代碼以使用“和”過濾器。 該代碼將類似於:

// rf = RowFilter.regexFilter(filterText.getText(), 0);
List<RowFilter<Object,Object>> filters = new ArrayList<RowFilter<Object,Object>>(2);
filters.add(RowFilter.regexFilter(filterText.getText(), 0));
filters.add(RowFilter.regexFilter(filterText.getText(), 1));
rf = RowFilter.andFilter(filters);

本示例共享一個文本字段,以在多列中查找相同的字符串。 您顯然會使用您的單個文本字段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM