JTextField showing through JComboBox's dropdown after Scrolling - java

When a JTextField is in a JScrollPanel, if the panel has been scrolled, whenever the dropdown from a JComboBox is over the JTextField, the text field shows through the dropdown.
This only happens after the content has been scrolled (not on startup of the application).
The main question is how can we fix this?
Bonus points if the answer:
Is not a hack
Explains why is it happening in the first place
Things I've tried:
Moving the dropdown outside of the scrollpane (no change)
Adding a repaint to any and every container I could find on scroll (no change)
Different Layout managers for the content of the scrollpane (no change)
Code Example:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TextFieldShowsThrough{
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createScrollDemo());
frame.pack();
// For demonstration purposes
frame.setSize(frame.getWidth() + 100, frame.getHeight() - 100);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static JScrollPane createScrollDemo(){
final Box optionsPanel = Box.createVerticalBox();
optionsPanel.add(createDropDown());
optionsPanel.add(createTextField("Option1"));
optionsPanel.add(createTextField("Option2"));
optionsPanel.add(createTextField("Option3"));
optionsPanel.add(createTextField("Option4"));
optionsPanel.add(createTextField("Option5"));
optionsPanel.add(Box.createVerticalGlue());
JScrollPane result = new JScrollPane(optionsPanel);
// Made attempts to fix here, but to no avail
/*result.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
#Override
public void adjustmentValueChanged(AdjustmentEvent e) {
result.repaint();
}
});*/
return result;
}
public static Box createDropDown(){
Box b = Box.createVerticalBox();
b.setAlignmentX(JLabel.LEFT_ALIGNMENT);
b.add(new JLabel("Language"));
JComboBox combo = new JComboBox(new String[]{"en", "fr", "es"});
combo.setMaximumSize(new Dimension(500, 25));
b.add(combo);
return b;
}
public static Box createTextField(String label){
Box mainBox = Box.createVerticalBox();
mainBox.setOpaque(true);
mainBox.setBackground(new Color((int)(Math.random() * 0x1000000))); // because fun
JLabel jLabel = new JLabel(label);
jLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);
mainBox.add(jLabel);
Box secondaryBox = Box.createHorizontalBox();
secondaryBox.setAlignmentX(JLabel.LEFT_ALIGNMENT);
TextField tf = new TextField();
tf.setMaximumSize(new Dimension(500, 25));
secondaryBox.add(tf);
mainBox.add(secondaryBox);
return mainBox;
}
}

That's because you're using a java.awt.TextField, which is heavy weight component, inside a light weight container. The popup window used by the JComboBox can also be a light weight component.
AWT components don't play well with Swing components, they have z-ordering issues.
Change TextField tf = new TextField(); to JTextField tf = new JTextField();
You should also avoid using setPreferred/Minimum/MaximumSize (see Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? for more details) and instead use layout constraints and sizing hints (like the columns property of the JTextField)

Related

How can I start a newline in a JPanel when the added text are two different font sizes

So I am creating a project that is a skeleton of a Java GUI but I am having some alignment issues. When I run my code the centered top text that says "Help Page" is pushed to the left side, while the help string is shifted downwards a little bit but also pushed to the right.
My goal is to have the top text centered and underlined with the other text below it and also centered. I have tried using multiple panels but still nothing has worked, Im guessing it's the mismatching font size by I dont know. Any help is appreciated!
private void helpGUI() {
clearGUI();
helpStr = "<html><br>This is the help page where the user can come for help<html/>";
label = new JLabel("<html><u>Help Page</u></html>");
label.setFont(new Font("Times", Font.PLAIN, 24));
helpTxt = new JLabel(helpStr);
helpTxt.setFont(new Font("Times", Font.PLAIN, 16));
panel.add(label);
panel.add(helpTxt);
panel.setAlignmentX(CENTER_ALIGNMENT);
button = new JButton("Previous");
bttnPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
bttnPanel.add(button);
frame.add(panel);
class previousButton implements ActionListener {
public void actionPerformed (ActionEvent e) {
GUIPG1(name);
}
}
button.addActionListener(new previousButton());
}
It really depends on what you are trying to achieve (for instance, what other components is that JPanel supposed to contain. Is it just those two labels? You show a button in your code as well. Where is that supposed to be added?). Regardless, for that specific panel with the two texts on the top, you could use BoxLayout for adding your JLabels vertically, and use setAlignmentX() to set the horizontal alignment of the texts. Example below:
Edit:
Alternatively (regarding underlying and centering the text), you can use the following in the example below:
titleLbl = new JLabel("<html><u>Help Page</u></html>", SwingConstants.CENTER);
titleLbl.setFont(new Font("Times New Roman", Font.PLAIN, 24));
titleLbl.setAlignmentX(JLabel.CENTER_ALIGNMENT);
App.java
import java.awt.*;
import java.awt.font.*;
import javax.swing.*;
import java.util.*;
public class App {
private void addComponentsToPane(Container pane) {
JLabel titleLbl = new JLabel("Help Page");
// add text attributes (i.e., underline, font family, font size, etc)
Font font = titleLbl.getFont();
Map<TextAttribute, Object> attributes = new HashMap<>(font.getAttributes());
attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
attributes.put(TextAttribute.FAMILY, "Times New Roman");
attributes.put(TextAttribute.SIZE, 24);
titleLbl.setFont(font.deriveFont(attributes));
titleLbl.setAlignmentX(JLabel.CENTER_ALIGNMENT);
JLabel infoLbl = new JLabel("This is the help page where the user can come for help");
infoLbl.setAlignmentX(JLabel.CENTER_ALIGNMENT);
infoLbl.setFont(new Font("Times New Roman", Font.PLAIN, 16));
Box box = new Box(BoxLayout.Y_AXIS);
box.add(titleLbl);
box.add(Box.createRigidArea(new Dimension(0, 5)));// creates space between the JLabels
box.add(infoLbl);
pane.add(box, BorderLayout.NORTH);
}
private void createAndShowGUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.setSize(640, 480);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new App().createAndShowGUI();
}
});
}
}

JLabel tooltip is not visible on top of BrowserView

I have this problem: tooltip of the label (JLabel) is hidden by a BrowserView.
The tooltip properly shown top of any other java component, but gets hidden by BrowserView. What I want is to make the tooltip visible on top of BrowserView. Anyone knows the reason for it and a way to get the tooltip visible.
Resulted UI and how tooltip is hidden is attached here.
Code sample:
public class TestFrame
{
public static void main (String args[])
{
JSplitPane splitPane = new JSplitPane();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel topPanel = new JPanel();
topPanel.setBorder(new LineBorder(Color.black));
topPanel.setSize(75, 75);
JLabel label = new JLabel("TestLabel");
label.setToolTipText("Test Tooltip 1");
topPanel.add(label);
splitPane.setLeftComponent(topPanel);
Browser browser = new Browser();
BrowserView browserView = new BrowserView(browser);
splitPane.setRightComponent(browserView);
browser.loadURL("http://www.google.com");
frame.add(splitPane);
frame.setSize(700, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Result UI: Right panel is a BrowserView which hides the Tooltip of left panel label
To let all know, the reason for this is JXBrowser is of 1.6.17 and it comes as Heavyweight by default.
And the Jtooltip is LightWeight. because of that Jtooltip is does not appear on top of the Heavyweight browser.
Few solutions.
1. Make the Browser lightweight (com.teamdev.jxbrowser.chromium.BrowserType#LIGHTWEIGHT)
2. Make the Tooltip HeavyWeight
You can override getToolTipLocation() and set tooltip text at a particular location (define x and y coordinates):
class BrowserLabel extends JLabel {
public BrowserLabel(String text) {
setText(text);
}
#Override
public Point getToolTipLocation(MouseEvent e) {
return new Point(x, y);
}
}

How to construct a JTextfield, and how to use the method selectAll()

I want to construct a Swing component JTextField, here is my Code
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JTextFieldGui{
JTextField textField;
JLabel labelInput;
JLabel labelOutput;
public static void main(String[] args) {
JTextFieldGui gui = new JTextFieldGui();
gui.go();
}
public void go(){
JFrame frame = new JFrame();
JPanel panelInput = new JPanel();
JPanel panelOutput = new JPanel();
labelInput = new JLabel("Your first name: ");
labelOutput = new JLabel("Enter your name, and you will see it here.");
textField = new JTextField(20);
JButton enter = new JButton("Enter");
JButton selectAll = new JButton("Select all text");
frame.setSize(300,200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panelInput.setLayout(new BoxLayout(panelInput, BoxLayout.X_AXIS));
textField.addActionListener(new LabelActionListener());
enter.addActionListener(new LabelActionListener());
selectAll.addActionListener(new TextFieldActionlistener());
frame.getContentPane().add(BorderLayout.NORTH, panelInput);
panelInput.add(labelInput);
panelInput.add(textField);
panelInput.add(enter);
panelInput.add(selectAll);
frame.getContentPane().add(BorderLayout.CENTER, panelOutput);
panelOutput.add(labelOutput);
}
class LabelActionListener implements ActionListener{
public void actionPerformed(ActionEvent event){
labelOutput.setText(textField.getText());
}
}
class TextFieldActionlistener implements ActionListener{
public void actionPerformed(ActionEvent event){
textField.selectAll();
}
}
}
Question1: I define the width of the text field in 20 columns, but it always take up a row, like image:
Question2: how to use the selectAll() method, I use it in a listener of the button selectAll, but when I click the button, nothing happens, why
I define the width of the text field in 20 columns, but it always take up a row,
This is the rule of a BoxLayout. A component is resized to fill the space available. A JTextField doesn't have a maximum size so it grows. The buttons and label do have a maximum size so they don't grow.
Don't use a BoxLayout, just use a FlowLayout. It will automatically leave space between each component which is a better layout.
I use it in a listener of the button selectAll, but when I click the button, nothing happens, why
Focus is still on the button. The selected text only displays when the text field has focus.
So in he listener code you need to add:
textField.requestFocusInWindow();
The following code is old:
frame.getContentPane().add(BorderLayout.NORTH, panelInput);
you don't need to get the content pane. You can just add the component to the frame.
the constraint should be the second parameter
there are new constraints to make the names more meaningful
So the code should be:
frame.add(panelInput, BorderLayout.PAGE_START, panelInput);
See the section from the Swing tutorial on How to Use BorderLayout for more information.

Java Swing spacing between objects

I have a Java program where i would prefer if i could get a very specific layout.
This is what I get:
JLabel JToggleButon JLabel JToggleButon
This is what I want:
JLabel JToggleButon
JLabel JToggleButon
This is the code:
package Main;
import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
public class SystemWindow {
static JFrame window = new JFrame("System statistics");
static JToggleButton button = new JToggleButton("Push me");
static JLabel status = new JLabel("Status: ");
static JLabel status2 = new JLabel("Status: ");
static JToggleButton button2 = new JToggleButton("Push me");
static FlowLayout layout = new FlowLayout();
public static void openWindow(){
window.setLayout(new GridBagLayout());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.add(status);
window.add(button);
window.add(status2);
window.add(button2);
window.setSize(100, 100);
window.pack();
window.setSize(200,70);
window.setVisible(true);
while(true){
status.setText("Status: "+button.isSelected());
status2.setText("Status: "+button2.isSelected());
}
}
}
p.s: Wrote the code in eclipse.
you can use GridLayout where you can specify rows, columns and spacings
i.e. add a jpanel with gridlayout and add these elements inside this panel
new JPanel(new GridLayout(2,2,5,5));
first parameter is the rows, second the columns and the others are the horizontal and vertical spacing between controls
I guess this works
JPanel panel = new JPanel(new GridLayout(2,2,5,5));
window.add(panel);
panel.add(status);
panel.add(button);
panel.add(status2);
panel.add(button2);
The behaviour you are getting is the default behaviour for FlowLayout. Read more about it here. Read morea bout layouts here and choose what you prefer.
As you are using GridBayLayout you have to provide some position constraints when adding controls to container. Here you have complete guide. 3rd party layout manager as MigLayout should suit your needs too.
while(true){
status.setText("Status: "+button.isSelected());
status2.setText("Status: "+button2.isSelected());
}
your computer is going to explode thanks to that code :)

Positioning in java swing

I have some troubles with positioning my label/password field.
With this code they both get positioned in the center next to each other, while I actually want them in the middle of my panel on top of each other.
Does anyone know how I should do that?
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
public class Paneel_Pincode extends JPanel {
Paneel_Pincode() {
setLayout(new FlowLayout());
JPasswordField pincode = new JPasswordField(15);
pincode.setLocation(500, 500);
JLabel pinInvoer = new JLabel();
ImageIcon pin1 = new ImageIcon("images/voerPincodeIn.jpg");
pinInvoer.setIcon(pin1);
pinInvoer.setLocation(500,700);
add(pincode);
add(pinInvoer);
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(1000,1000);
f.setLocationRelativeTo(null);
f.add(new Paneel_Pincode());
f.setVisible(true);
}
}
To get the hang of layouts, I'd recommend reading my article on them (http://java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/). It's old, but the concepts and how FlowLayout works are detailed.
What do you mean by "on top of each other"?
If you mean like
Password
<field>
EDIT: I REMEMBERED AN EASIER WAY TO DO THIS (completely in the JDK/JRE)...
(This is similar to what I'm doing with the BoxBeans below, but you don't need the BoxBeans. I created BoxBeans to be able to use BoxLayout in a UI builder a long time ago...)
JLabel label = new JLabel("Password") {
#Override public Dimension getMaximumSize() {
return super.getPreferredSize();
}
};
JPasswordField field = new JPasswordField() {
#Override public Dimension getMaximumSize() {
return super.getPreferredSize();
}
};
field.setColumns(10);
Box verticalBox = Box.createVerticalBox();
verticalBox.add(Box.createVerticalGlue());
verticalBox.add(label);
verticalBox.add(field);
verticalBox.add(Box.createVerticalGlue());
//
Box horizontalBox = Box.createHorizontalBox();
horizontalBox.add(Box.createHorizontalGlue());
horizontalBox.add(verticalBox);
horizontalBox.add(Box.createHorizontalGlue());
add(horizontalBox);
Previous answer for reference...
I DO NOT RECOMMEND THE FOLLOWING BUT IT MAY HELP OTHER READERS WITH IDEAS
You can do something like
setLayout(FlowLayout());
JPanel group = new JPanel(new BorderLayout());
group.add(new JLabel("Password"), BorderLayout.NORTH);
group.add(passwordField, BorderLayout.SOUTH);
add(group);
This will create a little panel in the top-center of the overall UI that contains the Password and field.
Note that the nested BorderLayout ensures that the label and field each get their preferred size. You'll need to call setColumns on the field to the number of chars you'd like displayed.
If you want to center the label/field vertically as well, you could do the following
setLayout(new GridBagLayout());
//
add(new JLabel("Password"),
new GridBagConstraints(0,0,1,1,1,1,
GridBagConstraints.SOUTH,GridBagConstraints.NONE,
new Insets(3,3,3,3), 0,0));
field.setColumns(10);
add(field, new GridBagConstraints(0,1,1,1,1,1,
GridBagConstraints.NORTH,GridBagConstraints.NONE,
new Insets(3,3,3,3), 0,0));
I hate using GridBagLayout in general, so I'll add a version using BoxLayout (but it's a bit trickier due to the preferred size settings)
JFrame f = new JFrame();
f.setLayout(new BorderLayout());
//
JPanel stuffH = new JPanel();
f.add(stuffH, BorderLayout.CENTER);
stuffH.setLayout(new BoxLayout(stuffH, BoxLayout.X_AXIS));
//
JPanel stuffV = new JPanel();
stuffV.setLayout(new BoxLayout(stuffV, BoxLayout.Y_AXIS));
//
JLabel label = new JLabel("Password");
BoxAdapter labelAdapter = new BoxAdapter();
labelAdapter.add(label);
JPasswordField field = new JPasswordField();
field.setColumns(10);
BoxAdapter fieldAdapter = new BoxAdapter();
fieldAdapter.add(field);
//
stuffV.add(new VerticalGlue()); // for vertical spacing
stuffV.add(labelAdapter);
stuffV.add(fieldAdapter);
stuffV.add(new VerticalGlue()); // for vertical spacing
//
stuffH.add(new HorizontalGlue()); // for horizontal spacing
stuffH.add(stuffV);
stuffH.add(new HorizontalGlue()); // for horizontal spacing
//
f.setVisible(true);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
A few notes on this:
I'm using my BoxBeans helper classes - see http://javadude.com/tools/boxbeans. This page is based on VisualAge for Java, but the jar at the bottom of the page can be used outside VAJ. I just tried it in eclipse, for example.
AFAICS, you cannot set a jframe's layout directly to BoxLayout, so I added an extra panel in between. There's a check in BoxLayout that has trouble with the automatic indirection of the content pane.
I nested the BoxLayouts so there's horizontal centering (the stuffH panel) containing a vertical centering (the stuffV panel). They are centered by surrounding them with "Glue" components, which are simply components that allow themselves to expand.
I had to put the label and field in a BoxAdapter which limits their maximum size to their preferred size. If you don't want to use BoxAdapter, you can acheive the same effect by using the following for the field and label:
JLabel label = new JLabel("Password") {
#Override public Dimension getMaximumSize() {
return super.getPreferredSize();
}
};
JPasswordField field = new JPasswordField() {
#Override public Dimension getMaximumSize() {
return super.getPreferredSize();
}
};
Hope this proves helpful to you and anyone else!
-- Scott
I would recommend the JGoodies FormLayout. Once you learn it, it's quite powerful and easy to do by hand coding.

Categories