How to add buttons and JPanels to a ZOrderComponent? - java

I'm trying to create a JFrame with a background image, a JLabel over the background image centered and toward the bottom, with two buttons on the right and left that say "Stay" and "Leave". This is already created. The issue arises with the order of each of the items. I cannot get the JLabel with text and buttons over the background image, with both of them showing. Here is my code; any advice would be appreciated. Thank you in advance.
public class SceneOne {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
JFrame SceneOne = new JFrame();
ImageIcon image = new ImageIcon(
"C:/Users/alan/Downloads/scary_forest_2.jpg");
JLabel imageLabel = new JLabel("", image, JLabel.CENTER);
JPanel panel = new JPanel(new BorderLayout());
panel.add(imageLabel, BorderLayout.CENTER);
SceneOne.add(panel);
SceneOne.setResizable(true);
imageLabel.setVisible(true);
SceneOne.pack();
JButton Leave=new JButton("Leave");
JButton Stay= new JButton ("Stay");
JPanel Leave1= new JPanel(new FlowLayout());
JPanel Stay1=new JPanel(new FlowLayout());
FlowLayout two = new FlowLayout(FlowLayout.LEFT);
FlowLayout three = new FlowLayout(FlowLayout.RIGHT);
Leave1.setLayout(two);
Stay1.setLayout(three);
Stay1.add(Leave);
Leave1.add(Stay);
Leave1.setOpaque(false);
Stay1.setOpaque(false);
SceneOne.add(Leave1);
SceneOne.add(Stay1);
JLabel label1 = new JLabel("Test");
SceneOne.add(label1);
label1.setText("<html><font color='red'> It was approximately 11:30 pm. The night sky was black not a single star piercing through the darkness"
+ "except the thick and powerful moonlight."
+ "<br>"
+ "You are alone leaving a costume party at a friend's place."
+ "It was rather boring and you decided to leave early."
+ "A stutter is heard and your"
+ "<br>"
+ "car begins to shake"
+ "Your headlights and car lights crack. The engine is left dead silent."
+ "You are left in a total silence"
+ "and baked in merely the moonlight."
+ "<br>"
+ "There is a mere second of silence till a harsh chill ripes through the"
+ "car like a bullet through paper. You are left dumbfounded. What do you do?</font><html>");
label1.setHorizontalAlignment(JLabel.CENTER);
label1.setVerticalAlignment(JLabel.BOTTOM);
label1.setVisible(true);
label1.setOpaque(false);
SceneOne.setComponentZOrder(panel, 0);
SceneOne.setComponentZOrder(label1, 0);
// SceneOne.setComponentZOrder(Leave1,0);
// SceneOne.setComponentZOrder(Stay1,0);
SceneOne.setSize(400,320);
SceneOne.setTitle("The Car");
SceneOne.setVisible(true);
SceneOne.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SceneOne.setLocation(500, 300);
}
}

First of all follow Java naming conventions. Variable names should NOT start with an upper case character. I have never seen a tutorial, text book or forum example that uses variable names like you are using. Don't make up your own conventions!
The default layout manager for a JFrame is a BorderLayout. When you use the frame.add(...) method the component is added to the CENTER of the BorderLayout by default. Only on component can be added to the CENTER so only the last component is displayed.
If you want components to appear on top of the image, then you need to add the components to the image. The basic code would be:
JLabel label = new JLabel( new ImageIcon(...) );
frame.add(label);
label.setLayout(....);
label.add(leaveButton);
label.add(labelWithText);
label.add(stayButton);
I'll let you work out the exact layout that you want.

Related

Scrollable Pane does not show contents

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

java swing calculator layouts

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:

Is it possible to implement scrollbar in Jframe without Jpanel?

I've written a long code involving several frames using only JFrame, and now I need a scrollbar for one of the frames. It seems like the only way to use JScrollPane is with JPanel, but I dont want to rewrite everything, is there a way?
frames[2] = new JFrame("Title");
frames[2].setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);//EXIT_ON_CLOSE
frames[2].setContentPane(new JLabel(new ImageIcon("/Users/mac/Downloads/bg.000.jpg")));
frames[2].setLayout(new BorderLayout());
frames[2].setLayout(new FlowLayout());
frames[2].setResizable(true);
frames[2].setSize(750,433);
label[4] = new JLabel (" Search : ", JLabel.CENTER);
label[4].setFont(new Font("Serif", Font.ITALIC, 16));
label[4].setForeground(Color.BLACK);// font color
frames[2].add(label[4]);
TextField[2]= new JTextField ("Enter name", 15);
TextField[2].setFont(font);
frames[2].add(TextField[2]);
This is the frame (frame[2]) that I would like the scrollbar to be used in.

