Java-Swing 在 jtable 的单元格中添加多行
Java-Swing adding multiple lines in jtable's cell
我想在 jtable 行的同一个单元格中插入多个字符串 line.This 是我将数据添加到 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>"
您的 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()}); }
所以,无需做任何特别的事情,我就可以让 <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));
}
}
}
也许您的代码中还有其他内容,您没有向我们展示,这导致了问题...
Updated with "dynamic" example
这已经超越了最初的问题,但是,TableModel
表示它支持的数据,它提供了 JTable
显示它的结构。
因此,给定一堆 "disconnected" 值,根据您的要求,TableModel
将 "sew" 它们放在一起。
以下示例简单地将上一首诗的每一行拆分为一个数组,其中每一行代表一个元素。
然后将其再次包裹,因此这首诗的每一节都是一行行...
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
,包装到基于 <html>
的 String
.
此外,您需要单击“添加”按钮添加每个新行才能显示
public class TestPane extends JPanel {
private MyTableModel model;
private int index = 0;
public TestPane() {
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."},
{"She looked a little startled,", "but after just a while", "she made sure we were occupied", "and cracked a wicked smile.", ""},
{"Her body started swaying.", "Her toes began to tap.", "She started grooving in her seat", "and rocking to the rap."},
{"My teacher said she changed her mind.", "She thinks it's now okay", "to bring my iPod into class.", "She takes it every day."}
};
setLayout(new BorderLayout());
model = new MyTableModel();
JTable table = new JTable(model);
table.setRowHeight(75);
add(new JScrollPane(table));
JButton add = new JButton("Add");
add.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (index < data.length) {
model.addRow(data[index]);
}
index++;
if (index >= data.length) {
add.setEnabled(false);
}
}
});
add(add, BorderLayout.SOUTH);
}
public class MyTableModel extends AbstractTableModel {
private List<String[]> rowData;
public MyTableModel() {
rowData = new ArrayList<>(25);
}
public void addRow(String[] data) {
rowData.add(data);
fireTableRowsInserted(rowData.size() - 1, rowData.size() - 1);
}
@Override
public int getColumnCount() {
return 1;
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return String.class;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Object value = null;
switch (columnIndex) {
case 0:
String[] data = rowData.get(rowIndex);
StringJoiner joiner = new StringJoiner("<br>", "<html>", "</html>");
for (String text : data) {
joiner.add(text);
}
value = joiner.toString();
break;
}
return value;
}
@Override
public int getRowCount() {
return rowData.size();
}
}
}
查看 How to Use Tables 了解更多详情
我想在 jtable 行的同一个单元格中插入多个字符串 line.This 是我将数据添加到 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>"
您的 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()}); }
所以,无需做任何特别的事情,我就可以让 <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));
}
}
}
也许您的代码中还有其他内容,您没有向我们展示,这导致了问题...
Updated with "dynamic" example
这已经超越了最初的问题,但是,TableModel
表示它支持的数据,它提供了 JTable
显示它的结构。
因此,给定一堆 "disconnected" 值,根据您的要求,TableModel
将 "sew" 它们放在一起。
以下示例简单地将上一首诗的每一行拆分为一个数组,其中每一行代表一个元素。
然后将其再次包裹,因此这首诗的每一节都是一行行...
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
,包装到基于 <html>
的 String
.
此外,您需要单击“添加”按钮添加每个新行才能显示
public class TestPane extends JPanel {
private MyTableModel model;
private int index = 0;
public TestPane() {
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."},
{"She looked a little startled,", "but after just a while", "she made sure we were occupied", "and cracked a wicked smile.", ""},
{"Her body started swaying.", "Her toes began to tap.", "She started grooving in her seat", "and rocking to the rap."},
{"My teacher said she changed her mind.", "She thinks it's now okay", "to bring my iPod into class.", "She takes it every day."}
};
setLayout(new BorderLayout());
model = new MyTableModel();
JTable table = new JTable(model);
table.setRowHeight(75);
add(new JScrollPane(table));
JButton add = new JButton("Add");
add.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (index < data.length) {
model.addRow(data[index]);
}
index++;
if (index >= data.length) {
add.setEnabled(false);
}
}
});
add(add, BorderLayout.SOUTH);
}
public class MyTableModel extends AbstractTableModel {
private List<String[]> rowData;
public MyTableModel() {
rowData = new ArrayList<>(25);
}
public void addRow(String[] data) {
rowData.add(data);
fireTableRowsInserted(rowData.size() - 1, rowData.size() - 1);
}
@Override
public int getColumnCount() {
return 1;
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return String.class;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Object value = null;
switch (columnIndex) {
case 0:
String[] data = rowData.get(rowIndex);
StringJoiner joiner = new StringJoiner("<br>", "<html>", "</html>");
for (String text : data) {
joiner.add(text);
}
value = joiner.toString();
break;
}
return value;
}
@Override
public int getRowCount() {
return rowData.size();
}
}
}
查看 How to Use Tables 了解更多详情