Controlling the image quality on the Java application side - java

I posted a question here asking about what to do about a blurry picture in a java application. What i discovered is that my windows scaling was causing the blurriness. Now i have a greater issue. I want the quality of the image and application to be controlled via the application, without having to change any windows settings if at all possible.
Instead of changing the scaling back to 100% there was another windows suggestion from here that said to change the compatibility settings of the java.exe and or javaw.exe to override the high DPI settings to have scaling performed by the system. It works, as in the picture is clearer, but the text loses a crispness to it.
The best quality i have found for the application is to have it not override the high DPI but simply have the scaling set to 100%. The text is crisp and clear and the picture is as well.
I am trying to figure out if there is a way to keep that quality without having to have the users change any of their windows settings to have the application look good. I'm wondering if there is a way in the code to achieve this.
here is an example program with my scaling at 125% The picture is blurry but the text good.
Here is an example of the DPI overridden by the system with scaling still at 125% notice that the text loses a crispness to it but the picture is better.
finally the scaling is 100% and the DPI is not being overridden anymore. this is the best text and picture quality
here is the example code, if anyone needs it.
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Color;
import java.awt.Component;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import java.awt.Insets;
import java.awt.Font;
import java.awt.Dimension;
import javax.swing.JTextArea;
#SuppressWarnings("serial")
public class example extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
example frame = new example();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public example() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 558, 373);
contentPane = new JPanel();
contentPane.setBackground(Color.WHITE);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{0, 0};
gbl_contentPane.rowHeights = new int[]{0, 0};
gbl_contentPane.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{1.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
JPanel panel = new JPanel();
panel.setBackground(Color.WHITE);
GridBagConstraints gbc_panel = new GridBagConstraints();
gbc_panel.fill = GridBagConstraints.BOTH;
gbc_panel.gridx = 0;
gbc_panel.gridy = 0;
contentPane.add(panel, gbc_panel);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[]{0, 0, 0};
gbl_panel.rowHeights = new int[]{0, 0, 0, 100, 0};
gbl_panel.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
JLabel lblWordsGoHere = new JLabel("Words go here");
lblWordsGoHere.setFont(new Font("Cambria Math", Font.PLAIN, 18));
GridBagConstraints gbc_lblWordsGoHere = new GridBagConstraints();
gbc_lblWordsGoHere.insets = new Insets(0, 0, 5, 0);
gbc_lblWordsGoHere.gridx = 1;
gbc_lblWordsGoHere.gridy = 0;
panel.add(lblWordsGoHere, gbc_lblWordsGoHere);
Component horizontalStrut = Box.createHorizontalStrut(20);
horizontalStrut.setPreferredSize(new Dimension(20, 20));
GridBagConstraints gbc_horizontalStrut = new GridBagConstraints();
gbc_horizontalStrut.insets = new Insets(0, 0, 5, 5);
gbc_horizontalStrut.gridx = 0;
gbc_horizontalStrut.gridy = 1;
panel.add(horizontalStrut, gbc_horizontalStrut);
JLabel lblNewLabel = new JLabel("More words here...");
lblNewLabel.setFont(new Font("Cambria Math", Font.PLAIN, 14));
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.insets = new Insets(0, 0, 5, 0);
gbc_lblNewLabel.anchor = GridBagConstraints.WEST;
gbc_lblNewLabel.gridx = 1;
gbc_lblNewLabel.gridy = 1;
panel.add(lblNewLabel, gbc_lblNewLabel);
JLabel lblNewLabel_1 = new JLabel(new ImageIcon(example.class.getResource("/icons/Step3.png")));
GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 0);
gbc_lblNewLabel_1.gridx = 1;
gbc_lblNewLabel_1.gridy = 2;
panel.add(lblNewLabel_1, gbc_lblNewLabel_1);
JTextArea txtrLotsOfDescriptive = new JTextArea();
txtrLotsOfDescriptive.setLineWrap(true);
txtrLotsOfDescriptive.setWrapStyleWord(true);
txtrLotsOfDescriptive.setEditable(false);
txtrLotsOfDescriptive.setFont(new Font("Cambria Math", Font.PLAIN, 14));
txtrLotsOfDescriptive.setText("Lorem ipsum dolor sit amet, eu tempor maluisset eum. Ipsum detracto mediocrem quo eu. Eos ad ocurreret argumentum. Cum ei vero ipsum alienum, has sonet impetus repudiandae at, cu viris assentior eum.");
GridBagConstraints gbc_txtrLotsOfDescriptive = new GridBagConstraints();
gbc_txtrLotsOfDescriptive.fill = GridBagConstraints.BOTH;
gbc_txtrLotsOfDescriptive.gridx = 1;
gbc_txtrLotsOfDescriptive.gridy = 3;
panel.add(txtrLotsOfDescriptive, gbc_txtrLotsOfDescriptive);
}
}

