gpt4 book ai didi

java - 单击 JTable 单元格应将表单显示到容器中

转载 作者:行者123 更新时间:2023-11-30 04:43:41 25 4
gpt4 key购买 nike

我是 Java Swing 新手,我正在尝试创建一个应用程序。

我有一个 MainApplication.java 文件,它扩展了 SingleFrameApplication,并在其中创建了一个名为 MainPanel 的 JPanel。此 MainPanel 有一个名为 SplitPane 的带有 VERTICAL_SPLIT 的 AnimatingSplitPane。

在 SplitPane 的顶部,我添加了另一个名为 MainContainer 的 JPanel。在 SplitPane 的底部,我添加了一个名为 FormContainer 的 JPanel。 MainContainer 加载另一个名为 DataSheetTable 的类(具有 JTable 的 JPanel)。

现在,当用户单击 DataSheetTable 的单元格时,我想将表单加载到 FormContainer 中。我不知道如何才能实现这一目标。

例如,DatasheetTable 有 Column1、Column2 和 Column3。当用户单击 Column1 的任何单元格时,我需要将 Form1 显示到 FormContanier 中。如果它单击 Column2 单元格,那么我需要将 Form2 显示到 FormContanier 中。

请让我知道一些示例代码,如何实现将表单动态加载到 FormContainer。

![提前谢谢您。]

<强> Image description for the issue

这里是 App.java 的示例代码

public class App extends SingleFrameApplication {
@Override protected void startup() {
configureDefaults();

View view = getMainView();
view.setComponent(createMainPanel());

show(view);
}

protected JComponent createMainPanel() {
// Create main panel with demo selection on left and demo/source on right
mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());


// Create splitpane on right to hold demo and source code
splitPane = new AnimatingSplitPane(JSplitPane.VERTICAL_SPLIT);
mainPanel.add(splitPane, BorderLayout.CENTER);

// Create panel to contain main panel
mainContainer = new JPanel();
splitPane.setTopComponent(mainContainer);

DataSheetTable dataSheetTable = new DataSheetTable();
mainContainer.add(dataSheetTable, BorderLayout.CENTER);
dataSheetTable.start();

formContainer = new JPanel(new BorderLayout());
splitPane.setBottomComponent(formContainer);
formContainer.add(new OrganizationForm());

return mainPanel;
}
}

这里是 DataSheetTable.java 文件的示例代码

public class DataSheetTable extends JPanel {

........
controlPanel = createControlPanel();
add(controlPanel, BorderLayout.NORTH);


routingTable = new JTable(routingModel);

.........

}

最佳答案

这里是拦截表格单元格点击事件的代码:

public class App extends JFrame {
private DefaultTableModel model = new DefaultTableModel(new Object[][] {
{ "Value1", "Value2", "Value3" }, { "Object1", "Object2", "Object3" } }, new String[] {
"Column1", "Column2", "Column3" });
private JTable table = new JTable(model);
private JPanel bottomPanel;

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
App a = new App();
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a.setLocationRelativeTo(null);
a.setVisible(true);
}
});
}

public App() {
JSplitPane pane = new JSplitPane();
pane.setOrientation(SwingConstants.HORIZONTAL);
pane.setLeftComponent(new JScrollPane(table));
bottomPanel = new JPanel();
bottomPanel.add(new JLabel("properties for column will be here"));
pane.setRightComponent(bottomPanel);
add(pane);

ListSelectionListener listener = new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
int column = table.getSelectedColumn();
int row = table.getSelectedRow();
if(row != -1 && column != -1) {
bottomPanel.removeAll();
//Here you add your components/create appropriate panel
//e.g. bottomPanel.add(new PropertiesPanelForValue1(...));
bottomPanel.add(new JLabel("User selected column " + column + " and row " + row
+ " with value: '" + table.getValueAt(row, column) + "'"));
bottomPanel.revalidate();
}
}
};
table.getSelectionModel().addListSelectionListener(listener);
table.getColumnModel().getSelectionModel().addListSelectionListener(listener);

pack();

pane.setDividerLocation(0.3);
setSize();
}

private void setSize() {
double widthPart = 0.3;
double heightPart = 0.5;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int width = (int) (screenSize.getWidth() * widthPart);
int height = (int) (screenSize.getHeight() * heightPart);
setSize(width, height);
Dimension windowSize = getSize();
int x = (int) ((screenSize.getWidth() - windowSize.getWidth()) / 2);
int y = (int) ((screenSize.getHeight() - windowSize.getHeight()) / 2);
setLocation(x, y);
}
}

编辑查看您的更新:只需添加对 FormContainerDataSheetTable 构造函数引用即可:

formContainer = new JPanel(new BorderLayout());
splitPane.setBottomComponent(formContainer);
formContainer.add(new OrganizationForm());

DataSheetTable dataSheetTable = new DataSheetTable(formContainer);
mainContainer.add(dataSheetTable, BorderLayout.CENTER);
dataSheetTable.start();

并在DataSheetTable中添加监听器:

public class DataSheetTable extends JPanel {

public DataSheetTable(final FormContainer formContainer) {
........
controlPanel = createControlPanel();
add(controlPanel, BorderLayout.NORTH);


routingTable = new JTable(routingModel);
ListSelectionListener listener = new ListSelectionListener() {...};
routingTable.getSelectionModel().addListSelectionListener(listener);
routingTable.getColumnModel().getSelectionModel().addListSelectionListener(listener);

.........
}

}

关于java - 单击 JTable 单元格应将表单显示到容器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11638097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com