Sunday, July 7, 2013
Wednesday, July 3, 2013
How to add scroll bar to your JScrollPane
import javax.swing.*; import javax.swing.table.DefaultTableModel; /** * * @author Lee */ public class MyTableFrame extends JPanel { public MyTableFrame() { Object[][] data = { {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null}, }; String[] columnNames = new String[data[0].length]; for(int i=0; i<data[0].length; i++) { columnNames[i] = "column: " + i; } DefaultTableModel myModel = new DefaultTableModel(data, columnNames); JTable myTable = new JTable(myModel); // Here, we add vertial scroll bar and horizontal scroll bar // VERTICAL_SCROLLBAR_ALWAYS could be VERTIAL_SCROLLBAR_AS_NEEDED // same for horizontal bar JScrollPane myScrollPane = new JScrollPane( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); myTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); myScrollPane.getViewport().add(myTable); this.add(myScrollPane); } private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("SimpleTableDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane. MyTableFrame newContentPane = new MyTableFrame(); newContentPane.setOpaque(true); //content panes must be opaque frame.setContentPane(newContentPane); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } };
Subscribe to:
Posts (Atom)