简体   繁体   中英

swing scrollpane setting to textpane

below is my code that are adding few swing components to frame.I m using two textpane and setting some text to both.But text is large and only textpane is visible when i run the code.so i tried to add scrollpane to textpane ta2 but then also nothing happens.scrollpane doesnot appear around textpane ta2.What is the mistake

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;

public class Test1 {

    public Test1() {
        String a="vdnogregnroei dfnoj";
         JFrame frm = new JFrame("frontend");
         JLabel l1= new JLabel("Enter name of text file");
         JLabel l2= new JLabel("Enter name of text file");
         final JTextField t1=new JTextField(15);
         final JTextField t2=new JTextField(15);
         JTextPane ta2=new JTextPane();
         JLabel l3=new JLabel("SIMILARITY");
         JLabel  l4=new JLabel("DIFFERENCES");
          JTextPane ta1=new JTextPane();
          JScrollPane sp2=new JScrollPane(ta2);
          frm.getContentPane().add(sp2);
         JButton b1=new JButton("COMPARE");
         frm.setLayout(new GridBagLayout());
         Container cont=frm.getContentPane();
         GridBagConstraints cnt=new GridBagConstraints();
         cnt.fill=GridBagConstraints.HORIZONTAL;
         cnt.insets=new Insets(10,10,10,10);
         cnt.gridx=1;
         cnt.gridy=1;
         cont.add(l1,cnt);
         cnt.gridx=2;
         cnt.gridy=1;
         cont.add(t1,cnt);
         cnt.gridx=1;
         cnt.gridy=2;
         cont.add(l2,cnt);
         cnt.gridx=2;
         cnt.gridy=2;
         cont.add(t2,cnt);
         cnt.gridx=1;
         cnt.gridy=3;
         cont.add(l3,cnt);
         cnt.gridx=2;
         cnt.gridy=3;
         cont.add(ta1,cnt);
         cnt.gridx=1;
         cnt.gridy=4;
         cont.add(l4,cnt);
         cnt.gridx=2;
         cnt.gridy=4;
         cont.add(ta2,cnt);
         cnt.gridx=1;
         cnt.gridy=5;
         cont.add(b1,cnt);
         ta1.setContentType("text/html");
         ta1.setText("sbdiu sdjj<b>bjksd</b>"+a+"<br/>dnsaod<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>vsdnono");

         ta2.setContentType("text/html");
         ta2.setText("sbdiu sdjj<b>bjksd</b>"+a+"<br/>dnsaod<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>vsdnono");
         frm.pack();
         frm.setVisible(true);
    }
    public static void main(String[] args)
    {
        Test1 obj=new Test1();
    }

}

You need to add the JTextPane to the JScrollPane and then add the JScrollPane to the JFrame (or its content pane):

public class Test1 {

    public Test1() {

        JFrame frm = new JFrame("frontend");

        JLabel l1 = new JLabel("Enter name of text file");
        JLabel l2 = new JLabel("Enter name of text file");
        JLabel l3 = new JLabel("SIMILARITY");
        JLabel l4 = new JLabel("DIFFERENCES");

        JTextField t1 = new JTextField(15);
        JTextField t2 = new JTextField(15);

        JTextPane ta1 = new JTextPane();
        JTextPane ta2 = new JTextPane();

        ta1.setContentType("text/html");
        ta2.setContentType("text/html");
        String a = "vdnogregnroei dfnoj";
        ta1.setText(a);
        ta2.setText(a);

        JScrollPane sp1 = new JScrollPane(ta1);
        JScrollPane sp2 = new JScrollPane(ta2);
        JButton b1 = new JButton("COMPARE");

        Container cont = frm.getContentPane();
        cont.setLayout(new GridBagLayout());
        GridBagConstraints cnt = new GridBagConstraints();

        cnt.fill = GridBagConstraints.HORIZONTAL;
        cnt.insets = new Insets(10, 10, 10, 10);
        cnt.gridx = 0;
        cnt.gridy = 0;
        cont.add(l1, cnt);

        cnt.gridx = 1;
        cnt.gridy = 0;
        cont.add(t1, cnt);

        cnt.gridx = 0;
        cnt.gridy = 1;
        cont.add(l2, cnt);

        cnt.gridx = 1;
        cnt.gridy = 1;
        cont.add(t2, cnt);

        cnt.gridx = 0;
        cnt.gridy = 2;
        cont.add(l3, cnt);

        cnt.gridx = 1;
        cnt.gridy = 2;
//      cnt.weightx = 1;
//      cnt.weighty = 1;
        cont.add(sp1, cnt);

        cnt.gridx = 0;
        cnt.gridy = 3;
//      cnt.weightx = 0;
//      cnt.weighty = 0;
        cont.add(l4, cnt);

        cnt.gridx = 1;
        cnt.gridy = 3;
        cont.add(sp2, cnt);

        cnt.gridx = 0;
        cnt.gridy = 4;
        cont.add(b1, cnt);

        frm.pack();
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.setVisible(true);
    }

    public static void main(String[] args) {

        new Test1();
    }
}

I will advise you to look carefully at the differences between this code and yours. Notice where I set the layout and what components I add.

Notes:

  • I commented out some lines that hint you about resizing behavior.
  • You probably want to call setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) on the frame.
  • The grid starts from x=0 and y=0 , not x=1 and y=1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM