Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Swing / AWT / SWT
JTable+JPanel Parent Table and Child Table
narender reddy
Greenhorn
Posts: 22
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
hi all i want the program on :how should we create the parent table if we want to click in the table anywhere it opens the new table/window with that related fields in the table
Thank you everybody. Much appreciated.
Craig Wood
Ranch Hand
Posts: 1535
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Here is the general idea.
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.AbstractTableModel; public class ClickAndShow { private JScrollPane getContent() { JTable table = new JTable(getModel()); table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); Dimension d = table.getPreferredSize(); d.width = 400; table.setPreferredScrollableViewportSize(d); table.addMouseListener(ml); return new JScrollPane(table); } private MouseListener ml = new MouseAdapter() { public void mousePressed(MouseEvent e) { JTable table = (JTable)e.getSource(); int row = table.rowAtPoint(e.getPoint()); int colCount = table.getColumnCount(); Object[] colNames = new Object[colCount]; Object[][] rowData = new Object[1][colCount]; for(int j = 0; j < colCount; j++) { colNames[j] = table.getColumnName(j); rowData[0][j] = table.getValueAt(row, j); } showRowData(rowData, colNames); } }; private void showRowData(final Object[][] data, final Object[] ids) { SwingUtilities.invokeLater(new Runnable() { public void run() { JTable table = new JTable(data, ids); Dimension d = table.getPreferredSize(); table.setPreferredScrollableViewportSize(d); JScrollPane scrollPane = new JScrollPane(table); // You could use a non-modal JDialog instead of this. JOptionPane.showMessageDialog(null, scrollPane, "selected data", JOptionPane.PLAIN_MESSAGE); } }); } private AbstractTableModel getModel() { return new AbstractTableModel() { public int getColumnCount() { return 3; } public int getRowCount() { return 6;} public String getColumnName(int col) { return "Column " + (col+1); } public Object getValueAt(int row, int col) { return String.valueOf(row + 1) + (col + 1); } }; } public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setContentPane(new ClickAndShow().getContent()); f.pack(); f.setLocation(200,200); f.setVisible(true); } }
Consider Paul's
rocket mass heater
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
ASAP
XML for Assignment Log
iBatis resultMap groupBy Question
its very very urgent
CMP Entity Beans Vs BMP Entity Beans
More...