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
Tim Cooke
paul wheaton
Jeanne Boyarsky
Ron McLeod
Sheriffs:
Paul Clapham
Liutauras Vilda
Devaka Cooray
Saloon Keepers:
Tim Holloway
Roland Mueller
Bartenders:
Forum:
Swing / AWT / SWT
JTable
Prashanth Bhanu
Ranch Hand
Posts: 110
posted 20 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi Friends,
I've a JTable and i want to show some of the rows and want to hide some.
What technic i've to use to hide those rows?.How do i achive this?.Can any body give me a tip on this?.
Thanks in advance
Prashu
Anand Karia
Ranch Hand
Posts: 158
I like...
posted 20 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
i think, RowHeight would be the solution for this.
Love is GOD and GOD is Love.
Anand Karia
IT Concretor.......
M/s. Anand Karia Concreting IT
Craig Wood
Ranch Hand
Posts: 1535
posted 20 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.table.*; public class RowVisibility { public RowVisibility() { JTable table = new JTable(new VisibleTableModel()); VisibilityController controller = new VisibilityController(table); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(controller.getUIPanel(), "North"); f.add(new JScrollPane(table)); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } public static void main(String[] args) { new RowVisibility(); } } class VisibleTableModel extends AbstractTableModel { int cols, rows; String[] headers; String[][] data, original; public VisibleTableModel() { cols = 4; rows = 32; headers = new String[]{ "column 1", "column 2", "column 3", "column 4" }; createData(); original = data; } public Class getColumnClass(int col) { return data[0][col].getClass(); } public String getColumnName(int col) { return headers[col]; } public int getColumnCount() { return cols; } public int getRowCount() { return rows; } public Object getValueAt(int row, int col) { return data[row][col]; } public void removeRow(int index) { String[][] temp = new String[--rows][cols]; for(int row = 0, newRow = 0; row < data.length; row++, newRow++) { if(row == index) { newRow--; // skip this row continue; } for(int col = 0; col < cols; col++) temp[newRow][col] = data[row][col]; } data = temp; fireTableRowsDeleted(index, index); } public void insertRow(int origRowIndex, int insertionIndex) { rows++; String[][] temp = new String[data.length + 1][cols]; System.arraycopy(data, 0, temp, 0, insertionIndex); System.arraycopy(original, origRowIndex, temp, insertionIndex, 1); System.arraycopy(data, insertionIndex, temp, insertionIndex + 1, data.length - insertionIndex); //printArray(temp); data = temp; fireTableRowsInserted(insertionIndex, insertionIndex); } private void printArray(Object[][] o) { for(int row = 0; row < o.length; row++) { for(int col = 0; col < o[0].length; col++) System.out.print(" " + o[row][col] + ", "); System.out.println(); } } private void printArray(Object[] o) { for(int col = 0; col < o.length; col++) System.out.print(o[col] + ", "); System.out.println(); } private void createData() { data = new String[rows][cols]; for(int row = 0; row < rows; row++) for(int col = 0; col < cols; col++) data[row][col] = "item " + (row * cols + col + 1); } } class VisibilityController { JTable table; VisibleTableModel model; JComboBox hide, show; public VisibilityController(JTable table) { this.table = table; model = (VisibleTableModel)table.getModel(); } private void hideRow(int rowItem, int rowIndex) { model.removeRow(rowIndex); hide.removeItem(rowItem); int i = findInsertionIndex(show, rowItem); show.insertItemAt(rowItem, i); } private void showRow(int origIndex) { int i = findInsertionIndex(hide, origIndex); model.insertRow(origIndex, i); show.removeItem(origIndex); hide.insertItemAt(origIndex, findInsertionIndex(hide, origIndex)); } private int findInsertionIndex(JComboBox cb, int item) { for(int j = 0; j < cb.getItemCount(); j++) { if(item < ((Integer)cb.getItemAt(j)).intValue()) return j; } return cb.getItemCount(); } public JPanel getUIPanel() { int rows = model.getRowCount(); hide = new JComboBox(); show = new JComboBox(); for(int j = 0; j < rows; j++) hide.addItem(j); show.setPreferredSize(hide.getPreferredSize()); ActionListener l = new ActionListener() { public void actionPerformed(ActionEvent e) { JComboBox combo = (JComboBox)e.getSource(); int selectedItem = ((Integer)combo.getSelectedItem()).intValue(); if(combo == hide) hideRow(selectedItem, combo.getSelectedIndex()); if(combo == show) showRow(selectedItem); } }; hide.addActionListener(l); show.addActionListener(l); JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(2,2,2,2); gbc.weightx = 1.0; addComponents(new JLabel("hide row"), hide, panel, gbc); addComponents(new JLabel("show row"), show, panel, gbc); return panel; } private void addComponents(JLabel label, JComboBox comboBox, JPanel panel, GridBagConstraints gbc) { gbc.anchor = gbc.EAST; panel.add(label, gbc); gbc.anchor = gbc.WEST; panel.add(comboBox, gbc); } }
I think I'll just lie down here for a second. And ponder this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
JTable
JTable ........
JTable
JTable
JTable(!!!)
More...