JButton Appears on One Computer, But Not Others (BorderLayout)

I am new to Swing. I am building a JFrame with a JScrollPane inside it using Eclipse IDE. Inside of the JScrollPane is a JPanel in Border Layout. I tried to add a JButton (called "submitAnswers") to the JFrame using the code below, but for some reason the button only appears at the end of the frame on my computer, but not on other computers (my friend tried it on his Mac and I tried it on a separate Windows OS like mine). Some proposed solutions that I have tried and from other sites that have not worked include:
Use the pack() method. Reason: since the preferred size of the JPanel is much longer in height than the JFrame (hence I employed a JScrollPane), packing the JFrame only causes the text to be not visible on the desktop.
Place button on content JPanel. Reason: I don't know. It just wouldn't appear on another desktop computer or my friend's mac computer.
Use BorderLayout.SOUTH instead of BorderLayout.PAGE_END. Reason: There was absolutely no change. The button would still be visible on my computer, but invisible on others.
Place button directly on JFrame. Reason: I don't know.
In addition, my JFrame is nested within a static method; hence, I've only included the relevant code for the specific method I'm having issues with.
Has anyone had this issue before? I would really appreciate your insight.
Code:
public static void createTestPage() {
JFrame testFrame = new JFrame("testing...1,2,3");
//Customizes icon to replace java icon
try {
testFrame.setIconImage(ImageIO.read(new File("src/icon.png")));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//Centers location of introFrame to center of desktop
Dimension screenDimensions = Toolkit.getDefaultToolkit().getScreenSize();
testFrame.setLocation(screenDimensions.width / 16,screenDimensions.height / 14);
//Size and display the introFrame.
Insets insets = testFrame.getInsets();
//Format size of screen itself
testFrame.setSize(1200 + insets.left + insets.right,
400 + insets.top + 250 + insets.bottom);
//Temporarily set screen so that it cannot be resized
testFrame.setResizable(false);
//Set background color of testFrame
testFrame.getContentPane().setBackground(new Color(75, 0, 130));
testFrame.setLayout(new BorderLayout());
//Set layout of testFrame
testFrame.setLayout(new BorderLayout(10, 1));
//Test content
JPanel testContentPanel = new JPanel();
testContentPanel.setBackground(new Color(75, 0, 130));
testContentPanel.setSize(new Dimension(900,2060));
testContentPanel.setPreferredSize(new Dimension(900, 2060));
//Test content pane layout
testContentPanel.setLayout(new BoxLayout(testContentPanel, BoxLayout.PAGE_AXIS));
//Create panel to hold instructions text
JPanel instructionsPanel = new JPanel();
instructionsPanel.setBackground(new Color(75, 0, 130));
instructionsPanel.setLayout(new BorderLayout(10,1));
//Create JPanel for submit answers button
JPanel submitAnswersPanel = new JPanel(new BorderLayout());
submitAnswersPanel.setBackground(new Color(75, 0, 130));
submitAnswersPanel.setVisible(true);
//Create button to submit personality test answers
JButton submitAnswers = new JButton("Submit Answers");
submitAnswers.setVisible(true);
submitAnswers.setBorder(new EmptyBorder(10, 400, 10, 400));
//Add submitAnswers button to panel
submitAnswersPanel.add(submitAnswers);
//Add submitAnswersPanel to test content panel
testContentPanel.add(submitAnswersPanel);
//Create scroll pane to allow for scrollable test (contents cannot fit one page)
JScrollPane testScrollPane = new JScrollPane();
testScrollPane.setViewportView(testContentPanel);
//Get rid of horizontal scroll bar and add vertical scrollbar
testScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
testScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//Speed up scrolling
testScrollPane.getVerticalScrollBar().setUnitIncrement(16);
testFrame.add(testScrollPane);
//Experiment to show button
testFrame.setVisible(true);
}
I've refactored your code a little to use method to create the individual components of the GUI. You can find the full code at this ideone link
What I saw when I first copied your code to my machine was that the only thing visible was the button. So I create all the components in their own methods and then added them to the frame and panels using the Border Layout. This then enabled me to put the instructions in the NORTH sections, the button in the SOUTH section and then the main bits would go in the CENTER section.
One thing to note about the sections: (From the documentation)
The components are laid out according to their preferred sizes and the constraints of the container's size. The NORTH and SOUTH components may be stretched horizontally; the EAST and WEST components may be stretched vertically; the CENTER component may stretch both horizontally and vertically to fill any space left over.
So you should add the component you want to scale in size to the CENTER section.
My main method now looks like this:
public static void main(final String[] args) {
final JButton submitAnswers = createSubmitAnswersButton();
final JPanel instructionsPanel = createInstructionsPanel();
final JPanel testContentPanel = createContentPanel();
testContentPanel.add(instructionsPanel, BorderLayout.NORTH);
testContentPanel.add(submitAnswers, BorderLayout.SOUTH);
final JScrollPane scrollingContentPane = createScrollPaneFor(testContentPanel);
final JFrame testFrame = createJFrame();
testFrame.add(scrollingContentPane, BorderLayout.CENTER);
testFrame.setVisible(true);
}

Java Swing panel layered on top with centered text

I'm making a simple Jeopardy-esque game:
using Java Swing. It's obviously a JFrame with a JPanel in it and buttons in rows.
Now what I need is to add a layered panel with a centered and wrapped text in it:
Which I can remove later. I already tried using JTextPane and JTextArea and JPanel, none of those want to even display. The best effect I have achieved with AWT Panel, it does display but I can't center or wrap text in it.
Here's some code for which I appologise, I would usually try to make it short and readable but since it's not working I don't know what to do with it to make ti look better:
JLabel questionLabel = new JLabel(questionList.get(randomNumber).getQuestion(), SwingConstants.CENTER);
Font font = new Font("Arial", Font.PLAIN, 20);
//------------------JTextPane--------------------
JTextPane questionPane = new JTextPane();
questionPane.setForeground(Color.WHITE);
questionPane.setSize(gameWidth, gameHeight);
questionPane.setText(questionList.get(randomNumber).getQuestion());
questionPane.setFont(font);
questionPane.setEditable(false);
//------------------AWT panel--------------------
Panel awtPanel = new Panel();
awtPanel.setBackground(Color.blue);
awtPanel.setSize(game.getWidth(),game.getHeight());
Label labelQuestion = new Label("<html>" + questionList.get(randomNumber).getQuestion() + "</html>", Label.CENTER);
labelQuestion.setFont(font);
awtPanel.setForeground(Color.white);
awtPanel.add(labelQuestion);
//------------------JPanel-----------------------
JPanel layeredPanel = new JPanel();
layeredPanel.setBackground(Color.blue);
layeredPanel.setSize(game.getWidth(),game.getHeight());
JLabel jLabelQuestion = new JLabel("<html>" + questionList.get(randomNumber).getQuestion() + "</html>", SwingConstants.CENTER);
jLabelQuestion.setFont(font);
layeredPanel.setForeground(Color.WHITE);
layeredPanel.add(jLabelQuestion, BorderLayout.CENTER);
game.getLayeredPane().add(layeredPanel, JLayeredPane.DEFAULT_LAYER);
try {
Thread.sleep(3000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
button.setEnabled(false);
font = new Font("Arial", Font.PLAIN, 16);
button.add(jLabelQuestion, BorderLayout.CENTER);
button.setDisabledIcon(new ImageIcon(source.getScaledInstance(gameWidth/4, gameHeight/5, java.awt.Image.SCALE_SMOOTH)));
questionList.remove(randomNumber);
logger.info(questionList.size());
game.getLayeredPane().remove(layeredPanel);
UPDATE: I chnaged to SWT rather than Swing, and I use the StackLayout with a few Composites in it, and just change between them as I see fit.
You can generally solve issues like this with a JLabel.
I would recommend encapsulating the above grid in the BorderLayout.CENTER of another pane, perhaps a new content pane. Then, add the caption to BorderLayout.NORTH.
As a more tangible example,
private void createContent() {
this.getContentPane().setLayout(new BorderLayout());
//establish the panel currently set as center, here labeled "everythingElse"
this.getContentPane().add(everythingElse, BorderLayout.CENTER);
//Create a JLabel with your caption
JLabel jlbl = new JLabel("Question");
//format that caption, most details being rather obvious, but most importantly:
jlbl.setHorizontalAlignment(SwingConstants.CENTER); //keeps text centered
this.getContentPane().add(jlbl, BorderLayout.NORTH); //add it to the top of the panel
//...other cleanup operations...
}
The issue with grid panes is that they have a limited tolerance for the number of components visible in them. If you overload one, it won't show. For BorderLayout panes, you can easily swap new items into and out of them.
For efficiency's sake, I might recommend compiling this JLabel as a final somewhere else in your code, and holding onto it for when you need it. This way, you will also dodge overhead from repeatedly creating the label object.
Lastly, avoid AWT whenever you can. It's been deprecated for an excess of ten years, and if you do use it you will run into numerous critical problems involving heavyweight and lightweight component incompatibilities. If you intend to use another windowing kit, consider implementing the new standard, JavaFX, with a JFXPane-- it's much more tolerant of HTML syntax, as well.

Categories

Resources