使用JRadioButtons更改JTable中的列不会刷新结构

塞弗·B。

我删除了大部分GUI,以使代码简短。

我有一个由3个JRadioButtons组成的按钮组,用于选择要在我的JTable中显示的表模式,该表模式包含在JScrollPane中

我试图使用fireTableStructureChanged()fireTableDataChanged()JTable.repaint()无济于事。谁能帮我?

这是一个简单的示例,该示例使用我的配置运行一个窗口,但不更新表。

public class test1 implements ActionListener {

private boolean payrollActive = false;

private JPanel mainPanel = new JPanel();
private JTable dataTable;

private Vector<String> courseColumns = new Vector<String>();
private Vector<String> courseColumnsPay = new Vector<String>();
private Vector<String> profsColumns = new Vector<String>();
private Vector<String> offSpaceColumns = new Vector<String>();

public test1() {
    //Add columns for tables
    String[] courseColsPay = {"Year", "Program", "Course", "Code", "CCCode", 
            "Weight", "Session", "Section", "Day", "STime", "FTime", 
            "BookedRM", "EnrolCap", "Description", "ProfFName", 
            "ProfLName", "ProfEmail", "Notes", "Syllabus", "Exam", 
            "CrossList", "PreReqs", "EnrolCtrls", "Shared",
            "TrackChanges", "Address", "WageType", "BasePay",
            "BenefitRate", "Budgeted", "PayAmount", 
            "MthAmount", "Term", "AccNumber", "PayAdmin", "PayableTo"};
    for (String col : courseColsPay) {
        courseColumnsPay.add(col);
    }
    for (int i = 0; i < 25; i++) {
        courseColumns.add(courseColsPay[i]);
    }
    String[] profCols = {"FName", "LName", "Email", "UTEmail", "Birthdate", 
            "OfficeBC", "OfficeRM", "Department", "Status", 
            "Fellowship", "OfficeStat", "PhoneNum", "HomeAddr",
            "HomePhoneNum", "Notes"};
    for (String col : profCols) {
        profsColumns.add(col);
    }
    String[] offSpaceCols = {"Building", "DeptID", "DivisionName", "BldgID", "RoomID",
            "Category", "Description", "ShareType", "DeptName",
            "Status", "SharePerc", "ShareOccupancy", "Area",
            "Fellow", "Commments", "Name", "Position",
            "Dept", "FTE", "CrossApp", "CrossPos", "CrossDept",
            "CrossFTE", "OtherOffice"};
    for (String col : offSpaceCols) {
        offSpaceColumns.add(col);
    }
    mainPanel.setSize(1260, 630);
    mainPanel.setLayout(null);

    JRadioButton coursesBtn = new JRadioButton("Courses");
    coursesBtn.setMnemonic(KeyEvent.VK_C);
    coursesBtn.setActionCommand("Course");
    coursesBtn.setSelected(true);
    coursesBtn.addActionListener(this);

    JRadioButton profsBtn = new JRadioButton("Professors");
    profsBtn.setMnemonic(KeyEvent.VK_P);
    profsBtn.setActionCommand("Professors");
    coursesBtn.addActionListener(this);

    JRadioButton officeSpBtn = new JRadioButton("Office Spaces");
    officeSpBtn.setMnemonic(KeyEvent.VK_O);
    officeSpBtn.setActionCommand("Office Spaces");
    coursesBtn.addActionListener(this);

    ButtonGroup tablesBtns = new ButtonGroup();
    tablesBtns.add(coursesBtn);
    tablesBtns.add(profsBtn);
    tablesBtns.add(officeSpBtn);

    JPanel tableRadioPanel = new JPanel(new GridLayout(0, 1));
    tableRadioPanel.setOpaque(true);
    tableRadioPanel.setBounds(0, 0, 150, 70);
    tableRadioPanel.add(coursesBtn);
    tableRadioPanel.add(profsBtn);
    tableRadioPanel.add(officeSpBtn);

    //table start
    DefaultTableModel coursesModel = new DefaultTableModel(courseColumns, 200);
    dataTable = new JTable(coursesModel);
    dataTable.setFillsViewportHeight(true);
    dataTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    JScrollPane scrollPane = new JScrollPane(dataTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
            JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    scrollPane.setBounds(160, 0, 1016, 558);
    //table code end

    mainPanel.add(tableRadioPanel);
    mainPanel.add(scrollPane);

}

public JComponent getMainPanel() {
    return mainPanel;
}

public JTable getDataTable() {
    return dataTable;
}


/**
 * Returns the list of columns for the given table
 * @param identifier the name of the table
 * @return a Vector<String> of column names
 */
public Vector<String> getColumns(String identifier) {
    switch (identifier) {
    case "Courses":
        if (payrollActive) {
            return courseColumnsPay;
        } else {
            return courseColumns;
        }
    case "Professors":
        return profsColumns;
    case "Office Spaces":
        return offSpaceColumns;
    default:
        return null;
    }
}

public static void createAndShowGui() {
    test1 vicu = new test1();

    JFrame frame = new JFrame("Victoria University Database Application");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(1260, 630);
    frame.setLocationRelativeTo(null);
    JLabel emptyLabel = new JLabel("");
    emptyLabel.setPreferredSize(new Dimension(175, 100));
    frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
    frame.getContentPane().add(vicu.getMainPanel());
    frame.getContentPane().setLayout(null);
    frame.setVisible(true);

}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGui();
        }
    });

}