Related

Another GridBagLayout Alignment

I read a lot about this problem here... But I just don't seem to get how to solve this.
All the solutions I tried are not working.
But let's start at the beginning:
I'm building my interface with Swing and trying to be modular.
So I've got a class (extending JPanel) for my left Main Menu.
The Menu is built with several buttons in a GridBagLayout.
But I am not able to get this layout to aligned to the top of the window (panel).
Example: Label at the Top of the Panel, Text Field below, button below the text field, etc.
Please see my code:
public class LeftMenu extends JPanel {
public LeftMenu(){
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[] { 86, 0 };
gbl_panel.rowHeights = new int[] {32, 32, 32, 32, 32, 32, 32, 32, 32 };
gbl_panel.columnWeights = new double[] { 0.0, Double.MIN_VALUE };
gbl_panel.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0 };
setLayout(gbl_panel);
JLabel lblEnterTrunkId = new JLabel("Enter Trunk ID");
GridBagConstraints gbc_lblEnterTrunkId = new GridBagConstraints();
gbc_lblEnterTrunkId.fill = GridBagConstraints.HORIZONTAL;
gbc_lblEnterTrunkId.insets = new Insets(0, 0, 5, 0);
gbc_lblEnterTrunkId.gridx = 0;
gbc_lblEnterTrunkId.gridy = 0;
gbc_lblEnterTrunkId.anchor = GridBagConstraints.NORTH;
add(lblEnterTrunkId, gbc_lblEnterTrunkId);
}
}
There is a text field and some buttons following behind the Label.
But I assume, these are not relevant...
If they are... they are mostly looking like the Label (just that they are not Labels... I think you get me)
All guides I read, are all pointing to the anchor of the GridBagConstraint. It is there.... but not working.
It's wonderfully aligned in the middle of the panel.
If it does matter:
the Panel ist used as a LeftComponent of a SplitPane:
public LeftMenu leftpanel = new LeftMenu();
splitPaneTrunks.setLeftComponent(leftpanel);
Looking forward to your help.
Here is a picture of my side menu... centered horizontally. as it should not be.
Instead of using GridBagLayout, you could make a wrap using a JPanel with the layout BorderLayout, like so:
setLayout(new BorderLayout());
JPanel gridBagWrap = new JPanel();
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[] { 86, 0 };
gbl_panel.rowHeights = new int[] {32, 32, 32, 32, 32, 32, 32, 32, 32 };
gbl_panel.columnWeights = new double[] { 0.0, Double.MIN_VALUE };
gbl_panel.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0 };
gridBagWrap.setLayout(gbl_panel);
JLabel lblEnterTrunkId = new JLabel("Enter Trunk ID");
GridBagConstraints gbc_lblEnterTrunkId = new GridBagConstraints();
gbc_lblEnterTrunkId.fill = GridBagConstraints.HORIZONTAL;
gbc_lblEnterTrunkId.insets = new Insets(0, 0, 5, 0);
gbc_lblEnterTrunkId.gridx = 0;
gbc_lblEnterTrunkId.gridy = 0;
gbc_lblEnterTrunkId.anchor = GridBagConstraints.NORTH;
gridBagWrap.add(lblEnterTrunkId, gbc_lblEnterTrunkId);
add(gridBagWrap, BorderLayout.NORTH);
here is a picture of my side menu... centered horizontally. as it should not be.
I think you mean it should not be centered vertically if you want the components displayed from the top.
In any case I think the problem is your weighty constraint. It needs to be non-zero for at least one of the components otherwise the components will be centered vertically.
Read the section from the Swing tutorial on How to Use GridBagLayout. There is a section on the weightx/weighty constraints that will explain this in more detail.
There is no surprise that you are lost with GridBagLayout manager.
It is in fact one of its most commonly mentioned features. GridBagLayout comes from the nineties and uses pixel-fixed gaps between components, which is not portable. With this manager, you have to tediously define each cell of the layout; no wonder people are confused.
I recommend to use MigLayout. And if it cannot be used, then GroupLayout.
Here is a solution with MigLayout:
package com.zetcode;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import net.miginfocom.swing.MigLayout;
/*
Demonstration of MigLayout manager.
Author: Jan Bodnar
Website: zetcode.com
*/
public class MigLayoutTrunkEx extends JFrame {
public MigLayoutTrunkEx() {
initUI();
}
private void initUI() {
JLabel lbl = new JLabel("Enter Trunk ID");
JTextField textField = new JTextField(10);
JButton btn1 = new JButton("A");
JButton btn2 = new JButton("B");
JButton btn3 = new JButton("C");
JButton btn4 = new JButton("D");
JButton btn5 = new JButton("E");
JTextArea area = new JTextArea(20, 25);
area.setBorder(BorderFactory.createEtchedBorder());
JLabel statusBar = new JLabel("Ready");
createLayout(lbl, textField, btn1, btn2, btn3,
btn4, btn5, area, statusBar);
setTitle("MigLayout example");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void createLayout(JComponent... arg) {
setLayout(new MigLayout("", "[align center]", ""));
add(arg[0], "split 7, flowy");
add(arg[1]);
add(arg[2]);
add(arg[3]);
add(arg[4]);
add(arg[5]);
add(arg[6]);
add(arg[7], "grow, push, wrap");
add(arg[8], "align left, growx, spanx");
pack();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
MigLayoutTrunkEx ex = new MigLayoutTrunkEx();
ex.setVisible(true);
});
}
}
It is easy, once you get to know MigLayout. Ten or fifteen minutes of work.
Screenshot:

How to get CardLayout implemented properly with Eclipse WindowBuilder?

So using WindowBuilder (latest version of Ecliple, Kepler) I've created a frame as so:
But I'd like to switch between them with a button, which I've created on the panelWelcome.
I'm guessing I'd add an itemListener, then create a method that switches between the cards.
The problem is, I have no idea how to proceed after that. Here's the code that's automatically generated:
package client;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.CardLayout;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
public class Test {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test window = new Test();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Test() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new CardLayout(0, 0));
JPanel panelWelcome = new JPanel();
frame.getContentPane().add(panelWelcome, "name_98933171901972");
GridBagLayout gbl_panelWelcome = new GridBagLayout();
gbl_panelWelcome.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0};
gbl_panelWelcome.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gbl_panelWelcome.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
gbl_panelWelcome.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
panelWelcome.setLayout(gbl_panelWelcome);
JLabel lblTitle = new JLabel("MEMEPlayer");
lblTitle.setFont(new Font("Segoe UI", Font.BOLD, 12));
GridBagConstraints gbc_lblTitle = new GridBagConstraints();
gbc_lblTitle.insets = new Insets(0, 0, 5, 0);
gbc_lblTitle.gridx = 5;
gbc_lblTitle.gridy = 0;
panelWelcome.add(lblTitle, gbc_lblTitle);
JLabel lblNewLabel = new JLabel("Welcome! To get started, select a movie from the drop down menu");
lblNewLabel.setFont(new Font("Segoe UI", Font.PLAIN, 11));
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.insets = new Insets(0, 0, 5, 0);
gbc_lblNewLabel.gridx = 5;
gbc_lblNewLabel.gridy = 2;
panelWelcome.add(lblNewLabel, gbc_lblNewLabel);
JComboBox comboBox = new JComboBox();
comboBox.setModel(new DefaultComboBoxModel(new String[] {"The Avengers (2012)", "Monsters, Inc. (2001)", "Prometheus (2012)"}));
GridBagConstraints gbc_comboBox = new GridBagConstraints();
gbc_comboBox.insets = new Insets(0, 0, 5, 0);
gbc_comboBox.gridx = 5;
gbc_comboBox.gridy = 4;
panelWelcome.add(comboBox, gbc_comboBox);
JButton btnNewButton = new JButton("Next >");
GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
gbc_btnNewButton.insets = new Insets(0, 0, 5, 0);
gbc_btnNewButton.gridx = 5;
gbc_btnNewButton.gridy = 6;
btnNewButton.addItemListener((ItemListener) this);
panelWelcome.add(btnNewButton, gbc_btnNewButton);
JLabel lblNewLabel_1 = new JLabel("");
lblNewLabel_1.setIcon(new ImageIcon("C:\\temp\\Meme1\\largeVLC.png"));
GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
gbc_lblNewLabel_1.gridx = 5;
gbc_lblNewLabel_1.gridy = 8;
panelWelcome.add(lblNewLabel_1, gbc_lblNewLabel_1);
JPanel panelVideo = new JPanel();
frame.getContentPane().add(panelVideo, "name_98968999152440");
}
}
Thanks for any help!
The problem I see you're facing is that you are setting the layout to the frame. This is a problem because it means that the frame can only have on visible component at a time. That component being one of the panels. So you can put a button. The button would have to be on the one of the panels, which may be hard to maintain, in terms of navigation.
So instead, have a main JPanel that has the CardLayout, and add your card panels to that main panel. Then you can add the main panel to the frame, along with buttons for navigation.
Another option is to have a menu bar with option to change the cards, then that way, you can keep the card layout on the frame, because navigation is controlled by the menu options.
See How to Use CardLayout if you're not really sure even how to use CardLayout, hand coding. You're going to need a reference to layout and call one of its navigation methods like show(), next(), or previous()
You may also find this post interesting. It used Netbeans, but maybe you'll pick something up

How to resize JPanel when JFrame is maximized?

I am new to java swing, recently I try to create a swing app to format text.
When I click the maximum button, the text panel's length does not resize, and the middle panel takes large space.
And seems setResizable(false) does not work
Code
public class MainFrame extends JFrame {
private static final long serialVersionUID = 7553142908344084288L;
private JTextArea fromTextArea;
private JTextArea toTextArea;
public MainFrame() {
super("jFormatter");
Panel mainPanel = new Panel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));
setContentPane(mainPanel);
fromTextArea = createTextArea();
lines.setBorder(BorderFactory.createMatteBorder(0, 1, 0, 1, Color.LIGHT_GRAY));
lines.setEditable(false);
Font f = new Font(Font.SANS_SERIF, Font.PLAIN, 16);
lines.setFont(f);
JScrollPane fromTextAreaScrollPanel = new JScrollPane(fromTextArea);
fromTextAreaScrollPanel.setBorder(BorderFactory.createEmptyBorder(15, 5, 15, 5));
fromTextAreaScrollPanel.getViewport().add(fromTextArea);
fromTextAreaScrollPanel.setRowHeaderView(lines);
mainPanel.add(fromTextAreaScrollPanel);
// control panel
mainPanel.add(createCtrlPanel());
toTextArea = createTextArea();
mainPanel.add(createTextAreaPanel(toTextArea));
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setResizable(false);
setVisible(true);
setLocationRelativeTo(null);
}
private JPanel createCtrlPanel() {
final JComboBox jComboBox = new JComboBox(formatters.keySet().toArray());
jComboBox.setBorder(BorderFactory.createTitledBorder("Text Format"));
JButton fmtButton = new JButton("Format >>");
JPanel ctrPanel = new JPanel(new GridBagLayout());
ctrPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
ctrPanel.add(jComboBox, gbc);
ctrPanel.add(Box.createRigidArea(new Dimension(50, 15)), gbc);
//gbc.fill = GridBagConstraints.NONE;
ctrPanel.add(fmtButton, gbc);
return ctrPanel;
}
private JScrollPane createTextAreaPanel(JTextArea textArea) {
JScrollPane fromTextAreaScrollPanel = new JScrollPane(textArea);
//fromTextAreaScrollPanel.setPreferredSize(new Dimension(300, 300));
fromTextAreaScrollPanel.setBorder(BorderFactory.createEmptyBorder(15, 5, 15, 5));
return fromTextAreaScrollPanel;
}
private JTextArea createTextArea() {
JTextArea textArea = new JTextArea(20, 40);
Font f = new Font(Font.SANS_SERIF, Font.PLAIN, 16);
textArea.setFont(f);
//fromTextArea.setLineWrap(true);
//textArea.setBackground(Color.LIGHT_GRAY);
textArea.setMargin(new Insets(0, 10, 0, 10));
return textArea;
}
public static void main(String[] args) {
new MainFrame();
}
}
result:
You should probably use BorderLayout or GridBagLayout for this. BoxLayout just lays out components one after the other at their preferred size. It doesn't make any attempt to resize the components or make them fill their parent.
Try to make a layout like this:
Code:
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
public class Test extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test frame = new Test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0};
gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
JScrollPane scrollPane = new JScrollPane();
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
gbc_scrollPane.gridheight = 2;
gbc_scrollPane.insets = new Insets(0, 0, 0, 5);
gbc_scrollPane.fill = GridBagConstraints.BOTH;
gbc_scrollPane.gridx = 0;
gbc_scrollPane.gridy = 0;
gbc_scrollPane.weightx=1;
contentPane.add(scrollPane, gbc_scrollPane);
JTextArea textArea = new JTextArea();
scrollPane.setViewportView(textArea);
JPanel panel = new JPanel();
GridBagConstraints gbc_panel = new GridBagConstraints();
gbc_panel.gridheight = 2;
gbc_panel.insets = new Insets(0, 0, 5, 5);
//gbc_panel.fill = GridBagConstraints.BOTH;
gbc_panel.gridx = 1;
gbc_panel.gridy = 0;
contentPane.add(panel, gbc_panel);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[]{0, 0, 0, 0};
gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0};
gbl_panel.columnWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
JComboBox comboBox = new JComboBox();
GridBagConstraints gbc_comboBox = new GridBagConstraints();
gbc_comboBox.insets = new Insets(0, 0, 5, 0);
gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox.gridx = 2;
gbc_comboBox.gridy = 0;
gbc_comboBox.weightx=0.0;
panel.add(comboBox, gbc_comboBox);
JButton btnNewButton = new JButton(">>");
GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
gbc_btnNewButton.insets = new Insets(0, 0, 5, 0);
gbc_btnNewButton.gridx = 2;
gbc_btnNewButton.gridy = 1;
panel.add(btnNewButton, gbc_btnNewButton);
JScrollPane scrollPane_1 = new JScrollPane();
GridBagConstraints gbc_scrollPane_1 = new GridBagConstraints();
gbc_scrollPane_1.gridheight = 2;
gbc_scrollPane_1.fill = GridBagConstraints.BOTH;
gbc_scrollPane_1.gridx = 2;
gbc_scrollPane_1.gridy = 0;
gbc_scrollPane_1.weightx=1;
contentPane.add(scrollPane_1, gbc_scrollPane_1);
JTextArea textArea_1 = new JTextArea();
scrollPane_1.setViewportView(textArea_1);
pack();
}
}

