Java-Swing adding multiple lines in jtable's cell

后端 未结 2 901
渐次进展
渐次进展 2020-12-12 08:14

i would like to insert multiple strings in the same cell in a jtable line by line.This is the way i added the data into jtable

       String Model,Brand,Seri         


        
2条回答
  •  囚心锁ツ
    2020-12-12 08:48

    So, without doing anything special, I can make ...
    ...
    work just fine...

    Multilines

    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[]{
                    "My teacher took my iPod.
    She said they had a rule;
    I couldn't bring it into class
    or even to the school." }); model.addRow(new String[]{ "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." }); model.addRow(new String[]{ "She looked a little startled,
    " + "but after just a while
    " + "she made sure we were occupied
    " + "and cracked a wicked smile.
    " }); model.addRow(new String[]{ "Her body started swaying.
    " + "Her toes began to tap.
    " + "She started grooving in her seat
    " + "and rocking to the rap." }); model.addRow(new String[]{ "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 GridBagLayout()); JTable table = new JTable(model); table.setRowHeight(75); add(new JScrollPane(table)); } } }

    Maybe there's something else in your code, which you're not showing us, which is causing the problem...

    Updated with "dynamic" example

    This is going past the original question, BUT, the TableModel represents the data it's backing, it provides the structure for the JTable to show it.

    So, given a bunch of "disconnected" values, it's the TableModel which is going to "sew" them together, based on your requirements.

    The following example simple splits each line of the previous poem in a an array, when each line represents a element.

    This is then wrapped again so each section of the poem is an array of lines...

    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...
    

    The example then uses a custom TableModel, which when asked for the value of the cell, takes the given "section" and builds a String out of each line, wrapping into a based String.

    Further, you need to click the Add button to add each new line before it can be displayed

    Dynamic data

    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 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("
    ", "", ""); for (String text : data) { joiner.add(text); } value = joiner.toString(); break; } return value; } @Override public int getRowCount() { return rowData.size(); } } }

    Take a look at How to Use Tables for more details

提交回复
热议问题