@Override
public void actionPerformed(ActionEvent e) {
    JRadioButton targetBtn = (JRadioButton) e.getSource();
    ((DefaultTableModel) dataTable.getModel()).
    setColumnIdentifiers(getColumns(targetBtn.getText()));
}
}
疯狂程序员

在您的示例中,您没有注册ActionListenerprofsBtnofficeSpBtn,而是继续注册到coursesBtn

JRadioButton coursesBtn = new JRadioButton("Courses");
//...
coursesBtn.addActionListener(this);

JRadioButton profsBtn = new JRadioButton("Professors");
//...
coursesBtn.addActionListener(this);

JRadioButton officeSpBtn = new JRadioButton("Office Spaces");
//...
coursesBtn.addActionListener(this);

将我注册ActionListener到正确的按钮后,它就可以正常工作

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用JRadioButtons更改JTable中的列不会刷新结构

来自分类Dev

JTable将不会刷新

来自分类Dev

刷新Jtable中的按钮

来自分类Dev

JTable需要刷新以显示更改

来自分类Dev

JTable列不会显示

来自分类Dev

([ngmodel])对象在方法中更改时不会刷新数据

来自分类Dev

使用Excel文件中的数据刷新表JTable

来自分类Dev

在Swing中刷新JTable数据

来自分类Dev

在Swing中刷新JTable数据

来自分类Dev

如何使用NetBeans 7.4 GUI设计器更改JTable中的列数?

来自分类Dev

当我更改JTable的列中的单元格时如何使用setValueAt

来自分类Dev

当查询更改时,Java刷新JTable

来自分类Dev

Java-方法更改JTable中变量的值(刷新不起作用)

来自分类Dev

更改搜索值时刷新/更新JTable中的荧光笔

来自分类Dev

更改R中表的列中的行结构

来自分类Dev

如何在JTable中更改或查找列类型

来自分类Dev

如何在JTable中更改或查找列类型

来自分类Dev

Jtable Cellrenderer不会更改整数值的背景

来自分类Dev

Winform中的Groupbox不会刷新

来自分类Dev

刷新代码而不会覆盖我的更改

来自分类Dev

刷新JTable中的所有数据

来自分类Dev

表刷新中的jtable单列/字段

来自分类Dev

IntelliJ中刷新gradle导致源文件夹结构发生更改

来自分类Dev

polymer / page.js更改javascript中的路由而不会导致页面刷新

来自分类Dev

更改后刷新页面不会在Web浏览器中呈现

来自分类Dev

在刷新服务器之前,更改不会反映在 tempate django 中

来自分类Dev

如果从订阅中显示,Ngx-Bootstrap 模式不会刷新数据更改

来自分类Dev

使用time.time()的当前时间在刷新时不会更改[Python]

来自分类Dev

使用URL.push即使URL更改,我的页面也不会刷新

Related 相关文章

热门标签

归档