JAVA JTAB multiple compoments - java

I'm begging with JAVA Swing. But when I use my JPanel can I only add one JTextPane. How can I do so I can add multiple.
Here's is what I do
JPanel panel = new JPanel();
JTP = new JTextPane();
JTP.setBackground(Color.black);
JTP.setForeground(Color.WHITE);
Lines = new JTextPane();
new BorderLayout();
panel.add(JTP, BorderLayout.WEST);
new BorderLayout();
panel.add(Lines, BorderLayout.EAST);
And there's is no errors. I know i not have giving you all the code but it's work.

JPanel panel = new JPanel();
This create a JPanel which uses a FlowLayout by default.
new BorderLayout();
This statement does nothing because you don't have a reference to the BorderLayout, to you can't use it as a layout manager on any panel.
But there is no need to use a BorderLayout for you panel, since a FlowLayout can display multiple components at one time. The problem is that the text pane needs a "preferred size" before it can be using with a layout manager.
For something simple why don't you just start with a JTextArea since it is easer to use. You can create the text area with code like:
JTextArea textArea = new JTextArea(5, 20);
and it will create a text area with a preferred size to display 5 lines of text with about 20 characters on each line.
Then you create two text areas and add them to your panel.
Of course any time you use a text area you should probably add it to a JScrollPane:
JScrollPane scrollPane = new JScrollPane( textArea );
panel.add( scrollPane );
and then add the scrollPane to the panel.

From the looks of it, you are only seeing one TextField because you aren't properly using BorderLayout.
EDIT: You said you want to change background colors, you can do that to a JTextField and a JTextArea. Check the docs! It's one line of code.
You are supposed to explicitly set the layout of the JPanel to BorderLayout like this :
JPanel panel = new JPanel(new BorderLayout());
OR
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
Even simpler, you don't need to use BorderLayout.
JPanel, by default, is assigned a FlowLayout which is capable of neatly handling more than one JComponent. Try using a JTextField or JTextArea instead of a JTextPane.
If you need to use a JTextPane because of a certain layout goal, then try the code examples I listed above.
Good luck. Don't be afraid to refer to the official Oracle docs/tutorials for Swing Layouts
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

Related

How do I resize the text inside my JScrollpane - JAVA

I'm currently learning more about Java. I'm working on creating a GUI which is able to 'translate' amino-acid characters into their 3-letter codes.
I've got everything working as intended, but I'm still struggling to understand how I can resize the text inside my JScrollpane to not exceed the width. (Example in picture)
Do I just need to change some settings or maybe add '\n's to fit the JTextArea? Here's the code:
Thanks in advance!
private void createGUI() {
Container window = this.getContentPane();
window.setLayout(new FlowLayout());
panel = new JPanel();
inputField = new JTextField();
startButton = new JButton("Convert to 3-letter code");
display = new JTextPane();
scroll = new JScrollPane(display);
//CUSTOMIZE GUI OBJECTS
inputField.setPreferredSize(new Dimension(200, 20));
display.setPreferredSize(new Dimension(400, 300));
startButton.addActionListener(this);
//SETTING UP TEXTAREA
display.setEditable(false);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
panel.setLayout(new BorderLayout());
panel.add(scroll, BorderLayout.CENTER);
//
window.add(inputField);
window.add(startButton);
window.add(panel);
}
Better use a JTextArea instead of a JScrollPane since the best that the JScrollPane can do is to dynamically resize (Dynamically Resize a JScrollPane?)
I changed JTextPane display to a JTextArea object and changed 'display.setLineWrap(true);'
This fixed the issue I was having with JTextPane.
To answer the question in the title: How do I resize the text inside my JScrollpane
Inside your scrollpane you have some JComponent. Either that JComponent is fully visible since it is smaller or equal to the JScrollpane's viewport. Or it is bigger, in which case the JScrollpane will start displaying scrollbars and the relevant part.
To resize the text you will just have to tell the JComponent inside the JScrollpane to display the text differently. Depending on the JComponent you use this method may vary. Here some examples:
In a JLabel and most other components, increase the font size (How to change the size of the font of a JLabel to take the maximum size)
In a JLabel, switch to a multiline label (Multiline text in JLabel)
In a JTextArea, turn on word wrapping and line wrapping
In a JEditorPane you can even use markup inside the document to use different font sizes at the same time

How can I get the JTextArea to appear above the JTextField?

When I add both of them to SOUTH using BorderLayout, only the JTextArea appears. I'm using the text field as an input which is then displayed in the text area as an output above it with some other text.
It works if I set the text area to NORTH but it doesn't look great.
JPanel cmdPanel = new JPanel(new BorderLayout());
field = new JTextField(20);
cmdPanel.add(field, BorderLayout.SOUTH);
JTextArea output=new JTextArea();
output.setEditable(false);
output.setLineWrap(true);
cmdPanel.add(output, BorderLayout.SOUTH);
This image shows how it looks now with the TextArea set to NORTH.
I'm just trying to have it appear over the TextField and move up
the screen as outputs are added.
You have not provided a minimal, reproducible example but from the screen capture of your running app, it looks like you are explicitly setting the size of the JFrame. Rather than doing that, you should call method pack. Note that you should call that method after you have added both your JTextField and JTextArea to the cmdPanel and also after you have added cmdPanel to the JFrame. Also you should call method setVisible on the JFrame after calling method pack().
You should also make sure that the JTextArea has a preferred size before adding it to cmdPanel. One way to do this is by calling the constructor that takes the number of rows and columns parameters.
Maybe also wrap the JTextArea in a JScrollPane and make that the "center" component of cmdPanel?
JPanel cmdPanel = new JPanel(new BorderLayout());
field = new JTextField(20);
cmdPanel.add(field, BorderLayout.SOUTH);
JTextArea output=new JTextArea(10, 20);
output.setEditable(false);
output.setLineWrap(true);
JScrollPane scrollPane = new JScrollPane(output);
cmdPanel.add(scrollPane, BorderLayout.CENTER);
It is not possible to add two components in the same position (i.e BorderLayout.PAGE_END). To add more components in the same position, you should create a JPanel, add the components on it and then add the JPanel to the desirable position (i.e BorderLayout.PAGE_END).

