Sunday, March 13, 2016

Table Maker With HTML

This program utilizes HTML code to create a table. Table rows can be added dynamically by clicking a button.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class TableMaker extends JFrame{
    private JPanel pcontrol=null;
    private JTextField tftitlerow=null;
    private ArrayList<JTextField> alcontentrow=null;
    private JTextField tfcontentrow=null;
    private JButton brow=null;
    private JButton bshow=null;
    private String content=null;
    private JPanel pdraw=null;
    private GridBagConstraints gbc=null;
    
    public TableMaker(){
        super("Table Maker");
        setSize(600,300);
        setLayout(new BorderLayout());
        
        content=new String();
        content="<html><table border=\"1\">";
        
        alcontentrow=new ArrayList<JTextField>();
        
        pcontrol=new JPanel();
        pcontrol.setPreferredSize(new Dimension(200,300));
        pcontrol.setLayout(new GridBagLayout());
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets=new Insets(5,0,0,0);
        tftitlerow=new JTextField();
        tftitlerow.setColumns(12);
        pcontrol.add(tftitlerow,gbc);
        
        brow=new JButton("Add Row");
        brow.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ev) {
                gbc.gridy++;
                alcontentrow.add(new JTextField());
                alcontentrow.get(alcontentrow.size()-1).setColumns(12);
                pcontrol.add(alcontentrow.get(alcontentrow.size()-1),gbc);
                pcontrol.revalidate();
            }
        });
        gbc.gridy++;
        pcontrol.add(brow,gbc);
        
        gbc.gridy++;
        bshow=new JButton("Show");
        bshow.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                content=content+"<tr><td align=\"center\"><b>"+tftitlerow.getText()+"</b></td></tr>";
                for(int i=0;i<alcontentrow.size();i++){
                    content=content+"<tr><td>"+alcontentrow.get(i).getText()+"</td></tr>";
                }
                content=content+"</table></html>";
                pdraw.removeAll();
                JLabel ldraw=new JLabel();
                pdraw.add(ldraw);
                ldraw.setText(content);
            }
        });
        pcontrol.add(bshow,gbc);
        add("West",pcontrol);
        
        pdraw=new JPanel();
        pdraw.setPreferredSize(new Dimension(400,300));
        pdraw.setBackground(Color.WHITE);
        add("Center",pdraw);
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    
    public static void main(String[] args){
        TableMaker app=new TableMaker();
    }
}

No comments:

Post a Comment