I want to show jtable containing my course information,it is working fine as i have showed a seperate jtable.... Now the 开发者_如何学Cproblem 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.
精彩评论