I have a JPanel(a) with box layout, and all the components are stacked on it vertically.
Among them, I have a JPanel(b) with horizontal box layout. To that JPanel(b) I have added rigid area, JPanel and JTextArea.
What I want is that JPanel(b) increases its height every time JTextArea expands due to word wrap. However, since my JPanel in beginning has the height of a single row. JTextArea doesn't expand because all space is filled.
Is there a way to fix this, an alternative?
It doesn't have to be JPanel and JTextArea, just something that can contain components and a JTextComponent that suporrts multi line.
class Question extends JPanel
{
public JPanel questionArea;
public JTextArea number, question;
public Question(Page page)
{
setSize(new Dimension(556, 100));
setBackground(Color.PINK);
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
Border in = BorderFactory.createDashedBorder(Color.BLACK);
Border out = BorderFactory.createMatteBorder(0, 0, 10, 0, Color.WHITE);
setBorder(BorderFactory.createCompoundBorder(out, in));
questionArea = new JPanel();
questionArea.setPreferredSize(new Dimension(556, 32));
questionArea.setBackground(Color.YELLOW);
questionArea.setLayout(new BoxLayout(questionArea, BoxLayout.LINE_AXIS));
out = BorderFactory.createMatteBorder(0, 0, 8, 0, Color.WHITE);
setBorder(BorderFactory.createCompoundBorder(out, in));
number = new JTextArea();
number.setPreferredSize(new Dimension(25, 32));
number.setBackground(Color.GREEN);
number.setFont(new Font("Arial", Font.BOLD, 15));
number.setText("10.");
number.setEditable(false);
question = new JTextArea();
question.setPreferredSize(new Dimension(494, 32));
question.setBackground(Color.PINK);
question.setFont(new Font("Arial", Font.BOLD, 15));
question.setText("What is the first question?");
questionArea.add(Box.createRigidArea(new Dimension(35, 32)));
questionArea.add(number);
questionArea.add(question);
add(questionArea);
page.editArea.add(this, page.content);
}
}
break
class Page extends JPanel
{
public JPanel editArea;
public Box.Filler blank;
public Page(JPanel panel)
{
setLayout(null);
setBounds(92, panel.getPreferredSize().height+40, 794, 1123);
setBackground(Color.WHITE);
editArea = new JPanel();
editArea.setLayout(new BoxLayout(editArea, BoxLayout.PAGE_AXIS));
editArea.setBounds(119, 96, 556, 931);
editArea.setBackground(Color.LIGHT_GRAY);
blank = new Box.Filler(new Dimension(556, -1), new Dimension(556, 931), new Dimension(556, 931));
editArea.add(blank);
add(editArea);
}
}
Page class is itself on a JPanel with null layout, no need for code, right?
I think horizontal BoxLayout is doing you in.
It will display components at their preferred size (using getPreferredSize() upon instantiation) and I don't believe it will readily resize it for you. You might want to change Panel(b) to a BorderLayout, and add the components to the EAST, WEST, and the JTextArea in CENTER.
Related
I want to be able to scroll down a dynamically generated list of movies. I tried adding a Scrollpane.
I have a navigation bar at the page start and in the center a jpanel with all the movies.
You can recreate this example by using this code:
private static JFrame frame;
public static void main(String[] args) throws HeadlessException {
frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.setBackground(new Color(32, 32, 32));
JPanel navigationPanel = createNavigationBar();
frame.add(navigationPanel, BorderLayout.PAGE_START);
JPanel moviePanel = createMoviePanel();
frame.add(moviePanel, BorderLayout.CENTER);
frame.setPreferredSize(new Dimension(1920, 1080));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Example App");
frame.pack();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
// MoviePanel Class
public static JPanel createMoviePanel() {
JPanel parentMoviePanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 25));
JPanel contentPanel = new JPanel(new GridLayout(0, 1));
JPanel moviePanel = new JPanel(new GridLayout(0, 9, 8, 5));
JScrollPane scrollPane = new JScrollPane(moviePanel);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.getVerticalScrollBar().setPreferredSize(new Dimension(0, 0));
scrollPane.setBorder(new EmptyBorder(0, 0, 0, 0));
parentMoviePanel.setBackground(new Color(32, 32, 32));
contentPanel.setBackground(new Color(32, 32, 32));
moviePanel.setBackground(new Color(32, 32, 32));
final File root = new File("");
for (int i = 0; i < 70; i++) {
// Get the image and scale it down
BufferedImage movieCover=new BufferedImage(150,200,BufferedImage.TYPE_INT_RGB);
Graphics2D g2d=(Graphics2D)movieCover.getGraphics();
g2d.setColor(Color.GRAY);
g2d.fillRect(0,0,movieCover.getWidth(),movieCover.getHeight());
// Create button and change settings
JButton movieButton = new JButton("Movie " + i, new ImageIcon(movieCover));
movieButton.setMargin(new Insets(0, 0, 0, 0));
movieButton.setPreferredSize(new Dimension(180, 250));
movieButton.setForeground(Color.WHITE);
movieButton.setContentAreaFilled(false);
movieButton.setBorderPainted(false);
movieButton.setFocusPainted(false);
movieButton.setHorizontalTextPosition(JButton.CENTER);
movieButton.setVerticalTextPosition(JButton.BOTTOM);
moviePanel.add(movieButton);
scrollPane.revalidate();
}
contentPanel.add(moviePanel);
contentPanel.add(scrollPane);
parentMoviePanel.add(contentPanel);
return parentMoviePanel;
}
// Navbar Class
public static JPanel createNavigationBar() {
JPanel navBar = new JPanel();
navBar.setLayout(new FlowLayout(FlowLayout.LEFT, 30, 20));
navBar.setBackground(new Color(25, 25, 25));
JButton homeButton = new JButton("Home");
homeButton.setContentAreaFilled(false);
homeButton.setBorderPainted(false);
homeButton.setFocusPainted(false);
JButton movieButton = new JButton("Movies");
movieButton.setContentAreaFilled(false);
movieButton.setBorderPainted(false);
movieButton.setFocusPainted(false);
// Add all the buttons to the navbar
navBar.add(homeButton);
navBar.add(movieButton);
return navBar;
}
What I'm trying to do is to scroll down this list of movies, using my mouse wheel without seeing any kind of scrollbar. It should look exactly how it looks now, but I want to be able to scroll down and see all the movies.
I don't know why it isn't working that's why I'm asking here in hope someone can explain to me what I'm doing wrong.
Your usage of a scroll pane is incorrect.
A Swing component can only have a single parent. The following code is creating the scroll pane with a child component. However you then remove the moviePanel from the scroll pane when you add it to the content pane.
So the scroll pane has no child component and will never work.
JScrollPane scrollPane = new JScrollPane(moviePanel);
...
contentPanel.add(moviePanel);
contentPanel.add(scrollPane);
However, even that won't solve your problem because you are now using a FlowLayout on your top level panel so all the child components will always be displayed at their preferred size so there is no need for scroll bars.
Get rid of all the scroll pane logic in your createMoviePanel() method.
Instead you probably want to add the scroll pane to the content pane:
//frame.add(moviePanel, BorderLayout.CENTER);
frame.add(new JScrollPane(moviePanel), BorderLayout.CENTER);
Now the scroll pane will dynamically resize as the frame size changes. Scrollbars will then appear as required.
I would like to make my JLabel have a border which fits around the text. I have tried using ints to resize but that doesn't work. Can anyone help?
Here is my Java code:
package first;
import java.awt.*;
import javax.swing.*;
public class TopLevelWindow {
static int hgap=5;
static int vgap=5;
private static void createWindow() {
//Create and set up the window.
JFrame frame = new JFrame("Window");
JLabel textLabel = new JLabel("Welcome Child",SwingConstants.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(textLabel, BorderLayout.CENTER);
textLabel.setPreferredSize(new Dimension(300, 100));
textLabel.setForeground(Color.YELLOW);
textLabel.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 5));
//Display the window.
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
frame.getContentPane().setBackground(Color.BLACK);
}
public static void main(String[] args) { createWindow(); }
}
I would want it to look like the text box in this:
http://www.bogleech.com/halloween/undertale-grillbys.png
is that possible in java?
Obviously, if you set your JLabel’s size to 300×300, its border will be around that rectangle.
Instead, leave the JLabel’s size alone, and place it inside a panel with a centering layout, then place the border on that panel:
JLabel textLabel = new JLabel("Welcome Child",SwingConstants.CENTER);
textLabel.setForeground(Color.YELLOW);
textLabel.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 5));
JPanel textPanel = new JPanel(new GridBagLayout());
textPanel.add(textLabel);
textPanel.setPreferredSize(new Dimension(300, 100));
frame.getContentPane().add(textPanel, BorderLayout.CENTER);
If you want to achieve the effect from the screen you should set the vertical alignment to NORTH and use CompoundBorder consisting of white outer border and inner invisible border which looks like the margin. Try this piece of code:
JLabel textLabel = new JLabel("Test test test test test test test...");
textLabel.setPreferredSize(new Dimension(500, 250));
textLabel.setForeground(Color.WHITE);
textLabel.setFont(new Font("Courier", Font.BOLD, 30));
// sets the text to the upper left corner
textLabel.setVerticalAlignment(SwingConstants.NORTH);
textLabel.setBorder(new CompoundBorder( // sets two borders
BorderFactory.createMatteBorder(10, 10, 10, 10, Color.WHITE), // outer border
BorderFactory.createEmptyBorder(10, 10, 10, 10))); // inner invisible border as the margin
I have a dynamically created jtable with a jscrollpane. I am trying to resolve the border issue. If i specify a border I get a border around the area where theres no table data. If i create empty border in the scrollpane i get the desired border, however the border around the headings are not there. I will include two pics so you can see issue.
The left and bottom border shouldn't be there.
This is correct, but the border on the heading is missing.
I will include pertinent code.
One class
private void createPanels(JButton close, JButton mapButton){
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
add(mainPanel);
setModal(true);
JPanel topPanel = new JPanel(new BorderLayout(0, 0));
topPanel.setPreferredSize(new Dimension(400, 30));
JLabel title = new JLabel("Mapping Flags");
title.setFont(new Font("SansSerif", Font.PLAIN, 17));
JPanel panelForTitle = new JPanel(new FlowLayout(FlowLayout.LEFT, 270, 30));
panelForTitle.add(title);
mainPanel.add(panelForTitle);
mainPanel.add(topPanel);
JPanel tablePanel = new JPanel(new BorderLayout());
tablePanel.setBorder(BorderFactory.createEmptyBorder(15, 25, 15, 25));
tablePanel.add(spTable);
mainPanel.add(tablePanel);
JPanel bottom = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 30));
close.setPreferredSize(new Dimension(90,22));
mapButton.setPreferredSize(new Dimension(90,22));
bottom.add(close);
bottom.add(mapButton);
mainPanel.add(bottom);
bottom.setMaximumSize(new Dimension(600, 0));
setTitle("Mapping Flags");
setSize(new Dimension(650, 380));
setResizable(false);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
Another class
public void setMappingsTable(){
dtm = new DefaultTableModel(new String[]{"Style ID", "Style", "Exact Match", "Carry Over", "OEM Temp"}, 0);
mappingsTable.setModel(dtm);
mappingsTable.setRowHeight(20);
mappingsTable.getColumnModel().getColumn(0).setPreferredWidth(75);
mappingsTable.getColumnModel().getColumn(1).setPreferredWidth(410);
mappingsTable.getColumnModel().getColumn(2).setPreferredWidth(130);
mappingsTable.getColumnModel().getColumn(3).setPreferredWidth(130);
mappingsTable.getColumnModel().getColumn(4).setPreferredWidth(130);
mappingsTable.setFont(new Font("SansSerif", Font.PLAIN, 12));
mappingsTable.setBackground(Color.WHITE);
Color color = mappingsTable.getGridColor();
// mappingsTable.setBorder(new MatteBorder(0, 0, 0, 0, color));
mappingsTable.setBorder(BorderFactory.createLineBorder(Color.RED,1));
addDataToTable();
}
public JScrollPane setScrollPane(JTable mappingsTable){
spTable = new JScrollPane(mappingsTable);
spTable.setBounds(45, 230, 700, 300);
// spTable.setBorder(BorderFactory.createEmptyBorder());
return spTable;
}
What can i do to either add in a header border or remove the bottom, right, left sections that are not part of the jscrollpane data? Thanks for any help. It seems no matter what i do its never perfectly what i need.
What can i do to either add in a header border
You can add a MatteBorder to the table header. Just specify the border for 3 sides of the component.
You can get the table header from the table
Another option might be to override the getPreferredScrollableViewportSize() method of the JTable. Then you can control the size of the scrollpane.
JTableHeader header = yourTable.getTableHeader();
header.setBorder(new LineBorder(Color.decode("#006699"),2));
I currently have a JFrame to start fullscreen, inside this jframe i have a jpanel, this jpanel includes a vertical scrollpane. Now if i resize my jframe vertically it just kinda removes the bottom part of the jpanel. Is there any way to just shrink the jscrollpane.
im currently using flowlayout for the jframe,
Scrollbar appear automatically when the preferred size of the components added to the scroll pane area greater than the size of the scroll pane.
The FlowLayout will wrap components to a new row, but it always gives the preferred size as the size required to fit the components on a single row, so the preferred height will never change.
To solve this problem you can use the Wrap Layout which simple extend FlowLayout to recalculate the preferred size when wrapping occurs.
The JPanel consists of 3 other panels, a top panel, a scrollpane in the middle and a botpanel. The top and bot panel are just button and checkboxes and stuff
private void initPane() {
createFolderCompPanel();
createBotPanel();
createTopPanel();
createScrollPane();
createTotalPanel();
add(totalPanel);
}
private void createFolderCompPanel() {
//Create folderCompPanel
folderCompPanel = new JPanel();
folderCompPanel.setLayout(new BoxLayout(folderCompPanel, BoxLayout.Y_AXIS));
folderCompPanel.add(Box.createVerticalGlue());
}
private void createTotalPanel() {
//Create TotalPanel
totalPanel = new JPanel();
totalPanel.setLayout(new BoxLayout(totalPanel, BoxLayout.Y_AXIS));
totalPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
totalPanel.add(topPanel);
totalPanel.add(scrollPane);
totalPanel.add(botPanel);
}
private void createScrollPane() {
//Create ScrollPane
scrollPane = new JScrollPane();
scrollPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setViewportBorder(BorderFactory.createEtchedBorder());
scrollPane.getVerticalScrollBar().setUnitIncrement(6);
}
private void createBotPanel() {
//Create BotPanel
botPanel = new JPanel();
botPanel.setLayout(new BoxLayout(botPanel, BoxLayout.X_AXIS));
//AddButton
addButton = new JButton("Add");
addButton.setEnabled(false);
addButton.addActionListener(this);
//SaveButton
saveButton = new JButton("Save");
saveButton.setEnabled(false);
saveButton.addActionListener(this);
//CancelButton
cancelButton = new JButton("Cancel");
cancelButton.setEnabled(false);
cancelButton.addActionListener(this);
lblTotalLength = new JLabel("Total Length: " + totalLength);
botPanel.add(Box.createRigidArea(new Dimension(10, 0)));
botPanel.add(addButton);
botPanel.add(Box.createRigidArea(new Dimension(10, 0)));
botPanel.add(lblTotalLength);
botPanel.add(Box.createHorizontalGlue());
botPanel.add(saveButton);
botPanel.add(Box.createRigidArea(new Dimension(10, 0)));
botPanel.add(cancelButton);
botPanel.add(Box.createRigidArea(new Dimension(10, 0)));
}
private void createTopPanel() {
//Create TopPanel
topPanel = new JPanel();
topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS));
//create deletedisplay button
deleteDisplayButton = new JButton("Delete Display");
deleteDisplayButton.addActionListener(this);
deleteDisplayButton.setEnabled(false);
//create displaybox
displayBox = new JComboBox();
displayBox.addActionListener(this);
displayBox.addItem("<None>");
for (String s : connect.getAllDisplays()) {
displayBox.addItem(s);
}
displayBox.setMaximumSize(displayBox.getPreferredSize());
//create newdisplay button
newDisplayButton = new JButton("New Display");
newDisplayButton.addActionListener(this);
topPanel.add(Box.createRigidArea(new Dimension(10, 0)));
topPanel.add(displayBox);
topPanel.add(Box.createHorizontalGlue());
topPanel.add(newDisplayButton);
topPanel.add(Box.createRigidArea(new Dimension(5, 0)));
topPanel.add(deleteDisplayButton);
topPanel.add(Box.createRigidArea(new Dimension(10, 0)));
}
this is the panel i add to the jframe
public GuiConstructor(){
super(APPLICATION_NAME);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setMinimumSize(new Dimension(630, 600));
setLayout(new FlowLayout(FlowLayout.LEFT));
LoopControlWindow folderSearch = new LoopControlWindow(connect, this);
add(folderSearch);
pack();
setLocationRelativeTo(null);
setResizable(true);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
I guess this is a simple question... basically it's about layout considerations. So let consider the code below, I get this:
.
public class TestCode_Web {
public static void main(String[] args) {
JFrame window = new JFrame("Test");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(200, 300);
// Inner panel ---------------------------------------------------------
JPanel innerPanel = new JPanel(new BorderLayout());
innerPanel.setBackground(new Color(250, 250, 200));
window.add(innerPanel);
// Northern panel ------------------------------------------------------
JPanel panelN = new JPanel(new BorderLayout());
JLabel labelN = new JLabel("Label");
panelN.add(labelN, BorderLayout.WEST);
panelN.setBackground(new Color(200, 250, 250));
innerPanel.add(panelN, BorderLayout.NORTH);
// Center panel --------------------------------------------------------
JPanel panelC = new JPanel();
panelC.setBackground(new Color(250, 200, 250));
JPanel panelCheckBoxes = new JPanel(new GridLayout(0, 1));
final JCheckBox c1 = new JCheckBox("C1");
final JCheckBox c2 = new JCheckBox("C2");
final JCheckBox c3 = new JCheckBox("C3");
panelCheckBoxes.add(c1);
panelCheckBoxes.add(c2);
panelCheckBoxes.add(c3);
int width = panelCheckBoxes.getPreferredSize().width;
int height = panelCheckBoxes.getPreferredSize().height;
panelCheckBoxes.setPreferredSize(new Dimension(width, height));
panelC.add(panelCheckBoxes);
innerPanel.add(panelC, BorderLayout.CENTER);
// Southern panel --------------------------------------------------------
JPanel panelS = new JPanel(new BorderLayout());
JLabel labelS = new JLabel(String.valueOf(width) + "/" + String.valueOf(height));
panelS.add(labelS, BorderLayout.WEST);
panelS.setBackground(new Color(250, 250, 200));
innerPanel.add(panelS, BorderLayout.SOUTH);
// ...
window.setVisible(true);
}
}
What I would like is to have this:
How could I achieve that ? I guesss there are several ways, I'm waiting for your diverse proposals...
One way to do this would be to overwrite the getPreferredSize() method of panelCheckBoxes to return panelC's width. This way, panelCheckBoxes' size will automatically adapt to the width of panelC.
final JPanel panelC = new JPanel();
// [...]
JPanel panelCheckBoxes = new JPanel(new GridLayout(0, 1)) {
public Dimension getPreferredSize() {
return new Dimension(panelC.getWidth(),
super.getPreferredSize().height);
}
};
For accessing panelC inside the anonymous inner class, it has to be final (i.e., after initialization, the panelC variable can not be assigned a new value, which is no problem in your case).
Just setting the preferredSize at that point in the constructor will not work, since (1) the size is not known yet, and (2) it might change when the window is resized. You could, however, use setPreferredSize after the call to window.setVisible(true);, when panelC got a size:
// after window.setVisible(true);
panelCheckBoxes.setPreferredSize(new Dimension(panelC.getWidth(),
panelCheckBoxes.getHeight()));
However, note that this way panelCheckBoxes still won't resize when you resize the window.
If you just want the checkboxes to be aligned to the left (but not necessarily stretch over the whole width), a simpler way would be to put panelC into the WEST container of the BorderLayout. Assuming that the colors are only for debugging and in the end everything will be the same color, you won't see a difference.
Finally, for more complex layouts you might want to check out GridBadLayout. It takes some getting used to, but once mastered, it's worth the effort.