简体   繁体   中英

how to add jtable and Jtextpane in single frame in java?

I want to show jtable containing my course information,it is working fine as i have showed a seperate jtable.... Now the problem is that i want to show jtable(containing my course info)on left side,along with JtextPane in a single frame on right side so that user can select item from jtable and paste it in right side(JTextPane) in java........... i dont know how to do this...... Any help would be appreciated....

Thanks in Advance

From the article How to Use Tables , I'd start with the TableSelectionDemo . It shows how to update a JTextArea in response to a ListSelectionEvent .

For example:

public class ListTest extends JPanel{

private JTable table;
private String COLUMN1 = "COLUMN1";
private JTextArea myTA;

public ListTest() {

    table = new JTable(new Object[][]{{"1"}, {"2"}}, new Object[]{COLUMN1});

    TableColumn col = table.getColumn(COLUMN1);
    col.setIdentifier(COLUMN1);
    col.setHeaderValue("Data");

    table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){

        @Override
        public void valueChanged(ListSelectionEvent e){
            if (!e.getValueIsAdjusting()){
                int selRow = table.getSelectedRow();

                final Object data = selRow >= 0 ? table.getModel().getValueAt(selRow, 0) : null;
                SwingUtilities.invokeLater(new Runnable(){

                    @Override
                    public void run(){
                        if (data != null){
                            myTA.setText(data.toString());
                        }
                        else{
                            myTA.setText("");
                        }

                    }
                });
            }
        }
    });

    setLayout(new BorderLayout());

    JScrollPane scroll = new JScrollPane(table);
    scroll.setPreferredSize(new Dimension(50, 200));
    add(scroll, BorderLayout.WEST);
    add(new JScrollPane(myTA = new JTextArea()), BorderLayout.CENTER);

}

public static void main(String[] args){
    JFrame frame = new JFrame("test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ListTest listTest = new ListTest();
    // Add content to the window.
    frame.add(listTest);

    // Display the window.
    frame.pack();
    frame.setSize(400, 200);
    frame.setVisible(true);
}
}

An advice: read the excellent Using Swing Components tutorial, there are the responses to almost every basic question and a lot of examples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM