0

I know there are many questions about it but i read all of them. Mostly people prefer adding textareas into a panel and adding this panel into JScrollPane. I tried this but it is not working.

Here is my code.

AppSed Class

package gui;

import javax.swing.SwingUtilities;

public class AppSed {

public static void main(String[] args){

SwingUtilities.invokeLater(new Runnable(){
    public void run(){

        new MainFrame();
    }
    });
}
}

MainFrame Class

package gui;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class MainFrame extends JFrame {

private TextPanel textPanel;
private ToolBar toolBar;

public MainFrame(){
    super("Sed Debugger");

    setLayout(new BorderLayout());

    textPanel = new TextPanel();
    toolBar = new ToolBar();
    add(textPanel,BorderLayout.CENTER );
    add(toolBar, BorderLayout.NORTH);

    setJMenuBar(createMenuBar());
    setSize(1200,800);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
}
public JMenuBar createMenuBar(){
     JMenuBar menuBar = new JMenuBar();

     JMenu file = new JMenu("File");
     JMenu debug = new JMenu("Debug");
     JMenu help = new JMenu("Help");

     JMenuItem newFile = new JMenuItem("New");
     JMenuItem saveFile = new JMenuItem("Open");
     JMenuItem openFile = new JMenuItem("Save");
     JMenuItem exit = new JMenuItem("Exit");

     file.add(newFile);
     file.add(openFile);
     file.add(saveFile);
     file.add(exit);

     JMenuItem run = new JMenuItem("Run");
     JMenuItem stop = new JMenuItem("Stop");
     JMenuItem refresh = new JMenuItem("Refresh");
     JMenuItem stepIn = new JMenuItem("Step In");
     JMenuItem stepOut = new JMenuItem("Step Out");
     JMenuItem stepOver = new JMenuItem("Step Over");
     JMenuItem breakPoint = new JMenuItem("New Breakpoint");

     debug.add(run);
     debug.add(stop);
     debug.add(refresh);
     debug.add(stepIn);
     debug.add(stepOut);
     debug.add(stepOver);
     debug.add(breakPoint);

     JMenuItem commentHelp = new JMenuItem("Comment Help");
     JMenuItem about = new JMenuItem("About");

     help.add(commentHelp);
     help.add(about);

     menuBar.add(file);
     menuBar.add(debug);
     menuBar.add(help);

     return menuBar;
}
}

TextPanel class

package gui;

import java.awt.Dimension;
public class TextPanel extends JPanel {
private InputPane input ; 
private JScrollPane scrollPane;

public TextPanel(){

    setLayout(new GridBagLayout());

    input = new InputPane();
    scrollPane = new JScrollPane(input);

    Dimension dim = scrollPane.getPreferredSize();
    dim.width = 510;
    dim.height = 290;
    scrollPane.setPreferredSize(dim);

    GridBagConstraints gc = new GridBagConstraints();

    gc.gridx = 0;
    gc.gridy = 0;
    gc.weightx = 0.25;

    add(scrollPane,gc);

}
}

InputPane class

package gui;

import java.awt.BorderLayout...;


public class InputPane extends JPanel {

private JTextArea inputText;
private JTextArea lineNumber;
private JScrollPane scrollPane;

public InputPane(){

    setLayout(new BorderLayout());

    Dimension dim = getPreferredSize();
    dim.width  = 500;
    dim.height = 250;
    setPreferredSize(dim);

    setBorder(BorderFactory.createTitledBorder("Original File"));

    inputText = new JTextArea();
    lineNumber = new JTextArea();

    Dimension dimInput = getPreferredSize();
    dimInput.width = 440;
    dimInput.height = 250;
    inputText.setPreferredSize(dimInput);

    Dimension lineDim = getPreferredSize();
    lineDim.width = 50;
    lineDim.height = 250;
    lineNumber.setPreferredSize(lineDim);
    lineNumber.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 2,             Color.gray));

    add(inputText,BorderLayout.EAST);
    add(lineNumber,BorderLayout.WEST);

}
nonegravity
  • 15
  • 1
  • 1
  • 6

2 Answers2

1
inputText = new JTextArea();
lineNumber = new JTextArea();

Instead of using two text areas on a panel, I'm thinking you should be adding the line number textarea to the row header of the scrollpane. Also, don't use setPreferredSize(). It defaults the purpose of using a scrollpane since scrollbars will only appear when the preferred size of the component is greater than the size of the scrollpane.

Instead of using two text areas you may want to check out Text Component Line Number which was designed to display line numbers beside a text component. The line number component is added to the row header of the scroll pane.

camickr
  • 321,443
  • 19
  • 166
  • 288
1

You "seem" to be trying to create a "split" with line numbers on the left and text on the right?

The first step, stop screwing with the preferredSize of the components. You don't control the factors by which these components need to calculate their requirements.

See Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? for more details

Instead, use the available properties to provide hints, which can allow them to calculate the amount of space they will require

Take a look at JTextArea(int, int) for more details

For example...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TextPanel());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TextPanel extends JPanel {

        private InputPane input;
        private JScrollPane scrollPane;

        public TextPanel() {

            setLayout(new GridBagLayout());

            input = new InputPane();
            scrollPane = new JScrollPane(input);

            GridBagConstraints gc = new GridBagConstraints();

            gc.gridx = 0;
            gc.gridy = 0;
            gc.weightx = 0.25;

            add(scrollPane, gc);

        }
    }

    public class InputPane extends JPanel {

        private JTextArea inputText;
        private JTextArea lineNumber;

        public InputPane() {

            setLayout(new BorderLayout());

            setBorder(BorderFactory.createTitledBorder("Original File"));

            inputText = new JTextArea(10, 20);
            lineNumber = new JTextArea(10, 4);

            lineNumber.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 2, Color.gray));

            add(inputText);
            add(lineNumber, BorderLayout.WEST);

        }
    }
}

Now, having said that, I would discourage you from this process and suggest using the JScrollPane#setRowHeaderView instead. See this example and How to Use Scroll Panes for more details

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • @nonegravity Again, I'd discourage you from using two `JTextArea`s in this way, better to use `JScrollPane#setRowHeaderView`, even adding the `lineNumber`s `JTextArea` as the header view would provide you with a much better solution... – MadProgrammer Jan 30 '15 at 01:20
  • I am new at designing GUI. Thanks for your advise , i will try to learn about it more. – nonegravity Jan 30 '15 at 01:24