Display different panel size in CardLayout - java

I have a JPanel (cardLayoutPanel) which layout is CardLayout.
I also have another 3 different panels (firstPagePanel, secondPagePanel, thirdPagePanel)
firstPagePanel size is approximately around 450x400,
secondPagePanel size is approximately around 800x600
thirdPagePanel size is approximately around 1024x768
I cardLayoutPanel.add all 3 panels and I show firstPagePanel first as my view.
I want to let my program displayed the size of firstPagePanel first which is 450x400
and then if secondPagePanel is displayed, it will change the size to 800x600 and if thirdPagePanel is displayed,it will change the size to 1024,768
Instead of guessing what my 3 panel size should be, I used frame.getPreferredSize() but my first view will always take in the size of my thirdPagePanel which is 1024x768 instead of 450x400;
What can I do to resolve this issue?
public class MainFrame {
private CardLayout cardLayout = new CardLayout();
private JPanel cardLayoutPanel = new JPanel();
private FirstPagePanel firstPagePanel = new FirstPagePanel();
private SecondPagePanel secondPagePanel = new SecondPagePanel();
private ThirdPagePanel thirdPagePanel = new ThirdPagePanel();
private JFrame frame = new JFrame("Panel size test");
public MainFrame() {
cardLayoutPanel.setLayout(cardLayout);
cardLayoutPanel.add(firstPagePanel,"1");
cardLayoutPanel.add(secondPagePanel,"2");
cardLayoutPanel.add(thirdPagePanel,"3");
cardLayout.show(cardLayoutPanel,"1");
frame.add(cardLayoutPanel);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.getPreferredSize();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setResizable(false);
}
}

