Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. import javax.swing.*;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.Dimension;
  6. import java.awt.GridBagConstraints;
  7. import java.awt.GridBagLayout;
  8. import java.awt.GridLayout;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.ActionListener;
  11. import java.awt.event.WindowAdapter;
  12. import java.awt.event.WindowEvent;
  13.  
  14.  
  15. public class MyGui1 extends JPanel implements ActionListener {
  16. /**
  17. *
  18. */
  19. static JScrollPane jsp;
  20. protected JPanel panel;
  21. protected JTextArea textarea;
  22. static JFrame frame;
  23. JLabel Jlabel=null;
  24.  
  25. public MyGui1() {
  26. //to lay out the container's components in a rectangular grid
  27. super(new GridBagLayout());
  28.  
  29. GridBagConstraints c = new GridBagConstraints();
  30. c.gridwidth = GridBagConstraints.REMAINDER;
  31.  
  32. c.fill = GridBagConstraints.HORIZONTAL;
  33.  
  34. JButton jbutton=new JButton("button");
  35. jbutton.setActionCommand("button");
  36. jbutton.addActionListener(this);
  37. add(jbutton,c);
  38.  
  39. c.gridy=1;
  40. c.gridwidth = GridBagConstraints.REMAINDER;
  41. c.fill = GridBagConstraints.BOTH;
  42.  
  43. c.weightx = 1.0;
  44. c.weighty = 1.0;
  45. setBackground(Color.cyan);
  46. panel=new JPanel(new GridLayout(0,1));
  47. jsp=new JScrollPane(panel);
  48.  
  49. jsp.setPreferredSize(new Dimension(300,300));
  50.  
  51. jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  52. jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
  53.  
  54. add(jsp, c);
  55. setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
  56.  
  57. }
  58.  
  59. public void actionPerformed(ActionEvent evt) {
  60.  
  61. if("button".equals(evt.getActionCommand())){
  62. execute();
  63. }
  64. }
  65.  
  66. synchronized public void execute(){
  67.  
  68. // to remove all the content of panel
  69. panel.removeAll();
  70. // to refresh the window
  71.  
  72. for(int i=0;i<20 ;i++){
  73.  
  74.  
  75. Jlabel=new JLabel("Labe1l"+i);
  76. Jlabel.setVerticalAlignment(SwingConstants.TOP);
  77. panel.add(Jlabel);
  78.  
  79. textarea=new JTextArea();
  80. textarea.setText("sample text");
  81. if(i==2){
  82. textarea.setText("sample text........................nsample text.................. ");
  83. }
  84. if(i==5){
  85. textarea.setText("sample text.nsample text.sample text.nsample text. ");
  86. }
  87. if(i==7){
  88. textarea.setText("sample text.nsample text.sample text.nsample text sample text..nsample text.sample text..nsample text. ");
  89. }
  90. textarea.append("n");
  91. textarea.setEditable(false);
  92. // in order to wrap up the data in text area
  93. textarea.setLineWrap(true);
  94. panel.add(textarea ,BorderLayout.CENTER);
  95.  
  96.  
  97. }
  98. jsp.revalidate();
  99. jsp.repaint();
  100. frame.repaint();
  101. frame.revalidate();
  102.  
  103.  
  104. javax.swing.SwingUtilities.invokeLater(new Runnable() {
  105. public void run() {
  106.  
  107. jsp.getVerticalScrollBar().setValue(0);
  108. jsp.validate();
  109.  
  110. }
  111. }); }
  112. @SuppressWarnings("static-access")
  113. private static void createAndShowGUI() {
  114. //Create and set up the window.
  115. frame = new JFrame("DesktopSearchEngine");
  116. frame.setLayout(new BorderLayout());
  117. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  118.  
  119. //adding window listener for exit operation
  120. frame.addWindowListener( new WindowAdapter()
  121. {
  122. public void windowClosing(WindowEvent e)
  123. {
  124. JFrame frame = (JFrame)e.getSource();
  125.  
  126. int result = JOptionPane.showConfirmDialog(
  127. frame,
  128. "Are you sure you want to exit the application?",
  129. "Exit Application",
  130. JOptionPane.YES_NO_OPTION);
  131.  
  132. if (result == JOptionPane.YES_OPTION)
  133. {
  134.  
  135. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  136. }
  137. else if(result==JOptionPane.NO_OPTION){
  138. frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  139. }
  140. }
  141. });
  142. //Add contents to the window GUI part and to perform all operations
  143. frame.add(new MyGui1());
  144.  
  145. //Display the window.
  146. frame.pack();
  147. //to keep the frame visible
  148. frame.setVisible(true);
  149.  
  150. }
  151.  
  152. public static void main(String[] args) {
  153. //Schedule a job for the event dispatch thread:
  154. //creating and showing this application's GUI.
  155. javax.swing.SwingUtilities.invokeLater(new Runnable() {
  156. public void run() {
  157. createAndShowGUI();
  158.  
  159.  
  160. }
  161. });
  162. }
  163.  
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement