When I press the button in the program below I want the popup menu from the JComboBox to appear and remain up.
However it only does that when on the second push when the Combobox is already visible. Is there any way I can make it appear on the first push?
public class Problem extends JPanel
implements ActionListener{
private static String SEARCH = "start search";
private static String SELECTED = "Selected";
private JTextField field2;
private JTextField field1;
private JComboBox list = new JComboBox();
public Problem() {
super(new GridBagLayout());
//Construct the panel
field2=new JTextField("");
field1=new JTextField("7564");
JButton search=new JButton("Search");
search.addActionListener(this);
search.setActionCommand(SEARCH);
list.addActionListener(this);
list.setActionCommand(SELECTED);
//Add everything to this panel.
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 8;
c.gridheight = 3;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0;
c.anchor = GridBagConstraints.NORTH;
add(field2,c);
list.setVisible(false);
add(list,c);
c.gridx = 8;
c.gridy = 0;
c.gridwidth = 8;
c.gridheight = 3;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0;
c.anchor = GridBagConstraints.NORTH;
add(field1,c);
c.gridx = 16;
c.gridy = 0;
c.gridwidth = 4;
c.gridheight = 3;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0;
c.anchor = GridBagConstraints.NORTH;
add(search,c);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand()==SEARCH){
String f2=field2.getText();
String f1=field1.getText();
if(f2.equals("")){
f2=selection(f1);
f2=null;
}
if(f2!=null){
field2.setText(f2);
}
else{
System.out.println("setPopupVisible runs");
field2.setVisible(false);
list.setVisible(true);
field2.setText("");
list.setPopupVisible(true);
}
}
else if(ae.getActionCommand().equals(SELECTED)){
System.out.println("select");
String listtext=(String) list.getSelectedItem();
list.removeAllItems();
if(listtext==null||!listtext.contains(": ")){
return;
}
String f2=listtext.split(": ")[0];
list.setVisible(false);
field2.setVisible(true);
field2.setText(f2);
}
}
private String selection(String sn) {
System.out.println("selection runs");
String name="7";
list.removeActionListener(this);
list.removeAllItems();
list.addItem(name);
list.addActionListener(this);
return null;
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Stuff");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
Problem newContentPane = new Problem();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
frame.setSize(800, 600);
}
public static void main(String[] args) throws FileNotFoundException {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGUI();
}
});
}
}
When toggling visibility of components, make sure the component has completed its internal update before doing custom stuff, that is f.i. delay the call to showing a combo's popup relative to showing the combo itself:
list.setVisible(true);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
list.setPopupVisible(true);
}
});
Related
This question already has answers here:
How to Remove an icon on a JButton?
(3 answers)
Closed 5 years ago.
I am working on the Tic Tac application all things are set but the only problem is that I am using images on the Button and I want to make a restart button when this restart Button pressed I want that all the images on the Button should be removed. I have also tried to setIcon(null) but when the Restart button press image is removed and one another problem arises is that Button's get disabled and I am not able to play again.
Is there is any other methods available to remove the icon from the Button?
The OP issue is more than setIcon(null), so I am giving this reply,
Try below source code, but there is a good multi pane scenario for you:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
/*
* Used resources within this class or illustration done within is just for the
* study purpose.
*/
public class ImageExample extends JFrame {
private static final long serialVersionUID = 1L;
/*
* used fixed images resource, you can take all directory resources as per your
* need
*/
private String[] leopard = new String[] { "leopard0", "leopard1", "leopard2", "leopard3" };
private URL url;
private JLabel label1, label2, label3, label4;
private ArrayList<Integer> index = new ArrayList<Integer>();
public ImageExample() {
init();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ImageExample();
}
});
}
private void init() {
setTitle("Add And Remove Image");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridBagLayout());
setBounds(400, 150, 625, 400);
for (int i = 0; i < leopard.length; i++)
index.add(i);
GridBagConstraints c = new GridBagConstraints();
JPanel panel = new JPanel(); // 1
panel.add(label1 = new JLabel(getIndexIcon(index.get(0))));
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.gridheight = 1;
add(panel, c);
panel = new JPanel(); // 2
panel.add(label2 = new JLabel(getIndexIcon(index.get(1))));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.0;
c.gridx = 1;
c.gridy = 0;
c.gridheight = 1;
add(panel, c);
panel = new JPanel(); // 3
panel.add(label3 = new JLabel(getIndexIcon(index.get(2))));
c.fill = GridBagConstraints.HORIZONTAL;
c.weighty = 0.0;
c.gridx = 0;
c.gridy = 1;
c.gridheight = 1;
add(panel, c);
panel = new JPanel(); // 4
panel.add(label4 = new JLabel(getIndexIcon(index.get(3))));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.0;
c.weighty = 0.0;
c.gridx = 1;
c.gridy = 1;
c.gridheight = 1;
add(panel, c);
panel = new JPanel(); // contains JButtons
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.0;
c.gridwidth = 2;
c.gridx = 0;
c.gridy = 2;
add(panel, c);
final JButton prev = new JButton("Previous");
final JButton addImage = new JButton("Add Image");
final JButton removeImage = new JButton("Remove Image");
final JButton next = new JButton("Next");
addImage.setEnabled(false);
panel.add(prev);
panel.add(addImage);
panel.add(removeImage);
panel.add(next);
prev.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
revalidateLabel(label1, revalidateIndex(0, index.get(0) - 1));
revalidateLabel(label2, revalidateIndex(1, index.get(1) - 1));
revalidateLabel(label3, revalidateIndex(2, index.get(2) - 1));
revalidateLabel(label4, revalidateIndex(3, index.get(3) - 1));
}
});
addImage.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
revalidateLabel(label1, index.get(0));
revalidateLabel(label2, index.get(1));
revalidateLabel(label3, index.get(2));
revalidateLabel(label4, index.get(3));
addImage.setEnabled(false);
removeImage.setEnabled(true);
prev.setEnabled(true);
next.setEnabled(true);
removeImage.requestFocus();
}
});
removeImage.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
removeLabelImage(label1);
removeLabelImage(label2);
removeLabelImage(label3);
removeLabelImage(label4);
prev.setEnabled(false);
next.setEnabled(false);
removeImage.setEnabled(false);
addImage.setEnabled(true);
addImage.requestFocus();
}
});
next.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
revalidateLabel(label1, revalidateIndex(0, index.get(0) + 1));
revalidateLabel(label2, revalidateIndex(1, index.get(1) + 1));
revalidateLabel(label3, revalidateIndex(2, index.get(2) + 1));
revalidateLabel(label4, revalidateIndex(3, index.get(3) + 1));
}
});
setVisible(true);
}
private ImageIcon getIndexIcon(int indexAt) {
if (indexAt < 0)
indexAt = leopard.length - 1;
if (indexAt > (leopard.length - 1))
indexAt = 0;
url = ImageExample.class.getResource("/resources/leopard" + indexAt + ".jpg");
ImageIcon icon = new ImageIcon(url.getPath());
return icon;
}
private void revalidateLabel(JLabel label, int indexAt) {
label.setIcon(getIndexIcon(indexAt));
label.revalidate();
}
private void removeLabelImage(JLabel label) {
label.setIcon(null);
label.revalidate();
}
private int revalidateIndex(int indexAt, int indx) {
if (indx < 0)
indx = leopard.length - 1;
if (indx > (leopard.length - 1))
indx = 0;
index.set(indexAt, indx);
return indx;
}
}
Problem and Code Summary:This will make sense if you run the code below. I'm using a JScrollPane and its scroll bar is disappearing when it is placed in a JPanel with a BorderLayout (NORTH). When the JPanel has a BorderLayout (CENTER), it works fine (with the scroll bar showing up). If you try to remove many of the components with the remove buttons, it gets annoying because the JScrollPane (with all of its components) clumps up in the middle of the JPanel it's in, when you want it to stay at the top. I tried to prevent this by obviously changing the BorderLayout of the JPanel from CENTER TO NORTH, but that just takes out the scroll bar completely. In the following code, the Main.placement (line 28) (change it to CENTER/NORTH to mess with it) controls the type of layout being used and line 171 shows how it is implemented. I just want to align the JSCrollPane at the top with the scroll bar showing up when it is neccessary.
Code Summary:
package main;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
public class Main{
public static final String placement = BorderLayout.CENTER;
public static void main(String[] args){
new TimeChart(true);
}
}
#SuppressWarnings("serial")
class TimeChart extends JFrame {
private boolean admin;
private TimeTable timeTable;
private static final JLabel title;
private final JLabel volLabel;
static {
title = new JLabel("Time Chart ");
}
private class TimeTable extends JPanel {
private JScrollPane scrollPane;
private JPanel table;
public final ArrayList<SessionRow> sessionRows;
private final JButton addButton;
private final JButton applyButton;
private static final double weight_sessnum = 0.2;
private static final double weight_signinout = 1.0;
private static final double weight_time = 0.3;
private static final double weight_delete = 0.0;
public class SessionRow {
private final SessionRow thisSessionRow; // for inner classes
private JTextField sessionnum, signinField, signoutField, total;
private JButton removeButton;
private SessionRow(int y) {
thisSessionRow = this;
String signinstring = "*signin time*";
String signoutstring = "*signout time*";
sessionnum = new JTextField("Session " + y + ": ");
sessionnum.setEditable(false);
signinField = new JTextField(signinstring);
signinField.addFocusListener(new FocusListener() {
#Override
public void focusGained(FocusEvent e) {
signinField.selectAll();
}
#Override
public void focusLost(FocusEvent e) {
}
});
signinField.setEditable(admin);
signoutField = new JTextField(signoutstring);
signoutField.addFocusListener(new FocusListener() {
#Override
public void focusGained(FocusEvent e) {
signoutField.selectAll();
}
#Override
public void focusLost(FocusEvent e) {
}
});
signoutField.setEditable(admin);
String totalstring = null;
try {
totalstring = "*session total*";
} catch (Exception e) {
totalstring = " -- ";
}
total = new JTextField(totalstring);
total.setEditable(false);
removeButton = new JButton("Remove");
removeButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
thisSessionRow.remove();
sessionRows.remove(thisSessionRow);
revalidate();
repaint();
}
});
}
public void remove() {
table.remove(this.sessionnum);
table.remove(this.signinField);
table.remove(this.signoutField);
table.remove(this.total);
table.remove(this.removeButton);
}
}
public TimeTable() {
super();
table = new JPanel();
scrollPane = new JScrollPane(table);
applyButton = new JButton("<html><div style=\"text-align: center;\">Apply<br>Changes</html>");
applyButton.setMargin(new Insets(5, 5, 5, 5));
applyButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
applyAllChanges();
}
});
applyButton.setEnabled(false);
addButton = new JButton("Add");
addButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
sessionRows.add(new SessionRow(sessionRows.size() + 1));
setupAll();
revalidate();
repaint();
if (!applyButton.isEnabled())
applyButton.setEnabled(true);
}
});
sessionRows = new ArrayList<SessionRow>();
setupSessionRows();
setupAll();
this.setLayout(new BorderLayout());
this.add(scrollPane, Main.placement); //PAY ATTENTION HERE
JScrollBar bar = scrollPane.getVerticalScrollBar();
bar.setPreferredSize(new Dimension(30, 0));
bar.setUnitIncrement(10);
InputMap im = bar.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
im.put(KeyStroke.getKeyStroke("down"), "positiveUnitIncrement");
im.put(KeyStroke.getKeyStroke("up"), "negativeUnitIncrement");
scrollPane.setBorder(BorderFactory.createLineBorder(Color.red));
this.setBorder(BorderFactory.createLineBorder(Color.green));
}
public void setupSessionRows() {
sessionRows.clear();
for (int i = 0; i < 30; i++) {
sessionRows.add(new SessionRow(i + 1));
}
System.gc();
}
public void setupAll() {
table.removeAll();
table.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(0, 0, 5, 2);
c.gridx = 0;
c.gridy = 0;
initColumnHeader(c);
for (int y = 0; y < sessionRows.size(); y++) {
c.gridy++;
initSessRow(sessionRows.get(y), c);
}
c.gridy++;
if (admin) {
initNewButtonRow(c);
c.gridy++;
}
initTotalHeader(c);
}
public void initSessRow(SessionRow row, GridBagConstraints c) {
c.weightx = weight_sessnum;
c.gridx = 0;
table.add(row.sessionnum, c);
c.weightx = weight_signinout;
c.gridx = 1;
table.add(row.signinField, c);
c.weightx = weight_signinout;
c.gridx = 2;
table.add(row.signoutField, c);
c.weightx = weight_time;
c.gridx = 3;
table.add(row.total, c);
if (admin) {
c.weightx = weight_delete;
c.gridx = 4;
table.add(row.removeButton, c);
}
}
public void initTotalHeader(GridBagConstraints c) {
c.weightx = weight_sessnum;
c.gridx = 0;
JTextField totalLabel = new JTextField("Total: ");
totalLabel.setEditable(false);
table.add(totalLabel, c);
c.weightx = weight_signinout;
c.gridx = 1;
JTextField blank1 = new JTextField();
blank1.setEditable(false);
table.add(blank1, c);
c.weightx = weight_signinout;
c.gridx = 2;
JTextField blank2 = new JTextField();
blank2.setEditable(false);
table.add(blank2, c);
c.weightx = weight_time;
c.gridx = 3;
JTextField totaltime = new JTextField("*TOTAL*");
totaltime.setEditable(false);
table.add(totaltime, c);
if (admin) {
c.weightx = weight_delete;
c.gridx = 4;
table.add(applyButton, c);
}
}
public void initNewButtonRow(GridBagConstraints c) {
c.weightx = weight_sessnum;
c.gridx = 0;
table.add(addButton, c);
c.weightx = weight_signinout;
c.gridx = 1;
JTextField blank1 = new JTextField();
blank1.setEditable(false);
table.add(blank1, c);
c.weightx = weight_signinout;
c.gridx = 2;
JTextField blank2 = new JTextField();
blank2.setEditable(false);
table.add(blank2, c);
c.weightx = weight_time;
c.gridx = 3;
JTextField blank3 = new JTextField();
blank3.setEditable(false);
table.add(blank3, c);
if (admin) {
c.weightx = weight_delete;
c.gridx = 4;
JTextField blank4 = new JTextField();
blank4.setEditable(false);
table.add(blank4, c);
}
}
public void initColumnHeader(GridBagConstraints c) {
c.weightx = weight_sessnum;
c.gridx = 0;
JTextField sessnum = new JTextField("Session #");
sessnum.setEditable(false);
table.add(sessnum, c);
c.weightx = weight_signinout;
c.gridx = 1;
JTextField signintime = new JTextField("Sign in Time");
signintime.setEditable(false);
table.add(signintime, c);
c.weightx = weight_signinout;
c.gridx = 2;
JTextField signouttime = new JTextField("Sign out Time");
signouttime.setEditable(false);
table.add(signouttime, c);
c.weightx = weight_time;
c.gridx = 3;
JTextField time = new JTextField("Time");
time.setEditable(false);
table.add(time, c);
if (admin) {
c.weightx = weight_delete;
c.gridx = 4;
JTextField delete = new JTextField("Delete");
delete.setEditable(false);
table.add(delete, c);
}
}
#Override
public void revalidate() {
if (sessionRows != null) {
int i = 1;
for (SessionRow sr : sessionRows) {
sr.sessionnum.setText("Session " + i);
i++;
}
}
super.revalidate();
}
}
public TimeChart(boolean admin) {
super();
this.admin = admin;
volLabel = new JLabel("Billy");
timeTable = new TimeTable();
this.setSize(900, 900);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initFrame();
this.setVisible(true);
scrollDown();
}
public void initFrame() {
this.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 0.0;
c.gridx = 0;
c.gridy = 0;
this.add(title, c);
c.weightx = 1.0;
c.weighty = 0.0;
c.gridx = 0;
c.gridy = 1;
this.add(volLabel, c);
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 2;
this.add(timeTable, c);
}
public void scrollDown() {
JScrollBar bar = timeTable.scrollPane.getVerticalScrollBar();
bar.setValue(bar.getMaximum());
}
public void applyAllChanges() {
}
}
Thanks in advance, I really appreciate it.
Why are my constraints for my buttons not working? I looked at the Java Docs and am doing the same thing the tutorials are doing, but for me the buttons stay the same regardless of what gridx, y, width, or fill I use. Any ideas? Here's my code:
class MyWindow
{
public static void main(String [] arg)
{
MyJFrame f = new MyJFrame("My GUI 2015");
f.setVisible(true);
f.setSize(10, 20);
f.add(f.p);
}
}
and
public class MyJFrame extends JFrame {
public JPanel p;
JButton close = new JButton("close");
JButton drawing = new JButton("drawing");
JButton image = new JButton("image");
JButton browser = new JButton("browser");
public MyJFrame(String title) {
super(title);
p = new JPanel();
buildButtons();
}
void buildButtons() {
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(0,40,0,150);
c.gridx = 0;
c.gridy = 0;
p.add(drawing, c);
c.gridx = 2;
c.gridy = 0;
p.add(close, c);
c.insets = new Insets(50,225,50,150);
c.gridx = 0;
c.gridy = 1;
p.add(image, c);
c.insets = new Insets(0,125,0,125);
c.gridx = 0;
c.gridy = 100;
c.gridwidth = 3;
c.fill = GridBagConstraints.HORIZONTAL;
p.add(browser, c);
}
}
The LayoutManager for the container is not specified in your current code (the default for a JPanel is FlowLayout). If you wish to use a GridBagLayout on the container, you must explicitly specify the LayoutManager:
p = new JPanel(new GridBagLayout());
//or
p.setLayout(new GridBagLayout());
So, I am having some really weird stuff going on here.
So, my entire class is this:
public class Test extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new Test();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public static void addComponentsToPane(Container pane) {
JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
button = new JButton("Button 1");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Button 2");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Button 3");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Long-Named Button 4");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 50;
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
pane.add(button, c);
button = new JButton("5");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 20;
c.weighty = 1.0;
c.anchor = GridBagConstraints.PAGE_END;
c.insets = new Insets(10,0,0,0);
c.gridx = 0;
c.gridwidth = 3;
c.gridy = 3;
pane.add(button, c);
}
private JFrame createAndShowGUI(JFrame frame) {
frame = new JFrame("GridBagLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
return frame;
}
JFrame frame = null;
public Test() {
createAndShowGUI(frame);
addComponentsToPane(frame.getContentPane());
}
}
So anyway, I am going to focus on this bit:
JFrame frame = null;
public Test() {
createAndShowGUI(frame);
addComponentsToPane(frame.getContentPane());
}
Which, produces this result (which works perfectly fine).
However, when I use this code:
JFrame frame = null;
public Test() {
frame = createAndShowGUI(frame);
addComponentsToPane(frame.getContentPane());
}
Which results in buttons 4 and 5 somehow duplicating themselves with a set size at the top of the screen. Also, buttons 4 and 5 appear to have different sizes.
Like this:
(And when I make the window smaller)
What is causing this? All I am doing is setting a variable from null or a JFrame, which should do nothing.
The first method throws a NullPointerException at the addComponentsToPane method because the frame attribute is never initialized and the second method adds the buttons twice to the frame. I have rewritten the code below and it works fine. You should also have a look at variable shadowing , because your frame attribute gets shadowed by the frame local variable in the addComponentsToPane method whick is rarely a good idea.
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new Test();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public static void addComponentsToPane(Container pane) {
JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
button = new JButton("Button 1");
if (true) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Button 2");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Button 3");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Long-Named Button 4");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 50;
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
pane.add(button, c);
button = new JButton("5");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 20;
c.weighty = 1.0;
c.anchor = GridBagConstraints.PAGE_END;
c.insets = new Insets(10, 0, 0, 0);
c.gridx = 0;
c.gridwidth = 3;
c.gridy = 3;
pane.add(button, c);
}
private void createAndShowGUI() {
frame = new JFrame("GridBagLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
JFrame frame = null;
public Test() {
createAndShowGUI();
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
I am having troubles getting the layout right.
I have a Class which extends JFrame and has a JPanel which has a the GridBagLayout as layout manager. I want 4 buttons which should be layout in this way
Somehow the vertical filling does not work.
I tried various ways but can't figure out how to make this work.
Sorry if this is a noob question, but tried it for more than an hour without any change :/
public class ButtonPanel extends JFrame {
private static final long serialVersionUID = 1L;
public ButtonPanel() {
this.setSize(800, 600);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
frame();
}
JButton objectsBtn = new JButton("Objekte"); //TODO Strings einfügen
JButton tenantBtn = new JButton("Mieter"); //TODO Strings einfügen
JButton ownerBtn = new JButton("Eigentümer"); //TODO Strings einfügen
JButton optionsBtn = new JButton("Einstellungen"); //TODO Strings einfügen
JPanel panel = new JPanel();
public void frame(){
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(5, 5, 5, 5);
c.fill = GridBagConstraints.BOTH;
c.gridwidth = GridBagConstraints.RELATIVE;
c.gridheight = GridBagConstraints.RELATIVE;
//Elemente
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 0;
panel.add(objectsBtn, c);
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 1;
c.gridy = 0;
panel.add(optionsBtn, c);
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 1;
panel.add(ownerBtn, c);
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 1;
c.gridy = 1;
panel.add(tenantBtn, c);
this.add(panel);
objectsBtn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
new Object();
}
});
ownerBtn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//TODO Eigentümer erstellen
}
});
}
}
greets
THE-E
EDIT: Found the bug
I used Seaglass as Look and Feel in the main-method
//Set Theme
try {
UIManager.setLookAndFeel("com.seaglasslookandfeel.SeaGlassLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
It works fine with the default Look and Feel Theme.
I have added the answer in the first post. It was a bug caused by the LookAndFeel skin. :/
greets
THE-E