Results 1 to 13 of 13
Thread: JScrollPane
- 08-15-2010, 02:59 PM #1
Senior Member
- Join Date
- Apr 2010
- Location
- Dhaka,Bangladesh
- Posts
- 178
- Rep Power
- 0
JScrollPane
I am trying too much to add a scrollbar on the jtextpane2(JTextPane class).But it is not running.Please help me by checking it.
import javax.swing.*;
import java.awt.*;
//starting main class
public class chat extends JApplet
{
Font font = new Font("Verdana", Font.ITALIC,30);
static JButton write;
static JButton browse;
static JButton word;
static JButton sentence;
static Container pane;
static JFrame frmMain;
static JScrollPane pan1;
static JScrollPane pan2;
static JLabel label;
private JTextPane jtextfield1;
private JTextPane jtextfield2;
private JTextPane jtextfield3;
private JTextPane jtextfield4;// enters information from user
chat(){
frmMain = new JFrame ("Compare suite"); //Create frame
frmMain.setSize(1000, 700); //Set size to 400x400 pixels
pane = frmMain.getContentPane(); //Get content pane
pane.setLayout(null); //Apply null layout
frmMain.setDefaultCloseOperation(JFrame.EXIT_ON_CL OSE);
//Create controls
browse = new JButton ("Browse");
write = new JButton ("Write");
word = new JButton ("Word");
sentence = new JButton ("Sentence");
jtextfield1=new JTextPane();
jtextfield2=new JTextPane();
jtextfield3=new JTextPane();
jtextfield4=new JTextPane();
label=new JLabel("Result");
////////////////See here///////scrollpane is nt working///////////
pan1=new JScrollPane();
pan2=new JScrollPane();
pan2.setViewportView(jtextfield3);
pan1.setViewportView(jtextfield2);
///////////////////////////////////////
frmMain.add(browse);
frmMain.add(word);
frmMain.add(write);
frmMain.add(sentence);
frmMain.add(jtextfield1);
frmMain.add(jtextfield2) ;
frmMain.add(jtextfield3);
frmMain.add(jtextfield4);
frmMain.add(label);
////////////////////
frmMain.add(pan1);
frmMain.add(pan2);
/////////////////////
write.setBounds(10,30,100,50);
browse.setBounds(130,30, 100, 50);
word.setBounds(250,30, 100, 50);
sentence.setBounds(370,30, 100, 50);
jtextfield1.setBounds(490,40,400,25);
jtextfield2.setBounds(10,100,700,450);
jtextfield3.setBounds(730,100,250,450);
jtextfield4.setBounds(300,580,600,80);
label.setBounds(300,520,100,90);
//Make frame visible
}
public static void main (String args[]){
//Look and feel
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAn dFeelClassName());
}
catch (ClassNotFoundException e) {}
catch (InstantiationException e) {}
catch (IllegalAccessException e) {}
catch (UnsupportedLookAndFeelException e) {}
//Prepare frame
chat ak=new chat();
frmMain.setResizable(false);
frmMain.setVisible(true);
}
}////end of class chat
Please help me..............
-
Ujjal, after 36 posts here, you should know by now to use code tags when posting code. Please edit your initial post and add tags. Luck.
-
As to your problem, well you're adding your JTextPane to the application, but you're also adding the JTextPane directly to the application too, and you can't do that. That and you never give the JScrollPane any size.
Other major problems with your code:
1. Unnecessary and in fact bad use of static
2. No use of layout managers and over-reliance on null layout.
3. Having class extend JApplet when it's not an applet but rather is a stand-alone application.
4. Setting bounds of a component being placed in a JScrollPane rather than its preferredSize.
- 08-15-2010, 03:51 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 08-15-2010, 04:40 PM #5
Senior Member
- Join Date
- Apr 2010
- Location
- Dhaka,Bangladesh
- Posts
- 178
- Rep Power
- 0
@Fubarable,
I am sorry but I have tagged where is my problem.
Now will you please explain Number 4.
- 08-15-2010, 04:46 PM #6
Senior Member
- Join Date
- Apr 2010
- Location
- Dhaka,Bangladesh
- Posts
- 178
- Rep Power
- 0
@Fubarable,
I am sorry but I have tagged where is my problem.
Now will you please explain Number 4?
-
I think you misunderstand me as I was referring to code tags, tags that allow your code to retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?
To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.
Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:
Java Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
The JScrollPane will just ignore the component's bounds, so if you want to set the size of a component that is being placed in a JScrollPane's viewport, you need to set its preferredSize by calling setPreferredSize(new Dimension(..., ...)) on the component.Now will you please explain Number 4?
-
For instance with code tags, your post above looks like:
You can still edit your original post and add these puppies... (just a suggestion)Java Code:import javax.swing.*; import java.awt.*; //starting main class public class chat extends JApplet { Font font = new Font("Verdana", Font.ITALIC,30); static JButton write; static JButton browse; static JButton word; static JButton sentence; static Container pane; static JFrame frmMain; static JScrollPane pan1; static JScrollPane pan2; static JLabel label; private JTextPane jtextfield1; private JTextPane jtextfield2; private JTextPane jtextfield3; private JTextPane jtextfield4;// enters information from user chat(){ frmMain = new JFrame ("Compare suite"); //Create frame frmMain.setSize(1000, 700); //Set size to 400x400 pixels pane = frmMain.getContentPane(); //Get content pane pane.setLayout(null); //Apply null layout frmMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create controls browse = new JButton ("Browse"); write = new JButton ("Write"); word = new JButton ("Word"); sentence = new JButton ("Sentence"); jtextfield1=new JTextPane(); jtextfield2=new JTextPane(); jtextfield3=new JTextPane(); jtextfield4=new JTextPane(); label=new JLabel("Result"); //....
- 08-16-2010, 04:49 PM #9
Senior Member
- Join Date
- Apr 2010
- Location
- Dhaka,Bangladesh
- Posts
- 178
- Rep Power
- 0
Here is my code where I am trying to add a JScrollPane.Inspite of trying much,I am unable to add it.Will you check this code and please tell where should I edit this code?
Code:
Java Code:import javax.swing.*; import java.awt.*; public class chat extends JApplet { Font font = new Font("Verdana", Font.ITALIC,30); JScrollPane pan1; JScrollPane pan2; JButton write; JButton browse; JButton word; JButton sentence; Container pane; JFrame frmMain; JLabel label; JTextPane jtextfield1; JTextPane jtextfield2; JTextPane jtextfield3; JTextPane jtextfield4;// enters information from user // static JPanel pnl; chat(){ frmMain = new JFrame ("Compare suite"); //Create frame frmMain.setSize(1000, 700); //Set size to 400x400 pixels pane = frmMain.getContentPane(); //Get content pane frmMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Close when X is clicked //Create controls browse = new JButton ("Browse"); write = new JButton ("Write"); word = new JButton ("Word"); sentence = new JButton ("Sentence"); jtextfield1=new JTextPane(); jtextfield2=new JTextPane(); jtextfield3=new JTextPane(); jtextfield4=new JTextPane(); label=new JLabel("Result"); pan1=new JScrollPane(jtextfield2); pan2=new JScrollPane( jtextfield3); frmMain.add(browse); frmMain.add(word); frmMain.add(write); frmMain.add(sentence); frmMain.add(jtextfield1); frmMain.add(jtextfield2) ; frmMain.add(jtextfield3); frmMain.add(jtextfield4); frmMain.add(label); frmMain.add(pan1); frmMain.add(pan2); write.setBounds(10,30,100,50); browse.setBounds(130,30, 100, 50); word.setBounds(250,30, 100, 50); sentence.setBounds(370,30, 100, 50); jtextfield1.setBounds(490,40,400,25); jtextfield2.setBounds(10,100,700,450); jtextfield3.setBounds(730,100,250,450); jtextfield4.setBounds(300,580,600,80); label.setBounds(300,520,100,90); frmMain.setResizable(false); frmMain.setVisible(true); }//end of constructor chat public static void main (String args[]){ try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException e) {} catch (InstantiationException e) {} catch (IllegalAccessException e) {} catch (UnsupportedLookAndFeelException e) {} //Prepare frame chat ak=new chat(); }//end of main }//end of chat
-
Please re-read my second post in this thread. You're still adding your components twice -- in the scrollpane and again in the application. You're still not setting the preferredSize of the scrolled component. Also read the Sun Swing tutorial section on layouts and all the other swing tutorials. As in most endeavors, you should study your tools before using them.
- 08-17-2010, 04:23 PM #11
Senior Member
- Join Date
- Apr 2010
- Location
- Dhaka,Bangladesh
- Posts
- 178
- Rep Power
- 0
@Fubarable,
I understand what you are talking.But I am beginner of GUI.Please compile and run the code of my last post and help me telling where should I change.
I added components in two times:
1. pan1=new JScrollPane(jtextfield2);
2. jtextfield2.setBounds(10,100,700,450);
I am trying too much but can't do nothing.I have made JScrollpane in another code but here is being nothing.Here is the code.
Code:
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class ScrollDemo extends JPanel { JTextPane text=new JTextPane(); public ScrollDemo() { JScrollPane pictureScrollPane = new JScrollPane(text); pictureScrollPane.setPreferredSize(new Dimension(300, 250)); pictureScrollPane.setViewportBorder(BorderFactory.createLineBorder(Color.black)); // Put it in this panel setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); add(pictureScrollPane); setBorder(BorderFactory.createEmptyBorder(60,20,40,20)); } public static void main(String s[]) { JFrame frame = new JFrame("ScrollDemo"); frame.setContentPane(new ScrollDemo()); frame.pack(); frame.setVisible(true); } }
- 08-17-2010, 05:23 PM #12
Senior Member
- Join Date
- Apr 2010
- Location
- Dhaka,Bangladesh
- Posts
- 178
- Rep Power
- 0
Ok.Thanks to you all.I have solved my problem.
@Fubarable,
Thanks,Now I understand your post.
Let's have look...
Code:
Java Code:public class Compare extends javax.swing.JFrame{ String gh=""; JButton write; JButton browse; JButton word; JButton sentence; JPanel pnl; JFrame frmMain; JLabel label; Container pane; JTextArea jtextpane2; JTextArea jtextpane1; JTextArea jtextpane3; JTextArea jtextpane4; JScrollPane pan1,pan2,pan3,pan4; /** Creates new form Compare */ public Compare() { initComponents(); } private void initComponents() { frmMain = new JFrame ("Compare suite"); //Create frame frmMain.setSize(1000, 700); //Set size to 400x400 pixels pane = frmMain.getContentPane(); //Get content pane //pane.setLayout(null); //Apply null layout frmMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Close when X is clicked //Create controls browse = new JButton ("Browse"); write = new JButton ("Write"); word = new JButton ("Word"); sentence = new JButton ("Sentence"); jtextpane1=new JTextArea(); jtextpane2=new JTextArea(); jtextpane3=new JTextArea(); jtextpane4=new JTextArea(); pan1=new JScrollPane(jtextpane1); pan2=new JScrollPane(jtextpane2); pan3=new JScrollPane(jtextpane3); pan4=new JScrollPane(jtextpane4); label=new JLabel("Result"); pnl = new JPanel(null); pane.add(pnl); pnl.add(browse); pnl.add(word); pnl.add(write); pnl.add(sentence); //pnl.add(jtextpane1);///Now I removed this line and only add the Scrollpane component // pnl.add(jtextpane2) ; // pnl.add(jtextpane3); // pnl.add(jtextpane4); pnl.add(label); pnl.add(pan1); pnl.add(pan2); pnl.add(pan3); pnl.add(pan4); pnl.setBounds(0, 0, 900, 700); write.setBounds(10,30,100,50); browse.setBounds(130,30, 100, 50); word.setBounds(250,30, 100, 50); sentence.setBounds(370,30, 100, 50); // jtextpane1.setBounds(490,40,400,25);//I removed it only bounds the JScrollpane component // jtextpane2.setBounds(10,100,700,450); // jtextpane3.setBounds(730,100,250,450); //jtextpane4.setBounds(300,580,600,80); label.setBounds(300,520,100,90); pan2.setBounds(10,100,700,450); pan1.setBounds(490,40,400,25); pan3.setBounds(730,100,200,450); pan4.setBounds(300,580,600,80);
-
I still strongly recommend that you get out of the habit of using null layout and absolute positioning. What happens if you try to resize your application? What about if later you decide to add a couple of JRadioButtons somewhere in the app? Are you going to want to have to fiddle with all the bounds numbers so that components below and to the right of the radio buttons are shifted over enough but not too much? If you use layout managers, it takes much of that worry out of your hands and handles it for you.
As a quickly made poor example (too many "magic numbers"), see this for instance:
Java Code:import java.awt.BorderLayout; import java.awt.GridLayout; import javax.swing.*; public class Compare2 { private JPanel mainPanel = new JPanel(); private JButton writeBtn = new JButton("Write"); private JButton browseBtn = new JButton("Browse"); private JButton wordBtn = new JButton("Word"); private JButton sentenceBtn = new JButton("Sentence"); private JTextField northField = new JTextField(); // magic numbers below should be changed to constants. private JTextArea centerArea = new JTextArea(30, 65); private JTextArea eastArea = new JTextArea(30, 20); private JTextArea southArea = new JTextArea(6, 80); public Compare2() { int blGap = 20; mainPanel.setLayout(new BorderLayout(blGap, blGap)); int ebGap = 20; mainPanel.setBorder(BorderFactory.createEmptyBorder(ebGap, ebGap, ebGap, ebGap)); mainPanel.add(createNorthPanel(), BorderLayout.NORTH); mainPanel.add(createCenterPanel(), BorderLayout.CENTER); mainPanel.add(createEastPanel(), BorderLayout.EAST); mainPanel.add(createSouthPanel(), BorderLayout.SOUTH); } private JPanel createNorthPanel() { JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 10, 0)); buttonPanel.add(writeBtn); buttonPanel.add(browseBtn); buttonPanel.add(wordBtn); buttonPanel.add(sentenceBtn); JPanel northPanel = new JPanel(); northPanel.setLayout(new BoxLayout(northPanel, BoxLayout.LINE_AXIS)); northPanel.add(buttonPanel); northPanel.add(Box.createHorizontalStrut(25)); northPanel.add(northField); return northPanel; } private JPanel createCenterPanel() { JPanel centerPanel = new JPanel(new BorderLayout()); centerPanel.add(new JLabel("Center Text Area", SwingConstants.LEFT), BorderLayout.NORTH); centerPanel.add(new JScrollPane(centerArea), BorderLayout.CENTER); return centerPanel; } private JPanel createEastPanel() { JPanel eastPanel = new JPanel(new BorderLayout()); eastPanel.add(new JLabel("East Text Area", SwingConstants.LEFT), BorderLayout.NORTH); eastPanel.add(new JScrollPane(eastArea), BorderLayout.CENTER); return eastPanel; } private JPanel createSouthPanel() { JPanel southPanel = new JPanel(new BorderLayout()); southPanel.add(new JLabel("South Text Area", SwingConstants.LEFT), BorderLayout.NORTH); southPanel.add(new JScrollPane(southArea), BorderLayout.CENTER); return southPanel; } public JComponent getPanel() { return mainPanel; } private static void createAndShowGUI() { JFrame frame = new JFrame("Compare2 Application"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new Compare2().getPanel()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
Similar Threads
-
how to add JScrollPane in JTable
By kiki2009 in forum AWT / SwingReplies: 12Last Post: 03-31-2010, 01:54 PM -
Two components in a JScrollPane
By carderne in forum New To JavaReplies: 9Last Post: 09-10-2009, 06:24 AM -
JScrollPane problem
By KArelVH in forum AWT / SwingReplies: 6Last Post: 04-27-2009, 09:40 PM -
jscrollpane
By kaemonsaionji in forum New To JavaReplies: 3Last Post: 02-25-2009, 08:39 AM -
help with JScrollPane
By tommy in forum AWT / SwingReplies: 1Last Post: 08-06-2007, 07:58 PM


LinkBack URL
About LinkBacks


Bookmarks