Java JTextArea that auto-resizes and scrolls - java

I have a JTextArea in a JPanel. How can I have the JTextArea fill the whole JPanel and resize when the JPanel resizes and scroll when too much text is typed in?

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
See http://download.oracle.com/javase/6/docs/api/javax/swing/JScrollPane.html or http://download.oracle.com/javase/tutorial/uiswing/components/scrollpane.html for more details on JScrollPane

Place the JTextArea inside of a JScrollPane, and place that into the JPanel with with a layout that fixes the size. An example with a GridBagLayout, for instance could look like this:
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
JScrollPane scrollpane = new JScrollPane();
GridBagConstraints cons = new GridBagContraints();
cons.weightx = 1.0;
cons.weighty = 1.0;
panel.add(scrollPane, cons);
JTextArea textArea = new JTextArea();
scrollPane.add(textArea);
This is only a rough sketch, but it should illustrate how to do it.

Related

pinned footer inside JScrollPane

I would like to pin my footer panel to the bottom of my JPanel, i have tried the following.
JPanel pane = new JPanel();
pane.setLayout(new BorderLayout());
pane.add(header, BorderLayout.PAGE_START);
pane.add(table, BorderLayout.CENTER);
pane.add(footer, BorderLayout.PAGE_END);
JScrollPane SP = new JScrollPane(pane, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
What am i missing here?
This should work
JPanel child = new JPanel(new BorderLayout());
child.add(header, BorderLayout.PAGE_START);
child.add(table, BorderLayout.CENTER);
JScrollPane scrollPane = new JScrollPane(child, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JPanel pane = new JPanel(new BorderLayout());
pane.add(scrollPane, BorderLayout.CENTER);
pane.add(footer, BorderLayout.PAGE_END);
I need to do footer with scroll like on the table
A JScrollPane doesn't support this type of functionality. So you somehow need to fake it.
Maybe you can use a second JScrollPane as the footer component. Then you share the model of the horizontal JScrollBar. Using RSDinh's code as a starting point you might do something like:
//pane.add(footer, BorderLayout.PAGE_END);
JScrollPane footerScrollPane = new JScrollPane(footer);
JScrollBar horizontal = footerScrollPane.getHorizontalScrollBar();
horizontal.setModel(scrollPane.getHorizontalScrollBar().getModel());

Set JComboBox height

I'm not able to set the height of a JComboBox, I searched in the web but didn't found the right answer.
As you can see from the image below the combo box fills nearly all the panel height and I'd like it to have a smaller height.
I tried setting size with getPreferredSize() method but it didn't work, it worked only for other components like the button.
My code
private JComponent firstPanel()
{
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JLabel spesaAnnuaSingola = new JLabel();
spesaAnnuaSingola.setText("Spesa Annua Singola");
panel.add(spesaAnnuaSingola, BorderLayout.NORTH);
JComboBox<String> listaSpese = new JComboBox<String>();
panel.add(listaSpese, BorderLayout.CENTER);
JTextField speseAnnuaSingolaTF = new JTextField();
speseAnnuaSingolaTF.setText("");
speseAnnuaSingolaTF.setEditable(false);
panel.add(speseAnnuaSingolaTF, BorderLayout.LINE_START);
JButton button = new JButton("CALCOLA")
{
public Dimension getPreferredSize()
{
return new Dimension(150,50);
};
};
JPanel leftflowpanel = new JPanel( new FlowLayout(FlowLayout.LEFT) );
leftflowpanel.add(speseAnnuaSingolaTF);
panel.add(leftflowpanel, BorderLayout.SOUTH);
JPanel rightflowpanel = new JPanel( new FlowLayout(FlowLayout.RIGHT) );
rightflowpanel.add(button);
panel.add(rightflowpanel, BorderLayout.SOUTH);
return panel;
}
And then:
public StatsPanel()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
GridBagConstraints c = new GridBagConstraints();
// I will need a grid layout
this.setLayout(new GridLayout(1, 1, 30, 30));
JPanel panelLeft = new JPanel(new BorderLayout());
panelLeft.setBorder(BorderFactory.createEmptyBorder(20, 10, 20, 50));
panelLeft.add(firstPanel(), BorderLayout.NORTH);
c.fill = GridBagConstraints.VERTICAL;
c.gridx = 0;
c.gridy = 0;
this.add(panelLeft);
}
JComboBox<String> listaSpese = new JComboBox<String>();
panel.add(listaSpese, BorderLayout.CENTER);
You add your combo box to the CENTER of the BorderLayout, which gets all the extra space of the frame. Don't add the combo box to the CENTER.
Instead you will need to nest panels. So create a panel for the NORTH of the BorderLayout. Then this panel will contain both your label and your combo box. Maybe use a vertical BoxLayout for this panel. Then both the label and the combo box will be displayed at their preferred heights.
Read the section from the Swing on Layout Manager for more information. The point is you can nest multiple panels each using a different layout to achieve your desired layout.

Java: Showing a BoxLayout panel in the middle of the frame

I recently started working with Java and I am not too sure how to put my BoxedLayout Panel in the middle of my `JFrame. At the moment, I have the following:
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JLabel quizLabel = new JLabel("Java Quiz",SwingConstants.CENTER);
quizLabel.setForeground(Color.BLUE);
quizLabel.setFont(new Font("Arial", Font.BOLD, 20));
quizLabel.setOpaque(true);
panel.add(quizLabel);
JLabel newLineLabel = new JLabel(" ",SwingConstants.CENTER);
newLineLabel.setOpaque(true);
panel.add(newLineLabel);
JLabel createdByLabel = new JLabel("Created By",SwingConstants.CENTER);
createdByLabel.setOpaque(true);
panel.add(createdByLabel);
JLabel nameLabel = new JLabel("XXX",SwingConstants.CENTER);
nameLabel.setOpaque(true);
panel.add(nameLabel);
contentPane.add(panel, BorderLayout.CENTER);
contentPane is taken from my frame. This gives me the following output:
I want the three labels inside the panel to appear in the middle of the Frame.
Because it is the only panel on the screen, the BoxLayout will fill the entire frame and thus depending on how your JComponents are created in the panel, it will show it like that on the frame too.
What I would do if I were you, is created a BorderLayout as a container for your BoxLayout.
This way, you can set your BoxLayout as the center of the Borderlayout.
See if this code works:
//This will fill your frame
JPanel containerPanel = new JPanel(new BorderLayout());
contentPane.add(containerPanel);
//this is the BoxPanel you wnat your components to be organized in
JPanel boxPanel = new JPanel(new BoxLayout());
//Add all your components to the boxPanel
//add your panel with all the components to the container panel
containerPanel.add(boxPanel, BorderLayout.CENTER);
The easiest way is to use a GridBagLayout. Using the default constraints a single component will be centered in the panel:
//contentPane.add(panel, BorderLayout.CENTER);
contentPane.setLayout( new GridBagLayout() );
contentPane.add(panel, new GridBagConstraints());

Having Multiple Panels with scroll panes

I created two panels and a main panel. Each panel contains a very large image, and I wanted both of them to be scroll-able to see the rest of the image. But when I add the two panels in the main panel and run it, the first panel is soo big that it covers the second panel. How would I implement ScrollPane for both panels?
import java.awt.BorderLayout;
import javax.swing.*;
public class BoardFrame extends JFrame {
JPanel mainPanel = new JPanel(new BorderLayout());
JLabel jLabel = new JLabel();
JPanel jPanelNorth = new JPanel();
JScrollPane scrollPane = new JScrollPane();
JLabel jLabel2 = new JLabel();
JPanel jPanelSouth = new JPanel();
JScrollPane scrollPane2 = new JScrollPane();
public BoardFrame() {
jLabel.setIcon(new ImageIcon("an image here"));
jPanelNorth.add(jLabel);
jLabel2.setIcon(new ImageIcon("an image here"));
jPanelSouth.add(jLabel2);
mainPanel.add(jPanelNorth, BorderLayout.NORTH);
mainPanel.add(jPanelSouth, BorderLayout.SOUTH);
add(mainPanel);
//where would I use this?
//scrollPane.setViewportView();
}
}
Each panel contains a very large image>
//JPanel mainPanel = new JPanel(new BorderLayout());
JPanel mainPanel = new JPanel(new GridLayout(0, 1));
You may want to use a GridLayout so that each scroll pane takes up half the frame so as much of each image as possible is displayed.
//JScrollPane scrollPane = new JScrollPane();
JScrollPane scrollPane2 = new JScrollPane(jPanelNorth);
The easiest way to use the scroll pane is to create the scrollpane with the component you want displayed and the scrollpane will add the component to the viewport for you.
//mainPanel.add(jPanelNorth, BorderLayout.NORTH);
mainPanel.add(scrollPane); // don't need the constraint when using GridLayout.
Then you add the scrollPane to the main panel, since the scrollpane contains the panel with the image.
it seems to use grid layout is much better than using border layout , in this case :
import java.awt.BorderLayout;
import javax.swing.*;
public class BoardFrame extends JFrame {
//1. use GridLayout with 2 rows and 1 column .
JPanel mainPanel = new JPanel(new GridLayout(2,1));
JLabel jLabel = new JLabel();
JPanel jPanelNorth = new JPanel();
JScrollPane scrollPane = new JScrollPane();
JLabel jLabel2 = new JLabel();
JPanel jPanelSouth = new JPanel();
JScrollPane scrollPane2 = new JScrollPane();
public BoardFrame() {
jLabel.setIcon(new ImageIcon("an image here"));
jPanelNorth.add(jLabel);
jLabel2.setIcon(new ImageIcon("an image here"));
jPanelSouth.add(jLabel2);
//2.you should place .setViewportView() here :
scrollPane.setViewportView(jPanelNorth);
scrollPane2.setViewportView(jPanelSouth);
mainPanel.add(scrollPane);//is in the top ("North")
mainPanel.add(scrollPane2);//next ("South")
//3.use setContentPane instead of add()
setContentPane(mainPanel);
}
}

JLabel positioning into JPanel

I have this code written to make a database connection and add a client:
//adding the left panel
JPanel left = new JPanel();
left.setPreferredSize(new Dimension(250, 500));
left.setLayout(new BoxLayout(left, BoxLayout.Y_AXIS));
add(left);
//adding the right panel
JPanel right = new JPanel();
right.setPreferredSize(new Dimension(250, 500));
right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS));
add(right);
//adding the jlabel title to the left panel
JLabel leftTitle = new JLabel("Add a client");
leftTitle.setAlignmentX(CENTER_ALIGNMENT);
left.add(leftTitle);
//adding the jlabel title to the right panel
JLabel rightTitle = new JLabel("Make a reservation");
rightTitle.setAlignmentX(CENTER_ALIGNMENT);
right.add(rightTitle);
//adding the jlabel "name"
JLabel nameL = new JLabel("Name:");
left.add(nameL);
and I want to move this JLabel here:
I've tried doing nameL.setAlignmentX(LEFT_ALIGNMENT); but it's still not working
Your problem is that you've used a BoxLayout.
left.setLayout(new BoxLayout(left, BoxLayout.Y_AXIS));
^^^^^^^^^
Your BoxLayout is set to align things centered along the y-axis, so no amount of setting alignment is going to change that. In order to fix your problem, you need a different layout manager like GroupLayout or CardLayout.

Categories