How to make Components align to the TOP in JPanel with BoxLayout?

I'm developing a game called GalaxyWar, and I am trying to make a map selection menu. I found a problem that when I am using a BoxLayout with BoxLayout.Y_AXIS on a JPanel with setAlignmentX(CENTER_ALIGNMENT), the subcomponents (JPanel's) with assigned size, take up the entire height of the panel (all together), instead of the assigned height!
Here is my code:
scrollPane = new JScrollPane();
scrollPane.setBounds(160, 11, 452, 307);
add(scrollPane);
mapContainer = new JPanel();
mapContainer.setAlignmentX(CENTER_ALIGNMENT);
mapContainer.setAlignmentY(JPanel.TOP_ALIGNMENT);
mapContainer.setLayout(new BoxLayout(mapContainer, BoxLayout.Y_AXIS));
scrollPane.setViewportView(mapContainer);
JPanel demoPanel = new JPanel();
demoPanel.setLayout(null);
demoPanel.setBackground(Color.YELLOW);
demoPanel.setSize(50, 100);
mapContainer.add(demoPanel);
I've researched on this for long, but couldn't find any solutions so far.
try to check out
setPreferredSize()
setMaximumSize()
setMinimumSize()
set all 3 to the same value.
If it still doesn't work, you can try to put the panel, of which you are trying to set the size to fixed, inside another panel.

How to set the size of JTextPane according to the size of JPanel?

I want to set the size of the JTextPane according to the size of the panel so that when i add other panels, it changes accordingly. But it just gives a small text pane in the center and when i add some text, it's size changes accordingly.
JPanel panel = new JPanel();
JTextPane txt = new JTextPane();
JScrollPane pane = new JScrollPane();
pane.add(txt);
panel.add(pane,BorderLayout.CENTER);
add(pane);
now the jtextpane just appears at the center of the screen like a small box. I want it to appear according to the size of the panel
JPanel uses FlowLayout by default which sizes components according to their preferred sizes. You can use BorderLayout which will use the maximum area possible.
Also using constraints such as BorderLayout.CENTER has no effect unless the container is actually using BorderLayout. Dont add components to the JScrollPane. This will replaces all components within the view of the component. Instead set the JTextPane as the ViewPortView, for example
JPanel panel = new JPanel(new BorderLayout());
JTextPane txt = new JTextPane();
JScrollPane pane = new JScrollPane(txt);
// pane.add(txt); remove
panel.add(pane, BorderLayout.CENTER);
Read:
How to Use BorderLayout
How to Use Scroll Panes
You added pane twice. Add panel to your base (a JFrame?) instead and remember to actually set your JPanel to use BorderLayout.

How to stop a JTextField from getting re-sized in GridLayout

I am creating a GUI with grid layout. I have added the JTextFields, JButtons and everything to it DIRECTLY. For an example, like this,
JButton b1 = new JButton("Hello"):
JButton b2 = new JButton("Bye");
JTextField t1 = new JTextField(10);
JTextField t2 = new JTextField(10);
GridLayout grid = new GridLayout(2,2,2,2);
JPanel panel = new JPanel();
panel.setLayout(grid);
panel.add(b1);
panel.add(t1);
panel.add(b2);
panel.add(t2);
In here, if the user resizes the window everything appears very large. The only other way I know to prevent these from getting these is adding all of each and everything to seperate JPanel which has FlowLayout. But thats not practical because if there are 20 stuff, there will be 20 JPanels. I tried setting the maximum size, minimum size and all, and they didnt change any. Please help.
not possible with plain GridLayout, because this is basic feature of this Layout Manager
use proper Layout Manager GridBagLayout or todays MigLayout
very simple way is by using SpringLayout
#mKorbel and #Logan have a point in their answers, but if you for some reason really really REALLY need to use a GridLayout and you really need the JTextField to stay a certain size then you can do so by putting the JTextField inside a JPanel with a BorderLayout.
JTextField fixedWidthField = new JTextField();
JPanel fieldPanel = new JPanel(new BorderLayout());
fieldPanel.add(fixedWidthField, BorderLayout.WEST);
GridLayout grid = new GridLayout(2,2,2,2);
JPanel panel = new JPanel();
panel.setLayout(grid);
panel.add(b1);
panel.add(fieldPanel);
panel.add(b2);
panel.add(t2);
We all recommend you use a more advanced layout manager. I personally like TableLayout but you need to download an extra Jar file for that.
That is the default behavior of gridLayout. I'm not sure its easy to stop it. I would use gridBagLayout instead. You can set the fill to none for each cell it has. It is more difficult at first, but it is all I use now.

Categories

Resources