fubarable

Swing Layout

Apr 26th, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.68 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.EventQueue;
  3. import javax.swing.JFrame;
  4. import javax.swing.JPanel;
  5. import javax.swing.border.EmptyBorder;
  6. import javax.swing.JLabel;
  7. import java.awt.Font;
  8. import java.awt.SystemColor;
  9. import java.util.Vector;
  10. import javax.swing.JTable;
  11. import javax.swing.border.LineBorder;
  12. import java.awt.Color;
  13. import javax.swing.ListSelectionModel;
  14. import javax.swing.SwingConstants;
  15. import java.awt.Dimension;
  16. import javax.swing.JScrollPane;
  17.  
  18. @SuppressWarnings("serial")
  19. public class RevisonList3 extends JFrame {
  20.  
  21.     protected static final int PREF_W = 1400;
  22.     protected static final int PREF_H = 500;
  23.     private JPanel contentPane;
  24.     private JScrollPane scrollPane;
  25.  
  26.     public static void main(String[] args) {
  27.         EventQueue.invokeLater(new Runnable() {
  28.             public void run() {
  29.                 try {
  30.                     RevisonList3 frame = new RevisonList3();
  31.                     frame.pack();
  32.                     frame.setLocationRelativeTo(null);
  33.                     frame.setVisible(true);
  34.                 } catch (Exception e) {
  35.                     e.printStackTrace();
  36.                 }
  37.             }
  38.         });
  39.     }
  40.  
  41.     public RevisonList3() {
  42.         setTitle("SVN Connector");
  43.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  44.         contentPane = new JPanel(){
  45.             @Override
  46.             public Dimension getPreferredSize() {
  47.                 if (isPreferredSizeSet()) {
  48.                     return super.getPreferredSize();
  49.                 }
  50.                 return new Dimension(PREF_W, PREF_H);
  51.             }
  52.  
  53.         };
  54.         contentPane.setBackground(Color.LIGHT_GRAY);
  55.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  56.         setContentPane(contentPane);
  57.         contentPane.setLayout(new BorderLayout());
  58.  
  59.         JLabel label = new JLabel("    SVN Connect", SwingConstants.CENTER);
  60.         label.setForeground(SystemColor.controlHighlight);
  61.         label.setFont(new Font("Lucida Grande", Font.BOLD, 14));
  62.         contentPane.add(label, BorderLayout.PAGE_START);
  63.  
  64.         scrollPane = new JScrollPane();
  65.         scrollPane.setViewportBorder(new LineBorder(new Color(0, 0, 0)));
  66.         contentPane.add(scrollPane, BorderLayout.CENTER);
  67.  
  68.         String[] columnNames = { "FileName", "Revision", "Author", "Date Modified", "URL" };
  69.  
  70.         Vector<Vector<Integer>> data = new Vector<>();
  71.         for (int i = 0; i < 130; i++) {
  72.             Vector<Integer> row = new Vector<>();
  73.             row.addElement(i);
  74.             for (int j = 0; j < columnNames.length - 1; j++) {
  75.                 row.addElement((int) (100 * Math.random()));
  76.             }
  77.             data.add(row);
  78.         }
  79.  
  80.         Vector<String> colNamesVector = new Vector<>();
  81.         for (String object : columnNames) {
  82.             colNamesVector.add(object);
  83.         }
  84.  
  85.         JTable table = new JTable(data, colNamesVector);
  86.  
  87.         table.setBackground(Color.LIGHT_GRAY);
  88.         table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  89.         table.setBorder(new LineBorder(Color.BLACK));
  90.         table.getColumnModel().getColumn(0).setMaxWidth(150);
  91.         table.getColumnModel().getColumn(1).setMaxWidth(55);
  92.         table.getColumnModel().getColumn(2).setMaxWidth(77);
  93.         table.getColumnModel().getColumn(3).setMaxWidth(220);
  94.         table.getColumnModel().getColumn(0).setMinWidth(150);
  95.         table.getColumnModel().getColumn(1).setMinWidth(55);
  96.         table.getColumnModel().getColumn(2).setMinWidth(77);
  97.         table.getColumnModel().getColumn(3).setMinWidth(220);
  98.         table.getColumnModel().getColumn(4).setMinWidth(850);
  99.         scrollPane.setViewportView(table);
  100.     }
  101. }
Add Comment
Please, Sign In to add comment