我想逐行在jtable中的同一单元格中插入多个字符串。这是我将数据添加到jtable中的方式
String Model,Brand,Serial;
String itemdetails=Model+Brand+Serial
model.addRow(new Object[]{itemdetails,amountText.getText()});
这里的问题是,将输出单行输出,但我想在jtbale的单元格中输出这样的输出。
Model //it is string coming from database
Brand //it is string coming from database
Serial //it is string coming from database
我已经尝试了这个,但它只使用双引号内的数据,而不是字符串。
"<html>lineOne <br/> lineTwo </html>"
答案 0 :(得分:3)
所以,没有做任何特别的事情,我可以让<html>...<br>...</html>
正常工作......
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
DefaultTableModel model = new DefaultTableModel(0, 1);
model.addRow(new String[]{
"<html>My teacher took my iPod.<br>She said they had a rule;<br>I couldn't bring it into class<br>or even to the school.</html>"
});
model.addRow(new String[]{
"<html>She said she would return it;<br>I'd have it back that day.<br>But then she tried my headphones on<br>and gave a click on Play.</html>"
});
model.addRow(new String[]{
"<html>She looked a little startled,<br>"
+ "but after just a while<br>"
+ "she made sure we were occupied<br>"
+ "and cracked a wicked smile.<br>"
});
model.addRow(new String[]{
"<html>Her body started swaying.<br>"
+ "Her toes began to tap.<br>"
+ "She started grooving in her seat<br>"
+ "and rocking to the rap.</html>"
});
model.addRow(new String[]{
"<html>My teacher said she changed her mind.<br>"
+ "She thinks it's now okay<br>"
+ "to bring my iPod into class.<br>"
+ "She takes it every day.</html>"
});
setLayout(new GridBagLayout());
JTable table = new JTable(model);
table.setRowHeight(75);
add(new JScrollPane(table));
}
}
}
也许您的代码中还有其他内容,您没有向我们展示,这导致了问题......
更新了“动态”示例
这已超过原始问题,但是TableModel
表示它支持的数据,它为JTable
提供了显示它的结构。
因此,给定一堆“断开连接”的值,TableModel
将根据您的要求将它们“缝合”在一起。
以下示例simple将上一首诗的每一行拆分成一个数组,每行代表一个元素。
然后再将它包裹起来,这样诗的每一部分都是一系列的行......
String data[][] = {
{"My teacher took my iPod.", "She said they had a rule;", "I couldn't bring it into class", "or even to the school."},
{"She said she would return it;", "I'd have it back that day.", "But then she tried my headphones on", "and gave a click on Play."},
etc...
然后该示例使用自定义TableModel
,当询问单元格的值时,取自给定的“section”并从每行构建String
,包装成{{1}基于<html>
。
此外,您需要单击“添加”按钮以添加每个新行,然后才能显示
String
请查看How to Use Tables了解详情
答案 1 :(得分:0)
您的for循环未正确关闭:
DefaultTableModel model = (DefaultTableModel) jt.getModel();
int i;
for(i=0; i<model.getRowCount(); i++){
String itemdetails=model+"\n"+brand;
System.out.println(itemdetails);
jt.setRowHeight(30);
model.addRow(new Object[]{i+1,itemdetails,amountText.getText()}); }