Nested Layouts (GridBagLayout inside a FlowLayout)

How can I position my labels with this code? It seems that gridbaglayout is not working here, especially the gridbagconstraints. Even if I change the gridx and gridy values, the labels are not moving.
I need to make it in like 3 even columns, 1 column = 1 panel
package nest;
import javax.swing.*;
import java.awt.*;
public class nested extends JFrame{
public static void main(String[] args) {
JFrame f=new JFrame("Bio Data");
JPanel p1=new JPanel(new GridBagLayout());
JPanel p2=new JPanel(new GridBagLayout());
//JPanel p3=new JPanel(new GridBagLayout());
GridBagConstraints c1=new GridBagConstraints();
GridBagConstraints c2=new GridBagConstraints();
JLabel l1=new JLabel("aa");
JLabel l2=new JLabel("bb");
c1.gridx=1;
c1.gridy=1;
p1.add(l1, c1);
c2.gridx=4;
c2.gridy=4;
p2.add(l2, c2);
f.add(p1);
f.add(p2);
f.setVisible(true);
f.setSize(800,500);
f.setResizable(false);
f.setLayout(new FlowLayout());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Changing gridx and gridy does not change the actual position of the labels, but rather the order/format that the labels will be displayed in. If you have something in gridx 4 but nothing in gridx 1,2 or 3 then that object will be the first column.
Also, it woulld be very odd to put a new panel in each column since you can put things in different rows of the gridBagLayout. Here is a short example of a gridBagLayout inside a flowLayout with three different columns of labels:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import java.awt.Insets;
public class nest extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
nest frame = new nest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public nest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new GridLayout(0, 1, 0, 0));
JPanel panel = new JPanel();
contentPane.add(panel);
panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
JPanel panel_1 = new JPanel();
panel.add(panel_1);
GridBagLayout gbl_panel_1 = new GridBagLayout();
gbl_panel_1.columnWidths = new int[]{0, 0, 27, 0};
gbl_panel_1.rowHeights = new int[]{0, 16, 0};
gbl_panel_1.columnWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
gbl_panel_1.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
panel_1.setLayout(gbl_panel_1);
JLabel lblNewLabel_1 = new JLabel("New label");
GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
gbc_lblNewLabel_1.gridx = 0;
gbc_lblNewLabel_1.gridy = 0;
panel_1.add(lblNewLabel_1, gbc_lblNewLabel_1);
JLabel lblNewLabel = new JLabel("New label");
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
gbc_lblNewLabel.gridx = 1;
gbc_lblNewLabel.gridy = 0;
panel_1.add(lblNewLabel, gbc_lblNewLabel);
JLabel lblNewLabel_2 = new JLabel("New label");
GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints();
gbc_lblNewLabel_2.insets = new Insets(0, 0, 5, 0);
gbc_lblNewLabel_2.gridx = 2;
gbc_lblNewLabel_2.gridy = 0;
panel_1.add(lblNewLabel_2, gbc_lblNewLabel_2);
JLabel lblLabel = new JLabel("New label");
GridBagConstraints gbc_lblLabel = new GridBagConstraints();
gbc_lblLabel.gridx = 2;
gbc_lblLabel.gridy = 1;
panel_1.add(lblLabel, gbc_lblLabel);
}
}
I created this with the Google WindowBuilder. Note that you don't necessarily need to set the columnWidths, rowHeights, columnWeights, or rowWeights (and it is better not to sometimes when you have dynamic content). Hopefully this helps.

