Results 1 to 14 of 14
  1. #1
    png
    png is offline Member
    Join Date
    Dec 2009
    Posts
    12
    Rep Power
    0

    Question Problem in adding new JTextPane at Runtime

    Hi,

    I am Creating a Editor Application in Java. Initally this applcation has a JTextPane(name EditorPane) which occupies all of the sapce of JInternalFrame and My maintoolbar also has a button to add the new JTextPane(name headPane) to the JInternalFrame Form at runtime. In the Editor class which extends JInternalFrame class , I am using the following code to add the JTextPane (at initial stage):

    JTextPane HeadPane, EditorPane;
    GroupLayout layout;
    GroupLayout.SequentialGroup hseqgroup;
    GroupLayout.SequentialGroup vseqgroup;

    Editor() // Constructor of my JInternalFrame class
    {
    setOpaque(true);
    setPreferredSize(new java.awt.Dimension(800, 600));
    EditorPane = new javax.swing.JTextPane();
    EditorPane.setOpaque(true);
    EditorPane.setMaximumSize(new Dimension(2147483647,2147483647));
    EditorPane.setMinimumSize(new Dimension(6,21));
    EditorPane.setPreferredSize(new Dimension(6,21));

    layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);

    hseqgroup = layout.createSequentialGroup();
    vseqgroup = layout.createSequentialGroup();
    GroupLayout.ParallelGroup hParallelGroup1 = layout.createParallelGroup
    (GroupLayout.Alignment.LEADING);
    hParallelGroup1.addComponent(EditorPane, javax.swing.GroupLayout.DEFAULT_SIZE, 790,
    Short.MAX_VALUE);

    hseqgroup.addGroup(hParallelGroup1);

    GroupLayout.ParallelGroup vparallelGroup1 = layout.createParallelGroup(GroupLayout.Alignment.L EADING);
    vparallelGroup1.addComponent(EditorPane, javax.swing.GroupLayout.Alignment.TRAILING,
    540, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE);

    vseqgroup.addGroup(vparallelGroup1);
    layout.setHorizontalGroup(hseqgroup);
    layout.setVerticalGroup(vseqgroup);
    pack();
    this.closable = true;
    this.maximizable = true;
    }

    and in the addNewJTextPane Button, I am using the following code:

    HeadPane = new JTextPane();
    HeadPane.setBounds(0, 0, EditorPane.getWidth(), 100);

    HeadPane.setOpaque(true);
    HeadPane.setMaximumSize(new Dimension(2147483647,100));
    HeadPane.setMinimumSize(EditorPane.getMinimumSize( ));
    HeadPane.setPreferredSize(EditorPane.getPreferredS ize());

    GroupLayout.ParallelGroup hparallelGroup1 = layout.createParallelGroup
    (GroupLayout.Alignment.LEADING);
    hparallelGroup1.addComponent(HeadPane, GroupLayout.DEFAULT_SIZE, 790, Short.MAX_VALUE);

    hseqgroup.addGroup(hparallelGroup1);

    GroupLayout.ParallelGroup vparallelGroup1 = layout.createParallelGroup (GroupLayout.Alignment.LEADING);
    vparallelGroup1.addComponent(HeadPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE);

    vseqgroup.addGroup(vparallelGroup1);

    this.EditorPane.setLocation(0, 120);

    When the user clicked on the button to add the new JTextPane, it adds the New JTextPane to the JInternal Form But change the size of the previous JTextPane and also the new JTextPane size and location not correct.

    The form is appearing like it has devided into two columns and the first column displays the previous JTextPane and the second column displays the new JTextPane starting from the last of the First JTextPane.

    I want to add the New JtextPane to the top of the previous JTextPane and also both JTextPane should occupy the complete horizontal area of the form. Only their vertical size should be differ and they should also resize when the form resize. Also I want to fix the vertical size of the new JTextPane not of the previous JTextPane.

    How can I do it?

    Help me

  2. #2
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    I've just try to read your code and I don't know exactly how can you do that, but if you want a faster answer you should repost your code using the [CODE] tag. Futhermore, I think that your explanation is a little bit messy I hardly could understand anything in the first reading, so maybe you should post some screeshots too.

  3. #3
    png
    png is offline Member
    Join Date
    Dec 2009
    Posts
    12
    Rep Power
    0

    Default Output file attachment

    I am attaching the three gif file which contains (in Zip folder Images)

    Initial.Gif: what will be the initial child form.

    Wrong.Gif: contains the output I am getting by above code.

    exactly what I want.GIF: contains the output I want.

    If you need any other clarification, please reply.
    Attached Files Attached Files

  4. #4
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    I would do that using a BorderLayout. In the NORTH of the border layout you add the first label, and then in the CENTER the next ones. Check out the Java swing tutorial and the API documentation (http://java.sun.com/j2se/1.4.2/docs/...derLayout.html).

    The components are laid out according to their preferred sizes and the constraints of the container's size. The NORTH and SOUTH components may be stretched horizontally; the EAST and WEST components may be stretched vertically; the CENTER component may stretch both horizontally and vertically to fill any space left over.
    Is that what you want in terms of resizing?

  5. #5
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    GroupLayout is a bit difficult to use.
    Regular readers here will expect me to suggest Box and BoxLayout.
    Other good choices are FlowLayout and GridLayout.
    See the layouts tutorial.

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class LayoutRx {
        JPanel centerPanel;
        JScrollPane scrollPane;
    
        public LayoutRx() {
            JTextPane headPane = new JTextPane();
            headPane.setBackground(Color.green.darker());
            scrollPane = new JScrollPane(headPane);
            scrollPane.setPreferredSize(new Dimension(200,140));
        }
    
        private JPanel getCenterPanel() {
            JTextPane editorPane = new JTextPane();
            editorPane.setBackground(Color.blue);
            centerPanel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = getConstraints();
            gbc.weighty = 1.0;
            gbc.fill = GridBagConstraints.BOTH;
            centerPanel.add(new JScrollPane(editorPane), gbc);
            return centerPanel;
        }
    
        private GridBagConstraints getConstraints() {
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.weightx = 1.0;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            return gbc;
        }
    
        private JPanel getUIPanel() {
            String[] ids = { "Add", "Remove" };
            ActionListener al = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    String id = e.getActionCommand();
                    boolean in = centerPanel.getComponent(0) == scrollPane;
                    if(id.equals("Add")) {
                        if(!in) {
                            centerPanel.add(scrollPane, getConstraints(), 0);
                        }
                    } else {
                        if(in) {
                            centerPanel.remove(scrollPane);
                        }
                    }
                    centerPanel.revalidate();
                }
            };
            JPanel panel = new JPanel();
            for(int i = 0; i < ids.length; i++) {
                JButton button = new JButton(ids[i]);
                button.setActionCommand(ids[i]);
                button.addActionListener(al);
                panel.add(button);
            }
            return panel;
        }
    
        public static void main(String[] args) {
            LayoutRx test = new LayoutRx();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test.getCenterPanel());
            f.add(test.getUIPanel(), "Last");
            f.setSize(500,600);
            f.setLocation(100,100);
            f.setVisible(true);
        }
    }

  7. #7
    png
    png is offline Member
    Join Date
    Dec 2009
    Posts
    12
    Rep Power
    0

    Default

    Thanks to all and specially thanks to hardwired. Now, I am following the concept of hardwired and it's working as I want.

    Now I have one more problem. How can I set the maximum number of characters per line in a JTextPane?

  8. #8
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    How can I set the maximum number of characters per line in a JTextPane?
    A JTextPane can, in principle, display text in multiple fonts and sizes.
    It can even include Icons.
    So setting the number of characters would not set the size.

    What the coide might do is to find the size of a representative character
    and then setPreferredSize() to a multiple of that character width & height.

    The size of a string is available from getStringBounds(String, Graphics)
    There is one catch-22:
    To determine the preferred size of an object you need a Graphics value.
    But the Graphics is unknown until the object is sized and about to display.
    About the best one can do is to determine initial size by creating an offscreen BufferedImage and do the initial sizing with its values.
    Then when the Graphics for the screen is available, resize the fonts to fit the screen area previously allocated.

  9. #9
    png
    png is offline Member
    Join Date
    Dec 2009
    Posts
    12
    Rep Power
    0

    Default

    Hi,

    Actually, My requirement is different. I have a multiline JtextPane. I want to allow the user to type onle some characters like 30 in one line. When the user type the 31th character, it must be start from the next line automatically in the same JTextPane. We are not restricting the size of the JTextpane. We only want to restrict the number of characters in each line of the JTextpane.

    How can we do it?

  10. #10
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    You could add a DocumentFilter to the JTextPane.
    When an inserted character would make a line too long, the filter can insert a newline.
    Implementation becomes tricky if you consider entering text into the middle of an existing line.
    Do you insert a newline at the end of the line
    whenever the user types a character into the middle of the line???

    OR you could just let the document be whatever, with line wrapping.
    Then when writing out the text insert line endings as desired.

  11. #11
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Quote Originally Posted by png View Post
    Hi,

    Actually, My requirement is different. I have a multiline JtextPane. I want to allow the user to type onle some characters like 30 in one line. When the user type the 31th character, it must be start from the next line automatically in the same JTextPane. We are not restricting the size of the JTextpane. We only want to restrict the number of characters in each line of the JTextpane.
    I ran into a similar problem. I eventually just wound up making a custom document which handled the setting of a max characters per row and set it to the JTextPane.

  12. #12
    Join Date
    Dec 2009
    Posts
    1
    Rep Power
    0

    Default

    Problem with Java v6 and v7

  13. #13
    png
    png is offline Member
    Join Date
    Dec 2009
    Posts
    12
    Rep Power
    0

    Default Problem with GridBagLayout

    I am facing a new problem with GridbagLayout. In the Program of hardwired (above), if there are the 24-25 line of text is already in the editorpane (old JTextPane), the add button does not add the new JTextPane of desired width.

    scrollPane.setPreferredSize(new Dimension(200,140));

    It is minimizing the Box. I want the box of desired width in every situation.

    How can we do it?

  14. #14
    png
    png is offline Member
    Join Date
    Dec 2009
    Posts
    12
    Rep Power
    0

    Default

    Hi,

    I have solved it. I have set the property

    GridBagConstraints.ipady=10;

    and its working what I want.

    Thanks to all

Similar Threads

  1. problem with Runtime.getRuntime().exec when running java in .bat
    By Shayko in forum Threads and Synchronization
    Replies: 2
    Last Post: 01-27-2010, 07:46 PM
  2. Problem with Runtime.getRuntime().exec with Linux Commands
    By swapnilnawale in forum Threads and Synchronization
    Replies: 1
    Last Post: 09-23-2009, 10:23 PM
  3. Adding component at runtime
    By Beju in forum Java Applets
    Replies: 5
    Last Post: 06-16-2009, 11:28 AM
  4. Problem with Runtime.exec()
    By nhabibi in forum Advanced Java
    Replies: 11
    Last Post: 07-02-2008, 01:35 AM
  5. Adding JARs to the classpath at runtime?
    By johann_p in forum Advanced Java
    Replies: 1
    Last Post: 06-26-2007, 10:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •