I cannot find out why this will not show the contents:
public class Bans implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
draw();
}
public static JFrame frame;
public static void draw() {
frame = new JFrame("Ban History");
frame.setPreferredSize(new Dimension(575,250));
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JLabel label = new JLabel("Ban History");
label.setFont(Main.header);
label.setHorizontalAlignment(SwingConstants.CENTER);
JPanel container = new JPanel();
JPanel border = new JPanel();
JScrollPane scrPane = new JScrollPane(border);
border.add(container);
container.setLayout(new GridLayout(0,1));
scrPane.setBorder(null);
for(Ban ban : Main.banlist) {
System.out.println(ban.id);
JPanel whitespace = new JPanel();
JPanel panel = new JPanel();
panel.setBackground(Color.getHSBColor(176F, 25.46F, 65.12F));
panel.setPreferredSize(new Dimension(510,0));
panel.setLayout(new GridLayout(0,2));
JLabel banDate = new JLabel();
banDate.setText("(#" + ban.id + ") Ban Date: " + ban.banDate.toString() + " ");
banDate.setFont(Main.body);
panel.add(banDate);
JLabel banName = new JLabel();
banName.setText("Banned By: " + ban.bannedByName);
banName.setFont(Main.body);
panel.add(banName);
JLabel banReason = new JLabel();
banReason.setText("Reason: " + ban.banReason);
banReason.setFont(Main.body);
panel.add(banReason);
JLabel banTime = new JLabel();
banTime.setText("Ban Time: " + ban.banTime);
banTime.setFont(Main.body);
banTime.setHorizontalAlignment(SwingConstants.RIGHT);
panel.add(banTime);
container.add(whitespace);
whitespace.add(panel);
}
frame.add(label,BorderLayout.PAGE_START);
frame.add(scrPane, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
It was working earlier, however now none of the contents of the ScrollPane show. I'm fairly new with Swing, however I would think that this should work. Yes, there are contents in the table. When ran it does print out 5 id numbers, which correspond to the correct numbers on the MySQL server, so that should be working fine.
The content is being added, but isn't being displayed because of how you have designed your layout, as the panels representing your bans have no height:
panel.setPreferredSize(new Dimension(510,0));
By following #FredK's suggestions in the comments, you can maintain the effect of your whitespace panel by applying a vertical gap between the rows in your layout. This is provided by another constructor available within GridLayout: GridLayout(int rows, int cols, int hgap, int vgap)
There are some demos available here: How to Use GridLayout
By mocking up some bans, I got the following with your code:
Note: By applying the suggestions (with a vgap of 5) the output was the same as the above
Side note: If you find something was working earlier it tends to be from a change you've made. If you use IntelliJ as your IDE there is a helpful feature for this called a Local History. By right clicking on the class you can see line-by-line comparisons of changes you made to the class with time stamps, similar to source control. You can read more here: Using Local History
I'm not saying you have to change your IDE, but as a beginner if you are doing a lot of experimentation you may find some benefit in it
Related
Given my requirements:
Single vertical column of JPanels.
Set the vertical location* of the JPanel without using the properties of a sibling.
Component position and size are fixed when the frame is resized.
Keep other layout aspects automatic (such as preferred size calculation), as much as possible
(*) Location: I mean location as in Component.setLocation(x, y).
is there a solution which is obvious, and if this is GridBagLayout, how to do this?
Details:
I want to put components vertically in a column container (like a vertical Box) by specifying their vertical location only. What is the best way to do this without loosing the other benefits of a layout such as BoxLayout?
In a vertical Box, setting the vertical position of a component must be done using a filler, or by adjusting the size of the component just above, there is no such possibility like:
panel.setLocation(getLocation().x, y)
On the other hand using a no layout container puts on me the task manage:
The initial size of the component
The container resizing events.
Here the solution of null layout is recommended, here this is a custom one, and here this is GridBagLaout. Also MIGLayout appears to be universal one (but I'd prefer no adding another library to my project).
I have written the following program for someone who was also looking for the same requirements.
Note: Make sure to add the first element to 0 position because there will be no more components and no position will be available other than 0, 2nd to 0 or 1, 3rd to 0 or 1 or 2 and so on
public class VerticalList extends JFrame {
JPanel pnl = null;
TextField tf = new TextField(10);
Box center = Box.createVerticalBox();
JScrollPane jsp = new JScrollPane(center);
JPanel ctrl = new JPanel(new FlowLayout());
JButton send = new JButton("Send");
public VerticalList() {
jsp.setAutoscrolls(true);
ctrl.add(send);
ctrl.add(new JLabel("Position:"));
ctrl.add(tf);
Container cnt = getContentPane();
cnt.add(jsp, BorderLayout.CENTER);
cnt.add(ctrl, BorderLayout.SOUTH);
send.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
pnl = new JPanel(new FlowLayout());
pnl.setBorder(BorderFactory.createLineBorder(Color.red));
pnl.add(new JLabel("Added to Position: "+tf.getText()));
pnl.setMaximumSize(new Dimension(Integer.MAX_VALUE, (int)pnl.getPreferredSize().getHeight()));
try{
int index = Integer.parseInt(tf.getText());
center.add(pnl, index);
}catch(Exception ex){
JOptionPane.showMessageDialog(null, "Please Provide a Valid position", "Position Error", JOptionPane.ERROR_MESSAGE);
}
validate();
}
});
setPreferredSize(new Dimension(300, 300));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
pack();
}
public static void main(String[] args) {
new VerticalList();
}
}
I'm using JGoodies Forms 1.8.0
I've been having a problem when a single oversized label causes the entire layout to no longer fit in a window. I would like that text to be visually trimmed, so it's obvious for the user that the text doesn't fit, but the rest of the GUI should still keep fitting.
I prepared a simplified example that exhibits the same behaviour. Here everything works fine because the window is large enough:
And here the same window, but resized:
Notice that the rightmost columns are no longer visible.
The desired effect is as following:
if the text fits, it should be displayed in its entirety
if the text doesn't fit, then the end of it should be cut off
text should be left-aligned
all buttons should be visible, all the time
button 100 should be in the very corner of the window
Here is the code for the screenshots:
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import java.awt.*;
import javax.swing.*;
public class Test extends JFrame {
Test() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(900, 600);
JPanel left = new JPanel();
left.setBackground(Color.BLUE);
JPanel right = new JPanel();
JLabel fox = new JLabel("The quick brown fox jumps over the lazy dog.");
fox.setFont(new Font(null, 0, 50));
JPanel rightBottom = new JPanel();
rightBottom.setLayout(new GridLayout(10, 10));
for (int i = 1; i <= 100; i++) {
rightBottom.add(new JButton("butt" + i));
}
CellConstraints cc = new CellConstraints();
this.setLayout(new FormLayout("100dlu,p:g", "f:p:g"));
this.add(left, cc.xy(1, 1));
this.add(right, cc.xy(2, 1));
right.setLayout(new FormLayout("f:p:g", "p,5dlu,f:p:g"));
right.add(fox, cc.xy(1, 1));
right.add(rightBottom, cc.xy(1, 3));
}
public static void main(String[] args) {
new Test().setVisible(true);
}
}
The specification of preferred size makes the layout to display buttons at their preferred size. As a result some of the buttons do not fit. Try specifying a different constraint. For example, use a constant size with a combination of grow and fill. For example try these:
this.setLayout(new FormLayout("100dlu,1dlu:g", "f:1dlu:g"));
right.setLayout(new FormLayout("f:1dlu:g", "p,5dlu,f:1dlu:g"));
At certain sizes button titles will not fit though.
Here is a result:
BorderLayout ... didn't help
Works fine for me:
import java.awt.*;
import javax.swing.*;
public class Test8 extends JFrame {
Test8() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(900, 600);
JPanel left = new JPanel();
left.setBackground(Color.BLUE);
JPanel right = new JPanel(new BorderLayout());
JLabel fox = new JLabel("The quick brown fox jumps over the lazy dog.");
fox.setFont(new Font(null, 0, 50));
JPanel rightBottom = new JPanel();
rightBottom.setLayout(new GridLayout(10, 10));
for (int i = 1; i <= 100; i++) {
rightBottom.add(new JButton("butt" + i));
}
right.add(fox, BorderLayout.NORTH);
right.add(rightBottom, BorderLayout.CENTER);
add(right);
}
public static void main(String[] args) {
new Test8().setVisible(true);
}
}
If you don't like the dots showing in the label, then try a non-editable text field.
When something doesn't work, then post the code you tried. A verbal explanation doesn't help.
I've bee teaching myself java and following along with the problems in the book. I'm trying to make a display for my calculator. In the example(I did not attach this) the buttons were a smaller size than what mine are and I can't figure out how to reformat them. I tried using the dimension class but it had no affect. Also, I can't get my text at the top of the calculator to align left.
Here is my code:
public class Calculator extends JFrame {
public Calculator() {
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(300, 300);
setLayout(new BorderLayout());
JPanel numberPanel = new JPanel();
add(numberPanel, BorderLayout.CENTER);
numberPanel.setLayout(new GridLayout(4, 3, 3, 3));
for(int i = 1; i < 10; i++) {
JButton button = new JButton(String.valueOf(i));
numberPanel.add(button);
}
JButton zero = new JButton("" + 0);
JButton dot = new JButton(".");
JButton clear = new JButton("C");
numberPanel.add(zero);
numberPanel.add(dot);
numberPanel.add(clear);
JPanel keyPanel = new JPanel();
add(keyPanel, BorderLayout.EAST);
keyPanel.setLayout(new GridLayout(4, 1, 3, 3));
JButton plus = new JButton("+");
JButton minus = new JButton("-");
JButton times = new JButton("*");
JButton divide = new JButton("/");
keyPanel.add(plus);
keyPanel.add(minus);
keyPanel.add(times);
keyPanel.add(divide);
JPanel equalsPanel = new JPanel();
add(equalsPanel, BorderLayout.SOUTH);
equalsPanel.setLayout(new GridLayout(1, 1));
JButton equals = new JButton("=");
equalsPanel.add(equals);
JPanel textPanel = new JPanel();
add(textPanel, BorderLayout.NORTH);
JTextField inputBox = new JTextField("0.0");
inputBox.setHorizontalAlignment(JTextField.LEFT);
inputBox.setEditable(false);
Font font = new Font("MonoSpaced", Font.BOLD, 20);
inputBox.setFont(font);
textPanel.add(inputBox);
setVisible(true);
}
public static void main(String[] args) {
new Calculator();
}
}
Imports were left off for brevity
GridLayout will laugh at you when you try and set a dimension. It does respect preferred sizes. You should select a layout manager that will respect preferred sizes. Or you can simply pack() (after you add all your components) your frame instead of setSize() and all the components preferred sizes will kick in. (Disclaimer - because of GridLayout though, if you try and resize the frame after that, you components will resize again)
See more at How to use Layout Managers. For a quick view of which layout managers respect preferred sizes and which ones don't, have a look at this post.
A common approach is to nest panels with different layout managers also, as seen here
UPDATE
As mentioned preciously, you should just call pack on the frame instead of set size. With your current code, this would cause the frame to be very small because of the preferred sizes of the components. If you want the buttons to have a bigger preferred size, you can set the font to a bigger font and/or use button.setMargins(new Insets(w,x,y,x)); to make the margins bigger. But it is preferred to pack the frame.
I would recommend using the Window Builder add-on if you’re using Eclipse. This tool will help you with many aspects of Swing. Learn by doing.
WindowBuilder Dowload Link
I need to do an assignment and create a calculator. It's a beginner Java course, so keep in mind that I'm no expert. It shouldn't look spectacular, so the easiest way to achieve the below would be great.
The inner workings of it is fine, but drawing it has been a real headache.
We've only gotten exposure to flowlayout so far...and in this instance it's not what I want at all. Let me start of by telling you what layout I'm looking for:
At the top a heading spreading across the calculator, with perhaps a
background fill.
Then below that, 2 buttons next to each other.
Below that, two labels next to each other.
Then two text field next to each other.
Below that, two labels next to each other.
Then two text field next to each other.
I tried drawing it here, but it doesn't format correctly. If I can put it in HTML it would basically be a simple table, with 6 rows and 2 columns. But the top row must span across both columns.
Flowlayout just put everything next to each other from left to right.
After that I tried using GridLayout, but the top heading was the problem here, as it didn't span across both columns.
Here is my code so far:
public class TripCalculator extends JFrame implements ActionListener {
public static final int WIDTH = 400;
public static final int HEIGHT = 300;
public static final int NUMBER_OF_CHAR = 4;
public JTextField stopTime, distance, tripTime, speed;
public TripCalculator() {
setSize(WIDTH, HEIGHT);
WindowDestroyer listener = new WindowDestroyer();
addWindowListener(listener);
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
JLabel heading = new JLabel("HEADING");
contentPane.add(heading);
contentPane.setLayout(new FlowLayout());
JButton addStop = new JButton("BUTTON1");
addStop.addActionListener(this);
JButton addLeg = new JButton("BUTTON2");
addLeg.addActionListener(this);
contentPane.add(addStop);
contentPane.add(addLeg);
JLabel subHead1 = new JLabel("LABEL1");
contentPane.add(subHead1);
JLabel subHead2 = new JLabel("LABEL2");
contentPane.add(subHead2);
stopTime = new JTextField(NUMBER_OF_CHAR);
contentPane.add(stopTime);
distance = new JTextField(NUMBER_OF_CHAR);
contentPane.add(distance);
JLabel subHead3 = new JLabel("LABEL3");
contentPane.add(subHead3);
JLabel subHead4 = new JLabel("LABEL4");
contentPane.add(subHead4);
tripTime = new JTextField(NUMBER_OF_CHAR);
contentPane.add(tripTime);
speed = new JTextField(NUMBER_OF_CHAR);
contentPane.add(speed);
}
}
I would greatly appreciate if anyone can show me in the right direction.
Flowlayout or Gridlayout by themselves won't help you. Either you can use Gridbaglayout, or a combination of layouts such as FlowLaout+Gridlayout.
If you are drawing a calculator, I am assuming you are drawing something like this :
Where you have title information at the top, a calculator keypad in the middle, and some other buttons at the bottom :
This could be achieved with a vertical box layout, with flowlayouts at the top and bottom, and in the middle a grid layout with all the number keys.
But... without you showing a diagram of what you want its very difficult to say.
Here's an example of using multiple Layout Managers as you can see you can use more than one, but you should use more than one JPanel to achieve what you want.
Also a recommendation is: Don't extend from JFrame, instead create a JFrame object as I did in this example and here's why you shouldn't do that.
import java.awt.*;
import javax.swing.*;
public class LayoutManagersExample {
public static void main(String args[]) {
new LayoutManagersExample();
}
public LayoutManagersExample() {
JFrame frame = new JFrame("Layout Managers Example");
JPanel topPane = new JPanel();
JPanel midPane = new JPanel();
JPanel panesHolder = new JPanel();
JLabel label = new JLabel("Top label");
JTextField field = new JTextField();
field.setColumns(5);
topPane.setLayout(new FlowLayout());
midPane.setLayout(new GridLayout(3, 2));
topPane.add(label);
topPane.add(field);
midPane.add(new JButton("Button 1"));
midPane.add(new JButton("Button 2"));
midPane.add(new JButton("Hello I'm a button"));
midPane.add(new JButton("HEY! Click me :)"));
midPane.add(new JButton("I love you"));
midPane.add(new JButton("This is another button"));
panesHolder.setLayout(new BoxLayout(panesHolder, BoxLayout.Y_AXIS));
panesHolder.add(topPane);
panesHolder.add(midPane);
frame.add(panesHolder);
frame.setSize(400, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
}
}
And this is how it looks like:
I have used the createVerticalGlue() successfully on a number of times. However in the following screen building code it is not working. I am still wondering why.
The situation is that I have two JPanels (leftPanel, rightPanel) within another Panel (centralPanel). Each of these two panels will host two JComponents using a BoxLayout Y-Axis. I want to distribute the remaining free space before, between and after the two JComponents.
I know that I can use empty borders and rigid areas to solve the problem but it happens that I am a bit hard headed!
My appologies for the long question.
public class MemberGUI extends JFrame {
JPanel contPane = (JPanel) this.getContentPane();
JPanel centralPanel = new JPanel();
JPanel leftPanel = new JPanel();
JPanel rightPanel = new JPanel();
JPanel lowerPanel = new JPanel();
JLabel title = new JLabel("Add/Delete Member");
JLabel nameLbl = new JLabel("Member's name");
JLabel idLbl = new JLabel ("Member ID");
JTextField nameFld = new JTextField(10);
JTextField idFld = new JTextField(10);
public void buildMe(){
//LayoutManagers
contPane.add(title,BorderLayout.PAGE_START);
contPane.add(centralPanel,BorderLayout.CENTER);
contPane.add(lowerPanel,BorderLayout.PAGE_END);
centralPanel.add(leftPanel);
centralPanel.add(rightPanel);
leftPanel.setLayout(new BoxLayout(leftPanel,BoxLayout.Y_AXIS));
rightPanel.setLayout(new BoxLayout(rightPanel,BoxLayout.Y_AXIS));
//leftPanel.add(Box.createRigidArea(new Dimension(0,50)));
leftPanel.add(Box.createVerticalGlue());
leftPanel.add(idLbl);
leftPanel.add(Box.createVerticalGlue());
leftPanel.add(nameLbl);
leftPanel.add(Box.createVerticalGlue());
//rightPanel.add(Box.createRigidArea(new Dimension(0,50)));
rightPanel.add(Box.createVerticalGlue());
rightPanel.add(idFld);
rightPanel.add(Box.createVerticalGlue());
rightPanel.add(nameFld);
rightPanel.add(Box.createVerticalGlue());
// JFrame Settings
this.setSize(500,400);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
}
afaics, centralPanel has default layoutManager, which is FlowLayout. This always sizes its children to their prefSize, so the glue never comes into play.
Nesting containers comes at a price (which nesting wizards like #Andrew happily pay :-) Also, it's not entirely trivial to get right (f.i. replace the upper textField with a comboBox to see the mis-alignment). In the longer run, there's hardly a route around learning to master a more powerful manager, like f.i. MigLayout (my current personal favorite)