0

I want a implementation like this.

JscrollPane's panel is divided into two columns.

First column has jtextarea.

Second column should have a jbutton with Ok text.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class TestScrollPane extends JFrame {
    JPanel newScrollPanel;
    JPanel leftPanel;
    JScrollPane scrollPane;
    JEditorPane editorPane;
    JButton button;

    public TestScrollPane() {
            leftPanel = new JPanel();
            newScrollPanel = new JPanel();
            editorPane = new JEditorPane();
            scrollPane = new JScrollPane(editorPane);
            button = new JButton("ok");
            leftPanel.setBackground(Color.WHITE);
            leftPanel.add(button);

            editorPane.setEditable(false);
            scrollPane.setPreferredSize(new Dimension(250, 140));
            scrollPane.setMinimumSize(new Dimension(10, 10));

            String text = "";
            for(int i=0;i<50;i++){
                    text = text + "line " + i + "\n";
            }
            editorPane.setText(text);
            scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

            newScrollPanel.setLayout(new BoxLayout(newScrollPanel, BoxLayout.X_AXIS));
            newScrollPanel.add(editorPane);
            newScrollPanel.add(leftPanel);
            scrollPane.getViewport().add(newScrollPanel);

            addComponentListener(new ComponentAdapter() {
                    @Override
                    public void componentHidden(ComponentEvent evt) {           }
                    @Override
                    public void componentShown(ComponentEvent evt) {
                            changePane();
                    }
            });

            this.add(scrollPane);
            setFrame();


    }

    private void changePane() {
            leftPanel.setLayout(null);
            Insets insets = leftPanel.getInsets();
            Dimension size = button.getPreferredSize();
            int buttonY = (int) (insets.top + scrollPane.getHeight() - size.getHeight());
            button.setPreferredSize(size);
            leftPanel.setPreferredSize(new Dimension(((int) size.getWidth()), editorPane.getHeight()));
            button.setBounds(0, buttonY, size.width, size.height);
    }


    void setFrame(){
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            this.pack();
            this.setLocationRelativeTo(null);
            this.setVisible(true);
    }


    public static void main(String[] args) {
            java.awt.EventQueue.invokeLater(new Runnable() {

                    public void run() {
                            TestScrollPane testScrollPane = new TestScrollPane();
                    }
            });
    }

}

I have two questions.

  1. In my class the ok button is not placed in the correct place. when scroll bar mover is at the top corner , ok button should be in the right bottom corner of the visible area. Ok button should be completely visible. In my code only visible a part of the button. Is there any issue with determining the Y coordinates ?

    int buttonY = (int) (insets.top + scrollPane.getHeight() - size.getHeight());
    
  2. Even though the scroll is moved, ok button should not move with the scroll bar. Is that possible to implement ?

rinjan
  • 550
  • 5
  • 19
  • 4
    *"Even though the scroll is moved, ok button should not move with the scroll bar."* The obvious solution is to put the OK button **outside** the scroll pane! – Andrew Thompson Oct 07 '15 at 15:22
  • 1
    thank you very much for the feedback and the solution. I have found a similar problem in http://stackoverflow.com/questions/10093425/java-how-to-draw-non-scrolling-overlay-over-scrollpane-viewport. But not sure it will work for my requirements. I'll try a solution from trashgod suggestion. Otherwise I will move it to the outside – rinjan Oct 07 '15 at 16:02

0 Answers0