I'm pretty new to GUI but I'm trying to create a simple version of notepad and would like scroll bars to appear around the text area. However, I'm not sure why it isn't appearing.
public class NutPad extends JPanel {
public static void main(String[] args) {
JFrame frame = new JFrame("NutPad");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new NutPad(), BorderLayout.CENTER);
frame.setSize(500,300);
frame.setVisible(true);
}
private NutPad() {
add(makeTextAreaPanel());
}
private JPanel makeTextAreaPanel() {
JPanel textAreaPanel = new JPanel();
textAreaPanel.setSize(100,100);
JTextArea textArea = new JTextArea(20, 60); //15,43
JScrollPane scrollPane = new JScrollPane(textArea);
textAreaPanel.add(scrollPane,BorderLayout.CENTER);
textAreaPanel.add(textArea);
return textAreaPanel;
}
}
Thanks
If you're going to use the BorderLayout.CENTER constraint, then the container needs to have its layout set to BorderLayout.
Also you don't need textAreaPanel since you can just add the scrollPane straight into your NutPad panel.
private NutPad() {
setLayout(new BorderLayout());
add(makeScrollPane(), BorderLayout.CENTER);
}
private JScrollPane makeScrollPane() {
JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
return scrollPane;
}
Now your text area will fill the frame and the scrollbars will appear when the text takes up more than the available space.
Hope that helps :)
Related
I am trying to make a dynamic tabs with the option to add new ones in the application and some buttons next to it... To do so I have a main class:
private static void initAndDisplayUI() {
frame = new JFrame(...)
tabbedPane = new TabbedPane();
insertTab(tabbedPane, TabFactory.createTab(), true);
insertNewTabButton(tabbedPane);
...
}
}
Container class:
public class TabbedPane extends JPanel {public TabbedPane() {
this.captions = new TabCaptions();
this.tabs = new ArrayList<Tab>();
this.contentContainer = new JPanel(new BorderLayout());
setLayout(new BorderLayout());
add(captions, BorderLayout.NORTH);
add(contentContainer, BorderLayout.CENTER);
}
...
}
And a TabCaptions:
public class TabCaptions extends JPanel {
private TabCaption selectedTab;
private JComponent tabsPane;
private JScrollPane scrollPane;
private JPanel buttonsPane;
public TabCaptions() {
createUI();
}
private void createUI() {
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
setBackground(Color.DARK_GRAY);
add(createTabsPane());
add(createButtonsPane());
add(Box.createHorizontalGlue());
}
private JComponent createTabsPane() {
tabsPane = new JPanel();
tabsPane.setOpaque(false);
tabsPane.setLayout(new BoxLayout(tabsPane, BoxLayout.X_AXIS));
scrollPane = new JScrollPane(tabsPane, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
return tabsPane;
}
...
}
As a result I have a region with the tabs and some button next to it. However the app window draws a scrollPane with some weird size... I would like to display this "add new tab" button right next to the tabs that are created, resize it when the new tab is being added BUT with the functionality to display a scrollbar once it hits the maximum window width. I already have the scrolling but how can I make the behaviour of this dynamic position of new page?
I already have the scrolling
Not based on the code you have provided:
scrollPane = new JScrollPane(tabsPane, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
return tabsPane;
You need to add the JScrollPane to the frame, so you need to return the scroll pane from the method:
scrollPane = new JScrollPane(tabsPane, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//return tabsPane;
return scrollPane;
package Rainbow;
import javax.swing.*;
import java.awt.*;
public class Entry
{
public static void main(String[] Q)
{
JFrame R = new JFrame();
JPanel P = new JPanel()
{
public Dimension getMaximumSize()
{ return new Dimension(Integer.MAX_VALUE,getMinimumSize().height); }
};
P.setLayout(new BoxLayout(P,BoxLayout.Y_AXIS));
JTextArea A = new JTextArea(
"VERYLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONGLONG");
A.setEditable(false);
A.setLineWrap(true);
P.add(new Label("Text"));
P.add(A);
JScrollPane S = new JScrollPane(P);
R.add(S);
R.setSize(300,300);
R.setLocationRelativeTo(null);
R.setVisible(true);
}
}
It is how it looks on startup.
StartUp
After reducing the Frame's size.
Reduce
The JTextArea inside would not reduce its size.
I'm using JTextArea here because it seems to be the easiest way to do line wrap on a component. So how to solve it? Or is there an alternate way to do the same thing?
You need to resize TextArrea too. Here are some related examples for your question: Java JTextArea that auto-resizes and scrolls.
Example described where JPanel is used and how we can do it:
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout()); //give your JPanel a BorderLayout
JTextArea text = new JTextArea();
JScrollPane scroll = new JScrollPane(text); //place the JTextArea in a scroll pane
panel.add(scroll, BorderLayout.CENTER); //add the JScrollPane to the panel
// CENTER will use up all available space
For some reason I can't get my scrollpane to be displayed within an applet.
public void init() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JScrollPane scrPane = new JScrollPane(panel);
scrPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrPane.setLayout(new ScrollPaneLayout());
frame.getContentPane().add(scrPane);
this.setVisible(true);
}
You never display the JFrame that you create!
This:
frame.getContentPane().add(scrPane):
this.setVisible(true); // this != frame
is not working because you create a JFrame and then ignore it.
You shouldn't have an applet display a JFrame anyway. If you need to show a separate window, consider showing a JDialog. Better still, why not simply put the JScrollPane in the applet itself?
e.g.,
public void init() {
//JFrame frame = new JFrame();
JPanel panel = new JPanel();
JScrollPane scrPane = new JScrollPane(panel);
scrPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
// scrPane.setLayout(new ScrollPaneLayout());
// frame.getContentPane().add(scrPane);
getContentPane().add(scrPane);
// this.setVisible(true);
}
I want to add a scroll bar to my JTextArea but it just won't show up. I have read a lot of stuff on forums but all in vain. Any suggestions are highly appreciated.
Thanks in advance. Below is my code.
JPanel pan, pan2;
JTextArea text = new JTextArea();
JTextField fname = new JTextField(18);
JLabel filename = new JLabel("Filename");
JButton view = new JButton("View");
public FileReading() {
setLayout(new BorderLayout());
pan = new JPanel();
pan2 = new JPanel();
JScrollPane scroll = new JScrollPane(text);
//scroll.setBounds(400,400,400,400);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
text.setEditable(false);
scroll.setViewportView(text);
pan2.add(scroll);
//scrollpane.setViewportView(text);
pan2.setLayout(new BorderLayout());
//pan2.add(scrollpane);
pan.setLayout(new FlowLayout());
pan.add(filename, FlowLayout.LEFT);
pan.add(fname, FlowLayout.CENTER);
pan.add(view, FlowLayout.RIGHT);
view.addActionListener(this);
fname.addActionListener(this);
pan2.add(text, BorderLayout.CENTER);
pan2.add(pan, BorderLayout.SOUTH);
//BorderLayout.EAST
//add(pan, BorderLayout.SOUTH);
add(pan2);//, BorderLayout.CENTER
setVisible(true);
}
public static void main(String args[]) {
FileReading frame = new FileReading();
frame.setTitle("Enter The Full Path to the File");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(400,400,400,400);
//frame.setSize(400,400);
}
You need to add scroll (JScrollPane) to the pan2 not to the text (JTextArea)
try this
pan2.add(scroll, BorderLayout.CENTER);
in place of pan2.add(text, BorderLayout.CENTER);
EDIT
JTextArea gets added automatically when we add JScrollPane into the panel, as you have added text (JTextArea) inside JScrollPane
here -> JScrollPane scroll = new JScrollPane(text);
Can try this
add(scroll);//, BorderLayout.CENTER
add(pan, BorderLayout.SOUTH);
instead of
add(pan2);//, BorderLayout.CENTER
This way we are directly adding the scrollpane to main frame and putting other things below
I cant get the scrollPane to resize correctly when added to the scrollPanel. I want the scrollPane to scale to the size of the scrollPanel. Any tips?
public class MTabbedPane_HomePanel extends JPanel {
private JPanel scrollPanel;
private JScrollPane scrollPane;
public MTabbedPane_HomePanel()
{
init();
makePanel();
}
private void init() {
scrollPanel = new JPanel();
scrollPane = new JScrollPane(scrollPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
}
private void makePanel() {
IAppWidgetFactory.makeIAppScrollPane(scrollPane);
setLayout(new BorderLayout());
scrollPane.setBorder(null);
add(scrollPane, BorderLayout.CENTER);
}
}
your code is correct, maybe
try to disable UIDelegate from IAppWidgetFactory.makeIAppScrollPane(), if remains unchanged
then
check how you added (if is used LayoutManager) for MTabbedPane_HomePanel to the parent
Call setPreferredSIze() for the panel.