I'm making an attempt at making a mini paint program.
I'm trying to have scroll bars and a paint surface in a JPanel (paint surface in the center, scroll bars on south and east)
CPanel.setLayout(new BorderLayout());
JScrollPane horiPane = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
JScrollPane vertiPane = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
CPanel.add(horiPane, BorderLayout.EAST);
CPanel.add(vertiPane, BorderLayout.SOUTH);
PaintSurface p = new PaintSurface();
p.setPreferredSize(dim);
CPanel.add(p, BorderLayout.CENTER);
Problem is when I resize it, it just resizes the paint surface and the JPanel to fix the new size instead of allowing for scrolling
How can I fix this?
You are using the JScrollPane incorrectly. The basic code is:
PaintSurface paintSurface = new PaintSurface();
JScrollPane scrollPane = new JScrollPane( paintSurface );
cPanel.add(scrollPane, BorderLayout.CENTER);
Start by reading the Swing Tutorial for the basics of using all Swing components.
Related
I have a frame f, panel Fpanel. and textarea j.
This is a part of my code.
The scroll does not seem to be working on my text area.
JTextArea j=new JTextArea();
j.setBounds(60,150, 400,400);
j.setMargin(new Insets(3,3,3,3));
j.setEditable ( false ); // set textArea non-editable
JScrollPane scroll = new JScrollPane(j);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
I have added
Fpanel.add(scroll);
and
f.add(Fpanel);
to my code as well but it does not seem to be scrolling.
Am I missing a piece of the code or have I written something incorrectly?
if you want to have a vertical scroll bar:
JPanel panel = new JPanel();
JScrollPane jsp = new JScrollPane(panel, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
if you want to have a horizontal scroll bar:
JPanel panel = new JPanel();
JScrollPane jsp = new JScrollPane(panel, ScrollPaneConstants.HORIZONTOL_SCROLLBAR_ALWAYS, ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
I have been working on this for hours. I honestly cannot figure it out. I have JTextArea's inside a JSplitPane which is inside a JPanel with a JButton and all that is put in my JFrame. I am using Layout managers. I have tried using pack(). I have tried using preferred sizes. Without the JPanel my button does not display in the proper location or switch buttons in other Tabs. With the JPanel it cuts off all my text, stops the scroll function(yes I have tried setting the TextAreas to always have horizontal and vertical scroll bars...does not solve the problem where text just stops wrapping for no apparent reason).
public static void main(String[] args) throws IOException
{
JFrame frame = new JFrame();
Deck blackjack = new Deck(Deck.TYPE[0]);
JTextArea textBlackjackUnshuffled = new JTextArea();
JTextArea textBlackjackShuffle = new JTextArea();
JButton shuffleButtonBlackjack = new JButton(new ImageIcon(ImageIO.read(new File("res/shuffle.png"))));
JToolBar toolBarBlackjack = new JToolBar("Blackjack");
JSplitPane splitPaneBlackjack = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
JPanel panel = new JPanel();
JTabbedPane tabbedPaneBlackJack = new JTabbedPane();
JTabbedPane tabbedPaneCanasta = new JTabbedPane();
JTabbedPane tabbedPanePinochle = new JTabbedPane();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
textBlackjackUnshuffled.setColumns(10);
textBlackjackUnshuffled.setLineWrap(true);
textBlackjackUnshuffled.setWrapStyleWord(true);
textBlackjackUnshuffled.setEditable(false);
textBlackjackUnshuffled.setFont(new Font("DejaVu Sans", Font.PLAIN, 100));
textBlackjackUnshuffled.append(blackjack.toString());
textBlackjackShuffle.setColumns(10);
textBlackjackShuffle.setLineWrap(true);
textBlackjackShuffle.setWrapStyleWord(true);
textBlackjackShuffle.setEditable(false);
textBlackjackShuffle.setFont(new Font("DejaVu Sans", Font.PLAIN, 100));
textBlackjackShuffle.append(blackjack.toString());
shuffleButtonBlackjack.setBorderPainted(false);
shuffleButtonBlackjack.setFocusPainted(false);
shuffleButtonBlackjack.setContentAreaFilled(false);
splitPaneBlackjack.add(new JScrollPane(textBlackjackUnshuffled));
splitPaneBlackjack.add(new JScrollPane(textBlackjackShuffle));
panel.add(splitPaneBlackjack, BorderLayout.CENTER);
panel.add(shuffleButtonBlackjack, BorderLayout.PAGE_END);
tabbedPaneBlackJack.addTab("Blackjack", panel);
frame.add(tabbedPaneBlackJack);
frame.setSize(new Dimension(Toolkit.getDefaultToolkit().getScreenSize()));
frame.setVisible(true);
}
You're adding the JScrollPanes to the panel in BorderLayout positions, but have not set the layout manager of panel to BorderLayout. In this situation, panel will be using JPanel's default layout manager, FlowLayout, a manager which is not smart enough to respect the scroll pane's preferred sizes.
Your code needs:
panel.setLayout(new BorderLayout());
i am new to java swing, currently i am developing an application using java swing, i am facing the problem is that when i populate my JList with JScrollPane it is displaying on my whole jframe.
code:
List<Element> parentElements = xmlUtil.getParentElements();
String title = "License";
JFrame f = new JFrame(title);
String[] parentTags = new String[parentElements.size()];
for(int i=0;i<parentElements.size();i++)
{
System.out.println(parentElements.get(i).getName());
parentTags[i] = parentElements.get(i).getName();
}
JList list = new JList(parentTags);
JScrollPane scrollPane = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(50,40));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = f.getContentPane();
contentPane.add(scrollPane);
f.setSize(900, 700);
f.setVisible(true);
Output:
i want output like this so it can cover small area in jframe
Please tell me how to resize it ? as i am stuck into this problem from very long time.
Add the JScrollPane to a JPanel and then add the JPanel to the JFrame:
JScrollPane scrollPane = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = f.getContentPane();
JPanel panel = new JPanel();
panel.add(scrollPane);
panel.setPreferredSize(new Dimension(50,40));
contentPane.add(panel);
Then you can use the setPreferredSize method of the JPanel to resize it how you want.
Also consider using a good layout manager. I use a combination of two (primarily):
Netbeans' drag-and-drop WYSIWYG editor (Matisse)
MigLayout -- easily the best hand-written layout available. Some learning curve required (but less learning than the rest).
I have a scrollable JPanel which accepts other JPanels dynamicly.
scrollPane = new ScrollPane();
jPanel2 = new JPanel();// container jPanel
jPanel2.setBackground(Color.pink);
jPanel2.setLayout(new GridLayout(0, 1));
scrollPane.add(jPanel2);
add(scrollPane, BorderLayout.CENTER);
the program can add and delete JPanels to this JPanel and it will scroll. when the JPanel is getting bigger and you add another JPanel i would like the Container JPanel to have the scroll bar at the top.
I am trying to add a scroll bar to a JTextArea. Would someone please tell me what I did wrong with the code below?
JFrame frame = new JFrame ("Test");
JTextArea textArea = new JTextArea ("Test");
JScrollPane scrollV = new JScrollPane (textArea);
JScrollPane scrollH = new JScrollPane (textArea);
scrollV.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollH.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.setVisible (true);
Thank you in advance.
EDIT: I fixed the code with the Adel Boutros' advice below.
//FRAME
JFrame frame = new JFrame ("Test");
frame.setSize(500,500);
frame.setResizable(false);
//
//TEXT AREA
JTextArea textArea = new JTextArea("TEST");
textArea.setSize(400,400);
textArea.setLineWrap(true);
textArea.setEditable(false);
textArea.setVisible(true);
JScrollPane scroll = new JScrollPane (textArea);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.add(scroll);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
It doesn't work because you didn't attach the ScrollPane to the JFrame.
Also, you don't need 2 JScrollPanes:
JFrame frame = new JFrame ("Test");
JTextArea textArea = new JTextArea ("Test");
JScrollPane scroll = new JScrollPane (textArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.add(scroll);
frame.setVisible (true);
Open design view
Right click to textArea
open surround with option
select "...JScrollPane".
A scroll pane is a container which contains another component. You can't add your text area to two different scroll panes. The scroll pane takes care of the horizontal and vertical scroll bars.
And if you never add the scroll pane to the frame, it will never be visible.
Read the swing tutorial about scroll panes.
You don't need two JScrollPanes.
Example:
JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane(ta);
// Add the scroll pane into the content pane
JFrame f = new JFrame();
f.getContentPane().add(sp);