Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import javax.swing.border.EmptyBorder;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import java.awt.BorderLayout;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JTable;
- import javax.swing.ListSelectionModel;
- import javax.swing.table.TableCellRenderer;
- import javax.swing.UIManager;
- public class mainGUI extends JFrame{
- JPanel panel;
- JTable table;
- Object[][] list;
- public mainGUI(boolean example){
- try{
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- }catch(Exception e){
- //Do nothing
- }
- panel = new JPanel(new BorderLayout());
- update();
- panel.add(new JScrollPane(table));
- panel.setBorder(new EmptyBorder(2, 2, 2, 2));
- this.add(panel, BorderLayout.CENTER);
- this.setDefaultCloseOperation(EXIT_ON_CLOSE);
- this.setTitle("Example");
- this.setSize(200,300);
- this.setVisible(true);
- if(example){ action(2); } //Example
- }//End of constructor
- private JTable buildTable(){
- String[] columns = {"Name", "Number", "Action"};
- list = new Object[5][3]; //Assume there is 5 values on the list
- list[0][0] = "Fred";
- list[1][0] = "Allison";
- list[2][0] = "John";
- list[3][0] = "Kelly";
- list[4][0] = "Jamie";
- for(int i = 0; i < 5; i++){
- list[i][1] = Integer.toString((int)(Math.random() * 100 + 1));
- if(i % 2 == 0){
- list[i][2] = new JButton("Activate Me");
- }else{
- list[i][2] = new JButton("Activate!");
- }
- }//End of for loop
- //JTable
- JTable table = new JTable(new customTable(list, columns));
- //Column Buttons
- TableCellRenderer buttonRenderer = new JTableButtonRenderer();
- table.getColumn("Action").setCellRenderer(buttonRenderer);
- table.addMouseListener(new JTableButtonMouseListener(table));
- //Misc
- table.setFillsViewportHeight(true);
- table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
- //Return
- return table;
- }//End of buildTable method
- private void update(){
- table = buildTable();
- panel.removeAll();
- panel.updateUI();
- panel.add(new JScrollPane(table));
- } //End of update method
- public void action(int selection){
- String message = list[selection][0] + " has number of " + list[selection][1];
- JOptionPane.showMessageDialog(this, message);
- update();
- }//end of action method
- public static void main(String[] args){
- boolean example = false;
- if(args.length != 0){
- example = true;
- }
- new mainGUI(example);
- }//End of main method
- } //End of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement