I have created a demo by swing and I want to show full text in textarea. I want to add two textarea into a scroll bar to show all component within a scroll bar. So to do that, I create a panel and then add two textarea on this one and then I add this panel into scroll bar. When I running demo, it show correctly and it also show correctly when I resize window larger but it have problem when I resize smaller screen. It does not wrap the text when resize the screen smaller (instead of appearing horizontal scroll bar).
Can you help me to automatically wrapped when resize the screen smaller.
Here my code:
JPanel gui = new JPanel(new GridLayout());
gui.setBorder(new EmptyBorder(2, 3, 2, 3));
JTextArea area = new JTextArea("JTextArea on JPanel inside JScrollPane does not resize properly");
area.setWrapStyleWord(true);
area.setLineWrap(true);
gui.add(area);
JTextArea area1 = new JTextArea("JTextArea on JPanel inside JScrollPane does not resize properly");
area1.setWrapStyleWord(true);
area1.setLineWrap(true);
gui.add(area1);
gui.setBackground(Color.WHITE);
JScrollPane scroller = new JScrollPane(gui);
//scroller.setBorder(new EmptyBorder(2, 3, 2, 3));
JFrame f = new JFrame("Big Text Fields");
f.add(scroller,BorderLayout.CENTER);
// Ensures JVM closes after frame(s) closed and
// all non-daemon threads are finished
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// See http://stackoverflow.com/a/7143398/418556 for demo.
f.setLocationByPlatform(true);
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
// should be done last, to avoid flickering, moving,
// resizing artifacts.
f.setVisible(true);
Just omit the scroll pane and make the text area a direct child of the container.
JTextArea does change its preferred size based on the text it contains. From the documentation:
java.awt.TextArea has two properties rows and columns that are used to determine the preferred size. JTextArea uses these properties to indicate the preferred size of the viewport when placed inside a JScrollPane to match the functionality provided by java.awt.TextArea. JTextArea has a preferred size of what is needed to display all of the text, so that it functions properly inside of a JScrollPane. If the value for rows or columns is equal to zero, the preferred size along that axis is used for the viewport preferred size along the same axis.
Related
When adding images to a TextButton in LibGDX how would I match the button to the contents of the image. Currently when I set size the image scales down to fit the button which leaves a large gap between the edge of the button and the contents of the image (See distance from green debug border to the edge of the buttons image).
I would like to be able to set the size of the button to match the content of the image so that the empty space around the image is ignored. I could crop out the empty space from the image but would have an off centered image due to the drop shadow and so would like to avoid that.
Top image shows the displayed dutton in game including the debug button border in green. Bottom image shows the source image with empty space.
TextButton.TextButtonStyle buttonStyle = new TextButton.TextButtonStyle();
buttonStyle.font = FontLoader.uiFont;
buttonStyle.up = new SpriteDrawable(new Sprite(assetManager.get(UISpriteLoader.SPRITE_UI_TAMING_BUTTON_BASE, Texture.class)));
buttonStyle.down = new SpriteDrawable(new Sprite(assetManager.get(UISpriteLoader.SPRITE_UI_TAMING_BUTTON_PRESSED, Texture.class)));
buttonStyle.over = new SpriteDrawable(new Sprite(assetManager.get(UISpriteLoader.SPRITE_UI_TAMING_BUTTON_HOVER, Texture.class)));
tameButton = new TextButton("Tame", buttonStyle);
tameButton.setPosition(1470, 1080-330-80);
tameButton.setSize(250, 80);
stage.addActor(tameButton);
Use a Ninepatch for your images. It will scale to fill the button out of the box.
I recommend you to use Skin Composer both for making the Ninepatches and for creating a Scene2d skin so you don't have to create your Styles in code.
I have used the Flash UIScrollbar component to scroll my texts which i have imported externally. Is it possible manipulate the scrollbar to put it in specific line of text? In default the scrollbar placed at the beginning of the text, but I want to it placed at the end or middle or specific line of text.
scrollPosition : Number Gets or sets the current scroll position and
updates the position of the thumb.
example:
myScrollBar.scrollPosition = 45;
Source:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/fl/controls/ScrollBar.html
I have been given a very specific requirement on how to display a list of messages. Each message can be up to 3200 characters long. For this particular display of the messages, only the first two lines of each are to be displayed, showing whatever fits of the message with the original format intact (tabs, spaces, newlines). The messages are to be contained in a JInternalFrame. When the user drags a side of the frame to increase or decrease its width, the visible text of a message is supposed to increase or decrease along with the frame, no horizontal scroll bar is desired.
I was able to get the desired behaviour with the text increasing/decreasing with the width of the frame using the following:
JTextArea ta = new JTextArea();
ta.setLineWrap(true);
ta.setWrapStyleWord(true);
ta.setRows(2);
ta.setEditable(false);
ta.setOpaque(false);
ta.setText(longString());
ta.setAlignmentY(TOP_ALIGNMENT);
JScrollPane scrollPane = new JScrollPane(ta);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
A list of scrollPanes are added to a JScrollPane with a vertical scroll bar but no horizontal scroll bar in the JInternalFrame.
The problem I am having is that the text is resting on the bottom of the JTextArea, so the user sees the last two lines of the message, not the first two lines. It appears to be possible to set the horizontal alignment, but not the vertical alignment. Is there some way to get the automatically expanding/contracting text with text alignment at the top?
This is something that's bugged me in the past as well. It has to do with how JTextComponents interact with JScrollPanes - specifically, there is a (invisible, when non-editable) Caret that ends up at the end of your JTextComponent. When the viewport tries to figure out what it should be viewing, it looks at the caret.
So, tell the JTextArea not to move the caret during your initialization block:
DefaultCaret c = new DefaultCaret();
c.setUpdatePolicy( DefaultCaret.NEVER_UPDATE );
ta.setCaret( c );
After loading text into the text area you can use:
textArea.setCaretPosition(0);
I have a JFrame that I am sticking a JPanel into to display an image, in this case 1024x1025. The problem I cannot seem to find an answer to is how to ensure my containing JFrame will display the whole image/JPanel. I have something close to this example:
ImagePanel view = new ImagePanel(); //extends JPanel
JFrame Container = new JFrame();
view.setSize(1024, 1025);
container = new JFrame(winTitle);
container.setLayout(new BorderLayout());
container.setSize(view.getSize());
container.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
container.add(view);
container.setVisible(true);
The problem with this obviously is that the JFrame is decorated by the underlying OS, so I have no idea how many pixels to make it larger than the size of the JPanel from system to system. As it is now, the image is cut off on the right and bottom by a handful of pixels each due to the setting of the JFrame to the same size as the JPanel. Is there a way to tell the JFrame to read the size of the Jpanel it is containing and make itself large enough to display the entire JPanel? Thanks!
EDIT
Setting preferred size on the image panels, then setting size (not preferred) on the container, then packing after the add gives me the behavior I was wanting. Can anyone explain whats going on here, I still am a bit fuzzy on order of operations by some swing components.
You should add the image panel to the contentPane of the JFrame. And you should just use pack() on the frame to make it take the appropriate size. It will take the size needed to accomodate the preferred size of its components.
After the container.add() and before the container.setVisible(), add a container.pack().
EDIT: You could also use Box.createVerticalStrut(int) and Box.createHorizontalStrut(int) in one of the sides of your BorderLayout. That should force the layout to leave room for the image.
If you are sure about the Image resolution, then use setPreferredSize(new Dimension(1024, 1025)) for the JFrame and also for the JPanel. Since JFrame uses BorderLayout by default, you dont need to specify container.setLayout(new BorderLayout());.
I am trying to create a sprite in Actionscript 3 using Flash Professional with fixed width and height that contains a list of TextFields that scrolls up and down. I want the highlighted textfield to be fixed focus with the text that goes out of the sprites bounds to be invisible. I am new to AS3 and tried to set the width and height of the sprite but it did not work. I would do this with CSS by having a with a fixed width and height and setting the overflow property to hidden. Thanks for any help!
Check out the ScrollPane component as you can set a fixed width and height for the content and choose to automatically show scroll bars for the pane.
Add a mask to the sprite to contrain what is visible, http://www.ilike2flash.com/2009/06/dynamic-masks-using-sprites-in.html