Java GUI - JTextArea expanding but not contracting

MAIN PROBLEM: In a JScrollPane with JPanel which contains a JTextArea, text wraps up if GUI is expanded but text does not wrap back when GUI is contracted. See example below
Okay I am building the GUI for an app I am currently working on and I am having a bit of a problem.
The explanation: My GUI is structured as illustrated below:
And this is what it looks like.
Upon expansion the the JTextArea inside the panelWithText expands and resizes the text as such:
But the problem is what happens when you make the GUI smaller. The "problem" is that I want the text to warp back as it was before. I did a little experimenting by implementing a ComponentListener to both the JScrollPane and the panelWithText and found out that componentResized is being called for panelWithText upon expansion but not for contraction. Is there any way to implement the behavior of the text warping back in the panelWithText Component?
PS: Apparently if I switch the JScrollPane with a regular JPanel it works. But I can't do that! I have a LOT of panelWithText to show to the user.
PS PS: Sorry here is the code I am using.
JFrameExt.java
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.FlowLayout;
import java.awt.Window.Type;
import javax.swing.ScrollPaneConstants;
import java.awt.CardLayout;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
public class JFrameExt extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JFrameExt frame = new JFrameExt();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JFrameExt() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 246, 164);
contentPane = new JPanel();
contentPane.setBorder(null);
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportBorder(null);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
contentPane.add(scrollPane, BorderLayout.CENTER);
JPanel panel = new JPanel();
scrollPane.setViewportView(panel);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[]{0, 0};
gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_panel.rowWeights = new double[]{0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
panelWithText panelWithText_ = new panelWithText();
GridBagConstraints gbc_panelWithText_ = new GridBagConstraints();
gbc_panelWithText_.anchor = GridBagConstraints.NORTH;
gbc_panelWithText_.insets = new Insets(0, 0, 5, 0);
gbc_panelWithText_.fill = GridBagConstraints.HORIZONTAL;
gbc_panelWithText_.gridx = 0;
gbc_panelWithText_.gridy = 0;
panel.add(panelWithText_, gbc_panelWithText_);
panelWithText panelWithText__1 = new panelWithText();
GridBagConstraints gbc_panelWithText__1 = new GridBagConstraints();
gbc_panelWithText__1.insets = new Insets(0, 0, 5, 0);
gbc_panelWithText__1.anchor = GridBagConstraints.NORTH;
gbc_panelWithText__1.fill = GridBagConstraints.HORIZONTAL;
gbc_panelWithText__1.gridx = 0;
gbc_panelWithText__1.gridy = 1;
panel.add(panelWithText__1, gbc_panelWithText__1);
panelWithText panelWithText__2 = new panelWithText();
GridBagConstraints gbc_panelWithText__2 = new GridBagConstraints();
gbc_panelWithText__2.insets = new Insets(0, 0, 5, 0);
gbc_panelWithText__2.fill = GridBagConstraints.BOTH;
gbc_panelWithText__2.gridx = 0;
gbc_panelWithText__2.gridy = 2;
panel.add(panelWithText__2, gbc_panelWithText__2);
panelWithText panelWithText__3 = new panelWithText();
GridBagConstraints gbc_panelWithText__3 = new GridBagConstraints();
gbc_panelWithText__3.insets = new Insets(0, 0, 5, 0);
gbc_panelWithText__3.fill = GridBagConstraints.BOTH;
gbc_panelWithText__3.gridx = 0;
gbc_panelWithText__3.gridy = 3;
panel.add(panelWithText__3, gbc_panelWithText__3);
panelWithText panelWithText__4 = new panelWithText();
GridBagConstraints gbc_panelWithText__4 = new GridBagConstraints();
gbc_panelWithText__4.insets = new Insets(0, 0, 5, 0);
gbc_panelWithText__4.fill = GridBagConstraints.BOTH;
gbc_panelWithText__4.gridx = 0;
gbc_panelWithText__4.gridy = 4;
panel.add(panelWithText__4, gbc_panelWithText__4);
panelWithText panelWithText__5 = new panelWithText();
GridBagConstraints gbc_panelWithText__5 = new GridBagConstraints();
gbc_panelWithText__5.insets = new Insets(0, 0, 5, 0);
gbc_panelWithText__5.fill = GridBagConstraints.BOTH;
gbc_panelWithText__5.gridx = 0;
gbc_panelWithText__5.gridy = 5;
panel.add(panelWithText__5, gbc_panelWithText__5);
panelWithText panelWithText__6 = new panelWithText();
GridBagConstraints gbc_panelWithText__6 = new GridBagConstraints();
gbc_panelWithText__6.fill = GridBagConstraints.BOTH;
gbc_panelWithText__6.gridx = 0;
gbc_panelWithText__6.gridy = 6;
panel.add(panelWithText__6, gbc_panelWithText__6);
setSize(300,100);
}
}
panelWithText.java
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JTextArea;
import javax.swing.ImageIcon;
import java.awt.BorderLayout;
public class panelWithText extends JPanel {
/**
* Create the panel.
*/
public void me_resized(Dimension d){
System.out.println("CALLED..");
super.setPreferredSize(d);
}
public panelWithText() {
setBackground(Color.DARK_GRAY);
setForeground(Color.WHITE);
setLayout(new BorderLayout(0, 0));
JTextArea txtrIveBeenReading = new JTextArea();
txtrIveBeenReading.setEditable(false);
txtrIveBeenReading.setColumns(28);
txtrIveBeenReading.setFont(new Font("Tahoma", Font.PLAIN, 10));
txtrIveBeenReading.setLineWrap(true);
txtrIveBeenReading.setWrapStyleWord(true);
txtrIveBeenReading.setText("\n A bunch of really important text here... A bunch of really important text here... A bunch of really important text here... A bunch of really important text here...\n");
txtrIveBeenReading.setForeground(Color.WHITE);
txtrIveBeenReading.setBackground(Color.DARK_GRAY);
add(txtrIveBeenReading, BorderLayout.CENTER);
}
}
After a little playing around, I came up with this...
public class Test {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JFrameExt frame = new JFrameExt();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public static class ScrollablePane extends JPanel implements Scrollable {
#Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(100, 100);
}
#Override
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
return 64;
}
#Override
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
return 128;
}
#Override
public boolean getScrollableTracksViewportWidth() {
return true;
}
#Override
public boolean getScrollableTracksViewportHeight() {
return false;
}
}
public static class JFrameExt extends JFrame {
private JPanel contentPane;
/**
* Create the frame.
*/
public JFrameExt() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 246, 164);
contentPane = new JPanel();
contentPane.setBorder(null);
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportBorder(null);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
contentPane.add(scrollPane, BorderLayout.CENTER);
JPanel panel = new ScrollablePane();
scrollPane.setViewportView(panel);
GridBagLayout gbl_panel = new GridBagLayout();
// gbl_panel.columnWidths = new int[]{0, 0};
// gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
// gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
// gbl_panel.rowWeights = new double[]{0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
panelWithText panelWithText_ = new panelWithText();
GridBagConstraints gbc_panelWithText_ = new GridBagConstraints();
gbc_panelWithText_.anchor = GridBagConstraints.NORTH;
gbc_panelWithText_.insets = new Insets(0, 0, 5, 0);
gbc_panelWithText_.fill = GridBagConstraints.HORIZONTAL;
gbc_panelWithText_.gridx = 0;
gbc_panelWithText_.gridy = 0;
gbc_panelWithText_.weightx = 1;
panel.add(panelWithText_, gbc_panelWithText_);
// panelWithText panelWithText__1 = new panelWithText();
// GridBagConstraints gbc_panelWithText__1 = new GridBagConstraints();
// gbc_panelWithText__1.insets = new Insets(0, 0, 5, 0);
// gbc_panelWithText__1.anchor = GridBagConstraints.NORTH;
//// gbc_panelWithText__1.fill = GridBagConstraints.HORIZONTAL;
// gbc_panelWithText__1.gridx = 0;
// gbc_panelWithText__1.gridy = 1;
// panel.add(panelWithText__1, gbc_panelWithText__1);
//
// panelWithText panelWithText__2 = new panelWithText();
// GridBagConstraints gbc_panelWithText__2 = new GridBagConstraints();
// gbc_panelWithText__2.insets = new Insets(0, 0, 5, 0);
//// gbc_panelWithText__2.fill = GridBagConstraints.BOTH;
// gbc_panelWithText__2.gridx = 0;
// gbc_panelWithText__2.gridy = 2;
// panel.add(panelWithText__2, gbc_panelWithText__2);
//
// panelWithText panelWithText__3 = new panelWithText();
// GridBagConstraints gbc_panelWithText__3 = new GridBagConstraints();
// gbc_panelWithText__3.insets = new Insets(0, 0, 5, 0);
//// gbc_panelWithText__3.fill = GridBagConstraints.BOTH;
// gbc_panelWithText__3.gridx = 0;
// gbc_panelWithText__3.gridy = 3;
// panel.add(panelWithText__3, gbc_panelWithText__3);
//
// panelWithText panelWithText__4 = new panelWithText();
// GridBagConstraints gbc_panelWithText__4 = new GridBagConstraints();
// gbc_panelWithText__4.insets = new Insets(0, 0, 5, 0);
//// gbc_panelWithText__4.fill = GridBagConstraints.BOTH;
// gbc_panelWithText__4.gridx = 0;
// gbc_panelWithText__4.gridy = 4;
// panel.add(panelWithText__4, gbc_panelWithText__4);
//
// panelWithText panelWithText__5 = new panelWithText();
// GridBagConstraints gbc_panelWithText__5 = new GridBagConstraints();
// gbc_panelWithText__5.insets = new Insets(0, 0, 5, 0);
//// gbc_panelWithText__5.fill = GridBagConstraints.BOTH;
// gbc_panelWithText__5.gridx = 0;
// gbc_panelWithText__5.gridy = 5;
// panel.add(panelWithText__5, gbc_panelWithText__5);
//
// panelWithText panelWithText__6 = new panelWithText();
// GridBagConstraints gbc_panelWithText__6 = new GridBagConstraints();
//// gbc_panelWithText__6.fill = GridBagConstraints.BOTH;
// gbc_panelWithText__6.gridx = 0;
// gbc_panelWithText__6.gridy = 6;
// panel.add(panelWithText__6, gbc_panelWithText__6);
setSize(300, 100);
}
}
public static class panelWithText extends JPanel {
/**
* Create the panel.
*/
public void me_resized(Dimension d) {
System.out.println("CALLED..");
super.setPreferredSize(d);
}
public panelWithText() {
setBackground(Color.DARK_GRAY);
setForeground(Color.WHITE);
setLayout(new BorderLayout(0, 0));
JTextArea txtrIveBeenReading = new JTextArea();
txtrIveBeenReading.setEditable(false);
txtrIveBeenReading.setColumns(28);
txtrIveBeenReading.setFont(new Font("Tahoma", Font.PLAIN, 10));
txtrIveBeenReading.setLineWrap(true);
txtrIveBeenReading.setWrapStyleWord(true);
txtrIveBeenReading.setText("\n A bunch of really important text here... A bunch of really important text here... A bunch of really important text here... A bunch of really important text here...\n");
txtrIveBeenReading.setForeground(Color.WHITE);
txtrIveBeenReading.setBackground(Color.DARK_GRAY);
add(txtrIveBeenReading, BorderLayout.CENTER);
}
}
}
Basically, you need a container that implements the Scrollable interface. This will allow you to specify that the container should track/match the view ports width. This will cause the container to be laid out when ever the view port changes size...
As a side note, you can use a single copy of the GridBagConstraints, when you add each new component to the container, the GridBagLayout will generate a copy of it's own. This is very powerful when you want to share properties of the GridBagConstraints between components ;)

Categories

Resources