Java 是否可以在Option Dialog box中设置表格行
我有一张表格 有些单元格包含很长的字符串,很难在其中左右滚动。我的问题是,是否可以在弹出的例如showDialog类型对话框中显示JTable中的行(即所选行被组织为列) 甚至连教程的链接都可以 我浏览过互联网,但我认为我没有真正使用正确的关键字,因为我有很多右键点击选项Java 是否可以在Option Dialog box中设置表格行,java,swing,jtable,Java,Swing,Jtable,我有一张表格 有些单元格包含很长的字符串,很难在其中左右滚动。我的问题是,是否可以在弹出的例如showDialog类型对话框中显示JTable中的行(即所选行被组织为列) 甚至连教程的链接都可以 我浏览过互联网,但我认为我没有真正使用正确的关键字,因为我有很多右键点击选项 如果这是不可能的,有没有其他建议如何做到这一点 您可以使用以下ListSelectionListener: final JTable dialogTable =new JTable(); table.getSelec
如果这是不可能的,有没有其他建议如何做到这一点 您可以使用以下
ListSelectionListener
:
final JTable dialogTable =new JTable();
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent event) {
int selectedRow = table.getSelectedRow();
if (selectedRow > -1) {
int columnCount = table.getModel().getColumnCount();
Object[] column = new Object[]{"Row "+(selectedRow+1)};
Object[][] data = new Object[columnCount][1];
for (int i = 0; i < columnCount; i++) {
Object obj = table.getModel().getValueAt(selectedRow, i);
data[i][0] = obj;
}
dialogTable.setModel(new DefaultTableModel(data, column));
JOptionPane.showMessageDialog(null, new JScrollPane(dialogTable));
}
}
});
final JTable dialogTable=new JTable();
table.getSelectionModel().addListSelectionListener(新ListSelectionListener()){
@凌驾
public void值已更改(ListSelectionEvent事件){
int selectedRow=table.getSelectedRow();
如果(选择箭头>-1){
int columnCount=table.getModel().getColumnCount();
对象[]列=新对象[]{“行”+(selectedRow+1)};
对象[][]数据=新对象[columnCount][1];
对于(int i=0;i
这将显示一个消息对话框,其中包含一个
JTable
,其中包含从所选行派生的数据。希望这对你有帮助 您可以使用以下ListSelectionListener
:
final JTable dialogTable =new JTable();
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent event) {
int selectedRow = table.getSelectedRow();
if (selectedRow > -1) {
int columnCount = table.getModel().getColumnCount();
Object[] column = new Object[]{"Row "+(selectedRow+1)};
Object[][] data = new Object[columnCount][1];
for (int i = 0; i < columnCount; i++) {
Object obj = table.getModel().getValueAt(selectedRow, i);
data[i][0] = obj;
}
dialogTable.setModel(new DefaultTableModel(data, column));
JOptionPane.showMessageDialog(null, new JScrollPane(dialogTable));
}
}
});
final JTable dialogTable=new JTable();
table.getSelectionModel().addListSelectionListener(新ListSelectionListener()){
@凌驾
public void值已更改(ListSelectionEvent事件){
int selectedRow=table.getSelectedRow();
如果(选择箭头>-1){
int columnCount=table.getModel().getColumnCount();
对象[]列=新对象[]{“行”+(selectedRow+1)};
对象[][]数据=新对象[columnCount][1];
对于(int i=0;i
这将显示一个消息对话框,其中包含一个JTable
,其中包含从所选行派生的数据。希望这对你有帮助
我想显示行中的所有值,每个值都在它们的cel中,垂直组织-这就是我所说的“在列中” 这应该在问题中,而不是在评论中 这没有默认功能,但您可以自己完成 您可以创建一个JPanel(可能使用GridBagLayout),一行中有两个标签来表示表中选定行的列中的数据 对于第一个标签中的数据,可以使用TableModel的
getColumnName(…)
方法
对于第二个标签中的数据,可以使用TableModel的getValueAt(…)
方法
另一个选项是简单地显示单元的工具提示。有关详细信息,请参见上的Swing教程部分
我想显示行中的所有值,每个值都在它们的cel中,垂直组织-这就是我所说的“在列中” 这应该在问题中,而不是在评论中 这没有默认功能,但您可以自己完成 您可以创建一个JPanel(可能使用GridBagLayout),一行中有两个标签来表示表中选定行的列中的数据 对于第一个标签中的数据,可以使用TableModel的
getColumnName(…)
方法
对于第二个标签中的数据,可以使用TableModel的getValueAt(…)
方法
另一个选项是简单地显示单元的工具提示。有关详细信息,请参见上的Swing教程部分 如图所示,工厂方法将显示在消息
参数中传递的对象
。如果该消息
是一列JTable
,则可以回收应用于原始表的任何自定义项
概括地说
- 向表中添加一个,然后获取
selectedRow
- 遍历表的模型并构造一个
,其行是newModel
的列selectedRow
- 创建一个
JTable newTable=newjtable(newModel)
- 应用任何非默认渲染器和编辑器
- 将
作为newjscrollpane(newTable)
参数传递给所选的消息
方法JOptionPane
table.getSelectionModel().addListSelectionListener(新建ListSelectionListener()){
@凌驾
public void值已更改(ListSelectionEvent e){
int-selectedRow=table.convertRowIndexToModel(table.getSelectedRow());
如果(选择箭头>-1){
DefaultTableModel newModel=新的DefaultTableModel();
String rowName=“行:”+selectedRow;
setColumnIdentifiers(新对象[]{rowName});
对于(int i=0;i