The easiest way is to:
Remove the current panel.
Add the new panel.
Pack the top level container.
E.G.
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class ThreeSizeGui {
public static void swapComponentsAndResizeUI(
JComponent ui,
JComponent current,
JComponent next) {
ui.remove(current);
ui.add(next);
current = next;
Component c = ui.getTopLevelAncestor();
if (c instanceof Window) {
Window w = (Window) c;
w.pack();
}
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
final JPanel ui = new JPanel(new BorderLayout(4, 4));
ui.setBorder(new EmptyBorder(6, 6, 6, 6));
JPanel controls = new JPanel(
new FlowLayout(FlowLayout.LEADING));
ui.add(controls, BorderLayout.PAGE_START);
int s = 100;
Dimension[] sizes = {
new Dimension(s * 4, s * 2),
new Dimension(s * 6, s * 3),
new Dimension(s * 8, s * 4)
};
final JComboBox cb = new JComboBox(sizes);
controls.add(cb);
final JPanel[] panels = new JPanel[sizes.length];
for (int ii = 0; ii < sizes.length; ii++) {
Dimension d = sizes[ii];
BufferedImage bi = new BufferedImage(
d.width, d.height, BufferedImage.TYPE_INT_RGB);
JPanel p = new JPanel(new GridLayout());
JLabel l = new JLabel(new ImageIcon(bi));
p.add(l);
panels[ii] = p;
}
ItemListener sizeListener = new ItemListener() {
JPanel current = panels[0];
#Override
public void itemStateChanged(ItemEvent e) {
JPanel next = panels[cb.getSelectedIndex()];
swapComponentsAndResizeUI(ui, current, next);
current = next;
}
};
cb.addItemListener(sizeListener);
ui.add(panels[0], BorderLayout.CENTER);
JFrame f = new JFrame("Three Sized Panels");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setContentPane(ui);
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}

Related

Java Swing GridLayout Change Grid Sizes

I'm trying to create a program that lists movies in a Netflix style to learn Front-End coding.
How I want it to look in the end:
My guess is that every movie is a button component with an image a name label and a release year label.
I'm struggling to recreate this look. This is how it looks when I try it:
The navigationbar in my image is at the page start of a border layout. Below the navigationbar the movie container is in the center of the border layout.
My idea was creating a GridLayout and then create a button for each movie and adding it to the GridLayout.
You can recreate this with this code:
public class Main {
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);
}
public static JPanel createMoviePanel() {
JPanel moviePanel = new JPanel();
GridLayout layout = new GridLayout(0, 10);
layout.setHgap(3);
layout.setVgap(3);
moviePanel.setLayout(layout);
moviePanel.setBackground(new Color(32, 32, 32));
ArrayList<String> exampleList = new ArrayList<>();
// Add stuff to the example list
for(int i = 0; i < 120; i++) {
exampleList.add(Integer.toString(i));
}
final File root = new File("");
for(final String movie : exampleList) {
JLabel picLabel = new JLabel();
try {
File imageFile = new File(root.getAbsolutePath() + "\\src\\images\\" + "imageName.jpg"); // Try to find the cover image
if(imageFile.exists()) {
BufferedImage movieCover = ImageIO.read(imageFile);
picLabel = new JLabel(new ImageIcon(movieCover));
} else {
BufferedImage movieCover = ImageIO.read(new File(root.getAbsolutePath() + "\\src\\images\\temp.jpg")); // Get a temp image
picLabel = new JLabel(new ImageIcon(movieCover));
}
} catch (IOException e) {
e.printStackTrace();
}
JLabel movieName = new JLabel("New Movie");
movieName.setForeground(Color.WHITE);;
JButton movieButton = new JButton();
movieButton.setLayout(new GridLayout(0, 1));
//movieButton.setContentAreaFilled(false);
//movieButton.setBorderPainted(false);
//movieButton.setFocusPainted(false);
movieButton.add(picLabel);
movieButton.add(movieName);
moviePanel.add(movieButton);
}
return moviePanel;
}
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;
}
}
I noticed that the GridLayout always tries to fit everything onto the window.
All that's needed is a properly configured JButton in a GridLayout.
E.G.
public static JPanel createMoviePanel() {
JPanel movieLibraryPanel = new JPanel(new GridLayout(0, 10, 3, 3));
movieLibraryPanel.setBackground(new Color(132, 132, 132));
int m = 5;
BufferedImage image = new BufferedImage(9 * m, 16 * m, BufferedImage.TYPE_INT_RGB);
for (int ii = 1; ii < 21; ii++) {
JButton picButton = new JButton("Mov " + ii, new ImageIcon(image));
picButton.setMargin(new Insets(0,0,0,0));
picButton.setForeground(Color.WHITE);
picButton.setContentAreaFilled(false);
picButton.setHorizontalTextPosition(JButton.CENTER);
picButton.setVerticalTextPosition(JButton.BOTTOM);
movieLibraryPanel.add(picButton);
}
return movieLibraryPanel;
}
Here is a complete source for the above with a tweak to put the year on a new line. It uses HTML in the JButton to break the button text into two lines.
The input focus is on the first button, whereas the mouse hovers over the '2009' movie:
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
class MovieGrid {
MovieGrid() {
JFrame f = new JFrame("Movie Grid");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.add(createMoviePanel());
f.pack();
f.setVisible(true);
}
public static JPanel createMoviePanel() {
JPanel movieLibraryPanel = new JPanel(new GridLayout(0, 10, 3, 3));
movieLibraryPanel.setBackground(new Color(132, 132, 132));
int m = 5;
BufferedImage image = new BufferedImage(
9 * m, 16 * m, BufferedImage.TYPE_INT_RGB);
for (int ii = 2001; ii < 2021; ii++) {
JButton picButton = new JButton(
"<html>Movie<br>" + ii, new ImageIcon(image));
picButton.setMargin(new Insets(0,0,0,0));
picButton.setForeground(Color.WHITE);
picButton.setContentAreaFilled(false);
picButton.setHorizontalTextPosition(JButton.CENTER);
picButton.setVerticalTextPosition(JButton.BOTTOM);
movieLibraryPanel.add(picButton);
}
return movieLibraryPanel;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
new MovieGrid();
}
};
SwingUtilities.invokeLater(r);
}
}
Same idea's from Andrew Thompson answer but with some minor text alignment changes and hover effect
final class Testing
{
public static void main(String[] args)
{
JFrame frame=new JFrame("NEFLIX");
frame.setContentPane(new GridDisplay());
frame.pack();
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private static final class GridDisplay extends JPanel implements ActionListener
{
private GridDisplay()
{
super(new GridLayout(0,5,20,20));
setBackground(new Color(0,0,0,255));
BufferedImage image=new BufferedImage(150,200,BufferedImage.TYPE_INT_RGB);
Graphics2D g2d=(Graphics2D)image.getGraphics();
g2d.setColor(Color.BLUE);
g2d.fillRect(0,0,image.getWidth(),image.getHeight());
HoverPainter painter=new HoverPainter();
for(int i=0;i<10;i++)
{
TVShowCard card=new TVShowCard(image,"Show "+i,"199"+i);
card.addMouseListener(painter);
add(card);
}
}
//highlight only on hover
private final class HoverPainter extends MouseAdapter
{
#Override
public void mouseExited(MouseEvent e) {
((TVShowCard)e.getSource()).setBorderPainted(false);
}
#Override
public void mouseEntered(MouseEvent e) {
((TVShowCard)e.getSource()).setBorderPainted(true);
}
}
private final class TVShowCard extends JButton
{
private TVShowCard(BufferedImage preview,String name,String year)
{
super();
setContentAreaFilled(false);
setBackground(new Color(0,0,0,0));
setFocusPainted(false);
setBorderPainted(false);
//I didn't use image icon & text horizontal alignment because the text always horizontally centered aligned but from the expected output it was left so created 2 labels for the job
setLayout(new GridBagLayout());
addIcon(preview);
addLabel(name,year);
addActionListener(GridDisplay.this);
}
private void addIcon(BufferedImage preview)
{
JLabel icon=new JLabel();
icon.setIcon(new ImageIcon(preview));
add(icon,new GridBagConstraints(0,0,1,1,1.0f,0.0f,GridBagConstraints.WEST,GridBagConstraints.NONE,new Insets(0,0,0,0),0,0));
}
private void addLabel(String name,String year)
{
JLabel label=new JLabel("<html><body>"+name+"<br>"+year+"</body></html>");
label.setForeground(Color.white);
label.setBackground(new Color(0,0,0,0));
add(label,new GridBagConstraints(0,1,1,1,1.0f,1.0f,GridBagConstraints.SOUTHWEST,GridBagConstraints.NONE,new Insets(5,0,0,0),0,0));
}
}
#Override
public void actionPerformed(ActionEvent e)
{
TVShowCard card=(TVShowCard)e.getSource();
//do stuff with it
}
}
}

How to make a JPanel that work like JOptionPane?

I'm making a simple POS system on NetBeans that would pop out a JPanel (quantity) asking for the quantity when the photo of the product is clicked. I am using a card layout and putting the panel inside the card layout doesn't seem to work as it is different in size. It's also very hard to position it since moving it makes a bigger panel (buy) absorb it and becomes a part of the panel, messing up the layout of that panel. I want to make the panel initially invisible and pop up only with this code:
public void mouseClicked(MouseEvent e) {
if (e.getSource() == bpie )
{
String name = "Banoffee Pie";
int price = 8;
quantity.setVisible(true);
}
}
I'm currently a beginner and have a hard time customizing JOptionPane dialogs and prefer the use of panels if possible. The problem could be solved with the use of another JFrame, however, the use of multiple frames according to experts, is bad practice.
Here is how I want the option pane to look:
I'm currently a beginner and have a hard time customizing JOptionPanes
The JOptionPane was made for utility rather than customizability. As soon as you start thinking 'How can I change a JOptionPane to..?' abandon the option pane and instead use a modal JDialog.
Here is an example of using a dialog. I've tweaked the layout along these lines:
The food icons centered below the title bar.
Abandoned the simpler button names for more descriptive ones.
Adding the question and answer in the same line with a spinner to choose the number.
Of course, colors need to be adjusted to suit the style seen above, which might (and might not - depending on further factors not immediately evident) best be approached by using a custom Pluggable Look and Feel.
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.util.Random;
public class SweetShop {
private JComponent ui = null;
private static JFrame frame = new JFrame("Sweet Shop");
private final JDialog dialog = new JDialog(frame, "Choose Sweets", true);
Random random = new Random();
SpinnerNumberModel quantityModel = new SpinnerNumberModel(1, 1, 144, 1);
SweetShop() {
initUI();
}
public final void initUI() {
if (ui!=null) return;
ui = new JPanel(new GridBagLayout());
ui.setBorder(new EmptyBorder(40,100,40,100));
JButton button = new JButton("Buy Sweets");
ui.add(button);
ActionListener openChooserListener = (ActionEvent e) -> {
dialog.setLocationRelativeTo(button);
dialog.setVisible(true);
};
button.addActionListener(openChooserListener);
dialog.add(getSweetSelectionPanel());
dialog.pack();
}
private JPanel getSweetSelectionPanel() {
JPanel panel = new JPanel(new BorderLayout());
int pad = 10;
panel.setBorder(new EmptyBorder(pad, pad, pad, pad));
JPanel iconPanel = new JPanel();
for (int ii=0; ii<12; ii++) {
iconPanel.add(new JLabel(new ImageIcon(getSize16Image())));
}
panel.add(iconPanel, BorderLayout.PAGE_START);
JPanel buttonPanel = new JPanel();
JButton okButton = new JButton("Buy Delicious");
buttonPanel.add(okButton);
ActionListener okListener = (ActionEvent e) -> {
System.out.println("Yuuuummmmm.. x " +
quantityModel.getNumber().intValue());
dialog.setVisible(false);
};
okButton.addActionListener(okListener);
JButton cancelButton = new JButton("No Thanks");
buttonPanel.add(cancelButton);
ActionListener cancelListener = (ActionEvent e) -> {
System.out.println("I just like licking them.");
dialog.setVisible(false);
};
cancelButton.addActionListener(cancelListener);
panel.add(buttonPanel, BorderLayout.PAGE_END);
JPanel questionPanel = new JPanel();
questionPanel.setBorder(new EmptyBorder(20, 50, 20, 50));
panel.add(questionPanel); // automatically uses CENTER constraint
JLabel label = new JLabel("How many do you wish to buy?");
Font font = label.getFont();
label.setFont(font.deriveFont(Font.ITALIC));
label.setText("How many do you wish to buy?");
label.setBorder(new EmptyBorder(5, 5, 5, 5));
questionPanel.add(label);
JSpinner spinner = new JSpinner(quantityModel);
questionPanel.add(spinner);
return panel;
}
private Image getSize16Image() {
int w = 16;
int h = 16;
if (random.nextBoolean()) {
w = random.nextInt(12) + 4;
} else {
h = random.nextInt(12) + 4;
}
BufferedImage bi = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
return bi;
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = () -> {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
SweetShop o = new SweetShop();
frame = new JFrame(o.getClass().getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setContentPane(o.getUI());
frame.pack();
frame.setMinimumSize(frame.getSize());
frame.setVisible(true);
};
SwingUtilities.invokeLater(r);
}
}

Always center the focused JPanel

I got a project where I always want to center the focused JPanel. So I thought I can just change the Viewport position. But I can't use the viewport. I created an example project to show how I use the viewport. I just want that the user only see one of the orange boxes. But it should be also possible to view all boxes at once. So the view has to zoom in or something like this. How can I fix this problem?
My example:
import javax.swing.*;
import java.awt.*;
public class main {
public static void main(String [] args){
//create JFrame
JFrame _frame = new JFrame();
//create Viewport
JViewport _view = new JViewport();
//create Mainpanel
JPanel _mainPanel = new JPanel();
//tell the view to handle mainpanel
_view.setView(_mainPanel);
//create Layout
GridLayout _layout = new GridLayout(3,3,3,3);
//set gridlayout to mainpanel
_mainPanel.setLayout(_layout);
for(int i = 0;i<12;i++){
JPanel _tempPanel = new JPanel();
_tempPanel.setBackground(Color.ORANGE);
_tempPanel.setBorder(BorderFactory.createLineBorder(Color.black));
_mainPanel.add(_tempPanel);
}
_view.setExtentSize(new Dimension(300,300));
//add mainpanel to frame
_frame.add(_mainPanel);
_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
_frame.pack();
//set size of Jframe
_frame.setSize(1000,1000);
_frame.setVisible(true);
}
}
JViewPort can not help you with your requirement.
Here is an ugly but running code. You can improve it yourself.
public static void main(String[] args) {
// create JFrame
JFrame _frame = new JFrame();
JPanel conPanel = new JPanel(new BorderLayout());
// create Mainpanel
JPanel _mainPanel = new JPanel() {
#Override
public String toString() {
return "All";
}
};
// create Layout
GridLayout _layout = new GridLayout(3, 3, 3, 3);
// set gridlayout to mainpanel
_mainPanel.setLayout(_layout);
JComboBox<JPanel> combo = new JComboBox<>();
combo.addItem(_mainPanel);
for (int i = 0; i < 12; i++) {
final int fi = i;
JPanel _tempPanel = new JPanel() {
#Override
public String toString() {
return "Panel" + fi;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString(toString(), 5, 15);
}
};
_tempPanel.setBackground(Color.ORANGE);
_tempPanel.setBorder(BorderFactory.createLineBorder(Color.black));
_mainPanel.add(_tempPanel);
combo.addItem(_tempPanel);
}
combo.addActionListener( e -> {
JPanel panel = (JPanel)combo.getSelectedItem();
conPanel.remove(_mainPanel);
_mainPanel.removeAll();
for(int i = 1; i < combo.getItemCount(); i++)
_mainPanel.add(combo.getItemAt(i));
conPanel.add(panel, BorderLayout.CENTER);
conPanel.revalidate();
conPanel.repaint();
} );
conPanel.add(_mainPanel, BorderLayout.CENTER);
JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
buttonsPanel.add(combo);
conPanel.add(buttonsPanel, BorderLayout.SOUTH);
// add mainpanel to frame
_frame.setContentPane(conPanel);
_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set size of Jframe
_frame.setSize(1000, 1000);
_frame.setVisible(true);
}

Remove huge gaps between check boxes on panel

Its pretty basic UI, but I cannot setup the JCheckBox buttons so that they are placed immediately after one another (vertically) without any spacing. How would I reduce the spacing seen below?
JPanel debugDrawPanel = new JPanel(new GridLayout(0,1));
JPanel eastPanel = new JPanel(new GridLayout(1,0));
JTabbedPane tab = new JTabbedPane();
click = new ClickPanel(this);
setSettings(new Settings());
for (Setting setting: getSettings().getAll()){
JCheckBox checkBox = new JCheckBox(setting.name);
checkBox.setName(setting.name);
checkBox.addItemListener(new CheckBoxItemListener(this));
debugDrawPanel.add(checkBox);
}
tab.addTab("Object Parameters", click);
tab.addTab("Debug Draw", debugDrawPanel);
It appears that the minimum vertical size is being set by the content of another tab. One way to get around that is to put the GridLayout in the PAGE_START of a BorderLayout before putting the panel with border layout into the tabbed pane.
The panel with GridLayout has an orange BG.
The panel with BorderLayout has a yellow BG.
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class TopAlignedComponents {
private JComponent ui = null;
TopAlignedComponents() {
initUI();
}
public void initUI() {
if (ui!=null) return;
ui = new JPanel(new BorderLayout(4,4));
ui.setBorder(new EmptyBorder(4,4,4,4));
JTabbedPane tb = new JTabbedPane();
ui.add(tb);
Image spacer = new BufferedImage(300, 100, BufferedImage.TYPE_INT_RGB);
tb.addTab("Spacer", new JLabel(new ImageIcon(spacer)));
String[] labels = {"Shapes", "Joints", "AABBs"};
JPanel checkPanel = new JPanel(new GridLayout(0, 1, 4, 4));
checkPanel.setBackground(Color.ORANGE);
for (String label : labels) {
checkPanel.add(new JCheckBox(label));
}
JPanel checkConstrain = new JPanel(new BorderLayout());
checkConstrain.setBackground(Color.YELLOW);
checkConstrain.add(checkPanel, BorderLayout.PAGE_START);
tb.addTab("Check", checkConstrain);
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
TopAlignedComponents o = new TopAlignedComponents();
JFrame f = new JFrame("Top Aligned Components");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
If i remember correctly, its because of your layout!
GridLayout divides your windowsize into equal parts, so i think you should either unset your windows size and use pack() or you could switch to a different layout.
( I assume your window's size is or minimum-size is set somewhere )

JDialog vs JOptionPane vs JPanel correct screen resize

I'm having endless problems when I need to show the user some very complex interface with save or cancel buttons and need this interface to correctly deal with different monitor resolutions.
Suppose for instance this interface needs to fit 17 JTextFields and a resizable JTextArea in a 1280 x 768 monitor (my 13'' laptop has vertical size of 760 pixel).
here is an SSCCE:
import java.awt.*;
import javax.swing.*;
public class OptionPanePanel extends JFrame
{
private static Container layoutComponents(String title, float alignment)
{
JPanel container = new JPanel();
BoxLayout layout = new BoxLayout(container, BoxLayout.Y_AXIS);
container.setLayout(layout);
for (int i = 0, n = 7; i < n; i++)
{
JTextField jtextField= new JTextField("jtextfield "+i, n);
jtextField.setAlignmentX(alignment);
container.add(jtextField);
container.add( new javax.swing.Box.Filler(new java.awt.Dimension(0, 20), new java.awt.Dimension(0, 20),
new java.awt.Dimension(32767, 20)));
}
JTextArea jTextArea = new JTextArea(15, 30);
container.add(jTextArea);
for (int i = 6, n = 13; i < n; i++)
{
JTextField jtextField= new JTextField("jtextfield "+i, n);
jtextField.setAlignmentX(alignment);
container.add(jtextField);
container.add( new javax.swing.Box.Filler(new java.awt.Dimension(0, 20), new java.awt.Dimension(0, 20),
new java.awt.Dimension(32767, 20)));
}
return container;
}
public static void main(String args[])
{
Container panel1 = layoutComponents("Left", Component.LEFT_ALIGNMENT);
JOptionPane.showConfirmDialog(
null, panel1, "addRecord", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
}
}
Now I would like the above example to behave so that:
The size of the window resizes not cropping any of the content
The size of the window somehow deals differently based on the resolution of the monitor.
I dont' have to statically specify maximumSize, MinimumSize and preferredSize (with the NetBeans GUI editor for instance) so that each time I have to make numerous tests just to find out the correct size
the JtextArea resizes itself vertically depending on the vertical resolution up to a maximum.
You can add an option pane to a dialog, as shown here and here.
As an aside, call setSize() after pack().
Addendum: Here's a variation of your sscce that places the option pane in a scroll pane having an initial size based on screen geometry, as shown here.
import java.awt.*;
import javax.swing.*;
public class OptionPanePanel extends JFrame {
private static Container layoutComponents(String title, float alignment) {
JPanel container = new JPanel();
BoxLayout layout = new BoxLayout(container, BoxLayout.Y_AXIS);
container.setLayout(layout);
for (int i = 0, n = 7; i < n; i++) {
JTextField jtextField = new JTextField("jtextfield " + i, n);
jtextField.setAlignmentX(alignment);
container.add(jtextField);
container.add(createFiller());
}
JTextArea jTextArea = new JTextArea(15, 30);
container.add(jTextArea);
for (int i = 6, n = 13; i < n; i++) {
JTextField jtextField = new JTextField("jtextfield " + i, n);
jtextField.setAlignmentX(alignment);
container.add(jtextField);
container.add(createFiller());
}
return container;
}
private static Box.Filler createFiller() {
return new Box.Filler(new Dimension(0, 20), new Dimension(0, 20),
new Dimension(Short.MAX_VALUE, 20));
}
public static void main(String args[]) {
Container panel = layoutComponents("Left", Component.LEFT_ALIGNMENT);
final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
JScrollPane jsp = new JScrollPane(panel){
#Override
public Dimension getPreferredSize() {
return new Dimension(320, 2 * screenSize.height / 3);
}
};
JOptionPane optPane = new JOptionPane();
optPane.setMessage(jsp);
optPane.setMessageType(JOptionPane.PLAIN_MESSAGE);
optPane.setOptionType(JOptionPane.OK_CANCEL_OPTION);
JFrame f = new OptionPanePanel();
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
f.add(optPane);
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
}
}
how can I show something like the above so that:
I don't know what that means. Post your SSCCE that shows us exactly the problem you are having.
This works fine for me:
import java.awt.*;
import javax.swing.*;
public class OptionPanePanel extends JFrame
{
public OptionPanePanel()
{
JPanel panel = new JPanel( new BorderLayout() );
JPanel north = new JPanel();
north.add( new JTextField(10) );
north.add( new JTextField(10) );
north.add( new JTextField(10) );
north.add( new JTextField(10) );
north.add( new JTextField(10) );
north.add( new JTextField(10) );
north.add( new JTextField(10) );
JTextArea textArea = new JTextArea(5, 20);
panel.add(north, BorderLayout.NORTH);
panel.add(new JScrollPane(textArea));
int result = JOptionPane.showConfirmDialog(
this, panel, "addRecord", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
}
public static void main(String[] args)
{
JFrame frame = new OptionPanePanel();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setVisible(true);
}
}
Thank you for you answers, so far I came with this solution: get the monitor height and if smaller than 1024 then show a small dialog within a JscrollPane (thanks to trashgod for pointing it), otherwise show a normal dialog with standard height (I have to calculate by trial unfortunately)
import java.awt.*;
import javax.swing.*;
public class OptionPanePanel extends JFrame
{
private static Container layoutComponents(String title, float alignment)
{
JPanel container = new JPanel();
BoxLayout layout = new BoxLayout(container, BoxLayout.Y_AXIS);
container.setLayout(layout);
for (int i = 0, n = 7; i < n; i++)
{
JTextField jtextField = new JTextField("jtextfield " + i, n);
jtextField.setAlignmentX(alignment);
container.add(jtextField);
container.add(createFiller());
}
JTextArea jTextArea = new JTextArea(15, 30);
container.add(jTextArea);
for (int i = 6, n = 13; i < n; i++)
{
JTextField jtextField = new JTextField("jtextfield " + i, n);
jtextField.setAlignmentX(alignment);
container.add(jtextField);
container.add(createFiller());
}
return container;
}
private static Box.Filler createFiller()
{
return new Box.Filler(new Dimension(0, 20), new Dimension(0, 20),
new Dimension(Short.MAX_VALUE, 20));
}
public static void main(String args[])
{
Container panel = layoutComponents("Left", Component.LEFT_ALIGNMENT);
/*Let's check the monitor height in multi monitor setup */
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
int monitorHeight = gd.getDisplayMode().getHeight();
int result;
if (monitorHeight <= 1024)
{
final Dimension preferredDimension = new Dimension(500, monitorHeight - 110);
panel.setPreferredSize(preferredDimension);
JScrollPane jsp = new JScrollPane(panel)
{
#Override
public Dimension getPreferredSize()
{
return new Dimension(500, 700);
}
};
result = JOptionPane.showOptionDialog(null, jsp,
"",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE,
null,
new String[]
{
("save"), ("cancel")
}, // this is the array
"default");
}
else
{
final Dimension preferredDimension = new Dimension(500, 700);
panel.setPreferredSize(preferredDimension);
result = JOptionPane.showOptionDialog(null, panel,
"",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE,
null,
new String[]
{
("save"), ("cancel")
}, // this is the array
"default");
}
if (result == JOptionPane.OK_OPTION)
{
//do something
}
}
}

Categories