gpt4 book ai didi

java - 与后端数据结构同步的 JTable 设计

转载 作者:搜寻专家 更新时间:2023-10-31 08:21:26 24 4
gpt4 key购买 nike

我有一个 JTable,它是使用表模型从数据结构加载的。数据结构的格式为 NavigableMap<Float,NavigableMap<Float,Boolean>> .示例数据是:

Table Format:
Range f1,v1 f2,v2 f3,v3 f4,v4
12.1-30.2 30,true 32,false 45,true 50,false
30.2-45.6 30,true 32.4,true 45,true 50.1,true

上述数据格式在DS中表示为

DS Format:
Key Value
12.1 <<30,true>,<32,false>,<45,true>,<50,false>>
30.2 <<30,true>,<32.4,true>,<45,true>,<50.1,true>>
45.6 null

我已经设法使用表模型在 Jtable 中表示上述给定数据。一旦数据从 DS 加载到表中,我必须允许用户编辑。现在这是我遇到问题的地方。我怀疑是否是应该使数据结构与表中的更改保持同步,或者我应该在用户完成编辑后从表中重新创建 DS,然后用旧的替换它。

此外,我需要验证数据(例如上面的例子——假设用户想要编辑值 30.1。他应该只被允许输入 12.1 和 45.6 之间的值。因为数据表是字符串的(一旦加载)我打算使用正则表达式和按键监听器并使用所有与正则表达式不匹配的用户按键和不在范围内的值。我不确定这是个好主意还是有什么影响.我想就此获得一些建议。

最佳答案

一旦用户完成对表格的编辑,我将重新创建您的 DS。

您始终可以创建一个自定义编辑器来显示一个弹出对话框,其中每个范围值都有两个单独的文本字段。然后,您可以将每个字段编辑为指定范围内的 double 值,并在将其保存到模型之前重新创建格式化字符串。这是我为您准备的一个旧示例:

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

/*
* The editor button that brings up the dialog.
*/
//public class TablePopupEditor extends AbstractCellEditor
public class TablePopupEditor extends DefaultCellEditor
implements TableCellEditor
{
private PopupDialog popup;
private String currentText = "";
private JButton editorComponent;

public TablePopupEditor()
{
super(new JTextField());

setClickCountToStart(2);

// Use a JButton as the editor component

editorComponent = new JButton();
editorComponent.setBackground(Color.white);
editorComponent.setBorderPainted(false);
editorComponent.setContentAreaFilled( false );

// Set up the dialog where we do the actual editing

popup = new PopupDialog();
}

public Object getCellEditorValue()
{
return currentText;
}

public Component getTableCellEditorComponent(
JTable table, Object value, boolean isSelected, int row, int column)
{

SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
System.out.println("run");
popup.setText( currentText );
// popup.setLocationRelativeTo( editorComponent );
Point p = editorComponent.getLocationOnScreen();
popup.setLocation(p.x, p.y + editorComponent.getSize().height);
popup.show();
fireEditingStopped();
}
});

currentText = value.toString();
editorComponent.setText( currentText );
return editorComponent;
}

/*
* Simple dialog containing the actual editing component
*/
class PopupDialog extends JDialog implements ActionListener
{
private JTextArea textArea;

public PopupDialog()
{
super((Frame)null, "Change Description", true);

textArea = new JTextArea(5, 20);
textArea.setLineWrap( true );
textArea.setWrapStyleWord( true );
KeyStroke keyStroke = KeyStroke.getKeyStroke("ENTER");
textArea.getInputMap().put(keyStroke, "none");
JScrollPane scrollPane = new JScrollPane( textArea );
getContentPane().add( scrollPane );

JButton cancel = new JButton("Cancel");
cancel.addActionListener( this );
JButton ok = new JButton("Ok");
ok.setPreferredSize( cancel.getPreferredSize() );
ok.addActionListener( this );

JPanel buttons = new JPanel();
buttons.add( ok );
buttons.add( cancel );
getContentPane().add(buttons, BorderLayout.SOUTH);
pack();

getRootPane().setDefaultButton( ok );
}

public void setText(String text)
{
textArea.setText( text );
}

/*
* Save the changed text before hiding the popup
*/
public void actionPerformed(ActionEvent e)
{
if ("Ok".equals( e.getActionCommand() ) )
{
currentText = textArea.getText();
}

textArea.requestFocusInWindow();
setVisible( false );
}
}

public static void main(String[] args)
{
String[] columnNames = {"Item", "Description"};
Object[][] data =
{
{"Item 1", "Description of Item 1"},
{"Item 2", "Description of Item 2"},
{"Item 3", "Description of Item 3"}
};

JTable table = new JTable(data, columnNames);
table.getColumnModel().getColumn(1).setPreferredWidth(300);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane(table);

// Use the popup editor on the second column

TablePopupEditor popupEditor = new TablePopupEditor();
table.getColumnModel().getColumn(1).setCellEditor( popupEditor );

JFrame frame = new JFrame("Popup Editor Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add( scrollPane );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}

关于java - 与后端数据结构同步的 JTable 设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3590897/

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