JRadioButtons not working - java

I'm having an issue where my JRadioButtons will not appear until moused over. I've looked up this issue before and it seems most people have solved it using layouts. However, I am not supposed to use them for my assignment, being completely new to graphics with Java I'm really struggling. Any ideas? Anything is appreciated.
import java.awt.GridLayout;
import javax.swing.*;
public class InsuarancePolicyApp {
public static void main(String[] args) {
//JFrame
JFrame f = new JFrame();
f.setVisible(true);
f.setSize(800, 600);
f.setLayout(null);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("Insurance Policy Application");
//JButtons
JButton addClient = new JButton("Add Client");
JButton openPolicy = new JButton("Open Policy");
f.getContentPane().add(addClient);
f.getContentPane().add(openPolicy);
openPolicy.setSize(300, 60);
addClient.setSize(380,60);
addClient.setLocation(30, 180);
openPolicy.setLocation(450, 180);
//JTextField
JTextField name = new JTextField("Name: ");
f.getContentPane().add(name);
name.setLocation(30,30);
name.setSize(380, 30);
//JList
String[] clientarray = {"Mark Mywords", "Jim Class", "Stan Dupp", "Mel Onhead", "Bob's Yoyo Shop", "Toys Aren't Us", "The Fish Rack"};
JList clients = new JList(clientarray);
f.getContentPane().add(clients);
clients.setLocation(30, 280);
clients.setSize(380, 250);
String[] policyarray = {"Policy 002354","Policy 005345", "Depreciable Policy 0789423", "Expirable Policy 009724"};
JList policies = new JList(policyarray);
f.getContentPane().add(policies);
policies.setLocation(450, 280);
policies.setSize(300, 250);
//JScrollPane
JScrollPane clientScroll = new JScrollPane(clients, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
f.getContentPane().add(clientScroll);
clientScroll.setLocation(30,280);
clientScroll.setSize(380,250);
JScrollPane policiesScroll = new JScrollPane(policies, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
f.getContentPane().add(policiesScroll);
policiesScroll.setLocation(450,280);
policiesScroll.setSize(300,250);
//JradioButons
JRadioButton b1 = new JRadioButton("Company",false);
JRadioButton b2 = new JRadioButton("Individual",false);
JRadioButton b3 = new JRadioButton("Standard",false);
JRadioButton b4 = new JRadioButton("Depreciable",false);
JRadioButton b5 = new JRadioButton("Expirable",false);
//ButtonGroups
ButtonGroup clientsGroup = new ButtonGroup();
b1.setLocation(120, 70);
b1.setSize(100,30);
b2.setLocation(260, 70);
b2.setSize(100,30);
f.getContentPane().add(b2);
f.getContentPane().add(b1);
clientsGroup.add(b1);
clientsGroup.add(b2);
ButtonGroup policiesGroup = new ButtonGroup();
b3.setLocation(600, 10);
b4.setLocation(600, 60);
b5.setLocation(600, 110);
b3.setSize(100,60);
b4.setSize(100,60);
b5.setSize(100,60);
f.getContentPane().add(b3);
f.getContentPane().add(b4);
f.getContentPane().add(b5);
policiesGroup.add(b3);
policiesGroup.add(b4);
policiesGroup.add(b5);
//Jlabel
JLabel label = new JLabel("Type:");
f.getContentPane().add(label);
label.setLocation(30, 55);
label.setSize(60,60);
JLabel label2 = new JLabel("Type:");
f.getContentPane().add(label2);
label2.setLocation(545, 10);
label2.setSize(60,60);
}
}

The problem is that the JRadioButtons are not being painted as the JFrame has already been displayed when they are added.
You need to call JFrame#setVisible after you add all the components to the container:
f.setVisible(true);
Aside: Don't use absolute positioning (null layout), use a layout manager instead. Using a layout manager removes the need for the setting component sizes & positions.

Related

Why are my JButton and JLists not appearing? (BorderLayout

I'm trying to learn swing and work through the task we have been given. I can't see why the code doesn't display the JButtons in the SOUTH section as there is no issue when displaying the textfields, combobox and labels in the CENTER section.
I used the same format to add components to my CENTER section as I did in the SOUTH and EAST but only the center displays anything.
public class ProductListGUI{
JMenu menu;
JMenuItem about,importData,inventory,export;
ProductListGUI(){
JFrame f = new JFrame("Assignment 2");
JPanel p1 = new JPanel();
JList<String> list = new JList<>();
list.setBounds(600,0,200,600);
JScrollPane scrollPane = new JScrollPane(list,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
JPanel p3 = new JPanel();
p1.setLayout(null);
p1.setBounds(0,0,600,500);
p1.setBackground(new Color(230,230,230));
scrollPane.setLayout(null);
scrollPane.setBounds(600,0,200,500);
p3.setLayout(null);
p3.setBounds(0,500,800,100);
p3.setBackground(new Color(230,230,230));
JLabel l1,l2,l3,l4;
JTextField t1,t2,t3;
l1=new JLabel("ProductID");
l1.setBounds(10,100,200,30);
t1=new JTextField();
t1.setBounds(100,100,200,30);
l2=new JLabel("Name");
l2.setBounds(10,150,200,30);
t2=new JTextField();
t2.setBounds(100,150,200,30);
l3=new JLabel("Quantity");
l3.setBounds(10,250,200,30);
t3=new JTextField();
t3.setBounds(100,250,200,30);
p1.setBorder(BorderFactory.createTitledBorder("Product Details"));
JCheckBox checkBox = new JCheckBox("Available for Next Day Delivery");
checkBox.setBounds(10,300,250,50);
l4 = new JLabel("Item Type");
l4.setBounds(10,200,200,30);
String[] itemType = {"Select type","Homeware","Hobby","Garden"};
JComboBox dropdown = new JComboBox(itemType);
dropdown.setBounds(100,200,120,20);
p1.add(t1);p1.add(l1);p1.add(t2);p1.add(l2);p1.add(t3);p1.add(l3);p1.add(l4);p1.add(dropdown);p1.add(checkBox);
JButton b1 = new JButton("New Item");
b1.setBounds(200,550,80,20);
JButton b2 = new JButton("Save");
b2.setBounds(300,550,80,20);
JButton b3 = new JButton("Delete Selected");
b3.setBounds(600,550,80,20);
b3.setEnabled(false);
p3.add(b1);p3.add(b2);p3.add(b3);
JMenuBar mb = new JMenuBar();
menu = new JMenu("Actions");
about = new JMenuItem("About");
importData = new JMenuItem("Import Data");
inventory = new JMenuItem("Inventory");
export = new JMenuItem("Export to CSV");
menu.add(about);menu.add(importData);menu.add(inventory);menu.add(export);
mb.add(menu);
f.getContentPane().add(p1,BorderLayout.CENTER);
f.getContentPane().add(scrollPane,BorderLayout.EAST);
f.getContentPane().add(p3,BorderLayout.SOUTH);
f.setJMenuBar(mb);
f.setSize(800,600);
f.setLayout(new BorderLayout());
f.setVisible(true);
}
These code changes should help at least a little bit.
Things that had to change:
Each JPanel should have a layout. Setting the layout to null (as per TG's comment) is not good.
The setBounds() methods for the buttons were removed.
The f.setLayout(new BorderLayout()); was moved to the top of the code where Components were being added to the JFrame before the layout was set.
import javax.swing.*;
import java.awt.*;
public class ProductListGUI {
JMenu menu;
JMenuItem about,importData,inventory,export;
ProductListGUI(){
JFrame f = new JFrame("Assignment 2");
JPanel panel1 = new JPanel();
JList<String> list = new JList<>();
list.setBounds(600,0,200,600);
JScrollPane scrollPane = new JScrollPane(list,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
JPanel panel3 = new JPanel();
panel1.setBounds(0,0,600,500);
panel1.setBackground(new Color(230,230,230));
scrollPane.setBounds(600,0,200,500);
panel3.setBounds(0,0,800,100);
panel3.setBackground(new Color(230,230,230));
JLabel l1,l2,l3,l4;
JTextField t1,t2,t3;
l1=new JLabel("ProductID");
l1.setBounds(10,100,200,30);
t1=new JTextField();
t1.setBounds(100,100,200,30);
l2=new JLabel("Name");
l2.setBounds(10,150,200,30);
t2=new JTextField();
t2.setBounds(100,150,200,30);
l3=new JLabel("Quantity");
l3.setBounds(10,250,200,30);
t3=new JTextField();
t3.setBounds(100,250,200,30);
panel1.setBorder(BorderFactory.createTitledBorder("Product Details"));
JCheckBox checkBox = new JCheckBox("Available for Next Day Delivery");
checkBox.setBounds(10,300,250,50);
l4 = new JLabel("Item Type");
l4.setBounds(10,200,200,30);
String[] itemType = {"Select type","Homeware","Hobby","Garden"};
JComboBox dropdown = new JComboBox(itemType);
dropdown.setBounds(100,200,120,20);
panel1.add(t1);
panel1.add(l1);
panel1.add(t2);
panel1.add(l2);
panel1.add(t3);
panel1.add(l3);
panel1.add(l4);
panel1.add(dropdown);
panel1.add(checkBox);
JButton b1 = new JButton("New Item");
JButton b2 = new JButton("Save");
JButton b3 = new JButton("Delete Selected");
b3.setEnabled(false);
panel3.add(b1);
panel3.add(b2);
panel3.add(b3);
JMenuBar mb = new JMenuBar();
menu = new JMenu("Actions");
about = new JMenuItem("About");
importData = new JMenuItem("Import Data");
inventory = new JMenuItem("Inventory");
export = new JMenuItem("Export to CSV");
menu.add(about);
menu.add(importData);
menu.add(inventory);
menu.add(export);
mb.add(menu);
f.setLayout(new BorderLayout());
f.getContentPane().add(panel1,BorderLayout.CENTER);
f.getContentPane().add(scrollPane,BorderLayout.EAST);
f.getContentPane().add(panel3,BorderLayout.SOUTH);
f.setJMenuBar(mb);
f.setSize(800,600);
f.setVisible(true);
}
public static void main(String[] args) {
ProductListGUI gui = new ProductListGUI();
}
}
Here is a very simple BorderLayout example that might help in the future, or not.
import javax.swing.*;
import java.awt.*;
public class TestGui {
public static void main(String[] args) {
JFrame frame = new JFrame("Test Frame");
frame.setLayout(new BorderLayout());
frame.setSize(800,600);
frame.getContentPane().add(createJPanel("CENTER", Color.RED), BorderLayout.CENTER);
frame.getContentPane().add(createJPanel("NORTH", Color.CYAN), BorderLayout.NORTH);
frame.getContentPane().add(createJPanel("EAST", Color.LIGHT_GRAY), BorderLayout.EAST);
frame.getContentPane().add(createJPanel("SOUTH", Color.GREEN), BorderLayout.SOUTH);
frame.getContentPane().add(createJPanel("WEST", Color.YELLOW), BorderLayout.WEST);
frame.setVisible(true);
}
private static JPanel createJPanel(String title, Color color) {
JPanel jPanel = new JPanel();
jPanel.setLayout(new BorderLayout());
jPanel.add(new JLabel(title), BorderLayout.CENTER);
jPanel.setBackground(color);
return jPanel;
}
}

background image hidding the other components like buttons labels and other, and vicce versa

how to solve hidding of components in this code
code is running without errors
but background image not displayed
how to change code to get the background image
when using validation method, its creating error in validation()
public class TEST{
public TEST() {
String[] strm = {"Jan", "Feb", "Mar", "Apr", "May"};
String[] stry = {"2016", "2017", "2018","2019"};
String[] strf={"NEW Delhi", "Bangalore", "Chennai"};
String[] strt={"Goa","Kashmir","Hyderabad"};
JFrame f = new JFrame("test");
f.setSize(500, 500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lfr = new JLabel("FROM");
JLabel lto = new JLabel("TO");
JLabel lda = new JLabel("DATE");
JLabel ld = new JLabel("DAY");
JLabel lm = new JLabel("MONTH");
JLabel y = new JLabel("YEAR");
JComboBox cfr = new JComboBox(strf);
JComboBox cto = new JComboBox(strt);
JComboBox cd = new JComboBox();
JComboBox cm = new JComboBox(strm);
JComboBox cy = new JComboBox(stry);
JButton bs = new JButton("Search");
JPanel p1 = new JPanel(null);
p1.setPreferredSize(new Dimension(500,500));
JLabel jimage = new JLabel();
jimage.setIcon(new ImageIcon("air.jpg"));
for(int i = 1; i <= 31; i++)
cd.addItem(i);
lfr.setBounds(20, 40, 100, 20);
cfr.setBounds(100, 40, 100, 20);
lto.setBounds(20, 100, 25, 20);
cto.setBounds(100, 100, 100, 20);
lda.setBounds(20, 160, 50, 20);
cd.setBounds(100, 160, 50, 20);
cm.setBounds(160, 160, 65, 20);
cy.setBounds(240, 160, 75, 20);
ld.setBounds(100, 190, 50, 20);
lm.setBounds(160, 190, 50, 20);
y.setBounds(240, 190, 50, 20);
bs.setBounds(20, 230, 100, 20);
p1.add(lfr);
p1.add(cfr);
p1.add(lto);
p1.add(cto);
p1.add(lda);
p1.add(cd);
p1.add(cm);
p1.add(cy);
p1.add(ld);
p1.add(lm);
p1.add(y);
p1.add(bs);
p1.add(jimage);
// validate();
f.add(p1);
f.setVisible(true);
}
public static void main(String[] args) {
new TEST();
}
}
The best you can do is something like:
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Test {
JLabel label;
JComboBox combo;
JButton button;
JPanel pane;
JFrame frame;
JPanel create1stRow() {
JPanel pane = new JPanel();
pane.setLayout(new FlowLayout(FlowLayout.LEFT));
pane.setOpaque(false);
String options[] = {"New Delhi", "Bangalore", "Chennai"};
label = new JLabel("FROM");
combo = new JComboBox(options);
pane.add(label);
pane.add(combo);
return pane;
}
JPanel create2ndRow() {
JPanel pane = new JPanel();
pane.setLayout(new FlowLayout(FlowLayout.LEFT));
pane.setOpaque(false);
String options[] = {"Goa", "Kashmir", "Hyderabad"};
label = new JLabel("TO");
combo = new JComboBox(options);
pane.add(label);
pane.add(combo);
return pane;
}
JPanel create3rdRow() {
JPanel pane = new JPanel();
JPanel dataPane = new JPanel();
pane.setLayout(new FlowLayout(FlowLayout.LEFT));
pane.setOpaque(false);
dataPane.setOpaque(false); //forgot to add this line when I took the pic
dataPane.setLayout(new GridLayout(2, 3)); //2 rows, 3 cols, so we can have the combos with their text aligned
String days[] = {"1", "2", "3", "4", "5"}; //Too lazy to write 31 days
String months[] = {"Jan", "Feb", "Mar", "Apr", "May"}; //Too lazy to write 12 months
String years[] = {"2016", "2017", "2018", "2019", "2020"};
label = new JLabel("DATE");
combo = new JComboBox(days);
//We add the combos
dataPane.add(combo);
combo = new JComboBox(months); //We're reusing the object, but change the data
dataPane.add(combo);
combo = new JComboBox(years); //The same as above
dataPane.add(combo);
//Now we add the labels
dataPane.add(new JLabel("DAYS"));
dataPane.add(new JLabel("MONTHS"));
dataPane.add(new JLabel("YEARS"));
pane.add(label);
pane.add(dataPane); //We add the whole pane to another one
return pane;
}
JPanel create4thRow() {
JPanel pane = new JPanel();
pane.setLayout(new FlowLayout(FlowLayout.LEFT));
pane.setOpaque(false);
button = new JButton("Search");
pane.add(button);
return pane;
}
public Test() {
frame = new JFrame("Test");
frame.setContentPane(new JLabel(new ImageIcon("C:/Users/Frakcool/workspace/StackOverflowProjects/src/test/Air.jpg")));
frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.PAGE_AXIS));
pane = create1stRow();
frame.add(pane);
pane = create2ndRow();
frame.add(pane);
pane = create3rdRow();
frame.add(pane);
pane = create4thRow();
frame.add(pane);
frame.pack();
//frame.setSize(500, 500); //If your image is too large use this
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main (String args[]) {
new Test();
}
}
As you can see in the above code, I'm not using a null layout but a combination of multiple Layout Managers and I suggest you to do it like this in the future.
But if you still want to use that ugly null layout, you were missing this line:
jimage.setBounds(0, 0, 500, 500);
before this one:
lfr.setBounds(20, 40, 100, 20);
The output that my above code gives is:
And the output given by your code with the line I added is:
As you can see, both are really similar, and I could have done them identical but I don't have enough time to do so, but you can by combining the Layout Managers I posted above.
Note: I forgot to mention that to make this program to show the background image, I needed to make every other panels not opaque with pane.setOpaque(false); so, be sure to use this whenever you need to show something that is behind another panel.

My GUI is not showing up fully

When I run my program, it only shows a few lines, and then it shows gray under it. Can someone explain to me why this happens? I wanted the grid layout to have 8 rows, which should include the labels and text boxes. I am not sure why only a few of them appear.
public class Application extends JFrame {
private JPanel panel;
private JLabel label1, label2, label3, label4, label5, label6, label7,
label8;
private JTextField text1, text2, text3, text4, text5, text6, text7, text8;
public Application() {
JFrame gui = new JFrame();
gui.setLayout(new GridLayout(8, 2));
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setTitle("Vacation Expenses");
gui.setSize(500, 500);
panel = new JPanel();
gui.add(panel);
label1 = new JLabel("Number of days on the trip");
label2 = new JLabel("Amount of airfare");
label3 = new JLabel("Amount of car rental fees");
label4 = new JLabel(
"Number of miles driven, if a private vehicle was used");
label5 = new JLabel("Amount of parking fees, if any");
label6 = new JLabel("Amount of taxi charges, if any");
label7 = new JLabel("Conference or seminar registration fees, if any");
label8 = new JLabel("Lodging charges, per night");
text1 = new JTextField("0", 10);
text2 = new JTextField("0", 10);
text3 = new JTextField("0", 10);
text4 = new JTextField("0", 10);
text5 = new JTextField("0", 10);
text6 = new JTextField("0", 10);
text7 = new JTextField("0", 10);
text8 = new JTextField("0", 10);
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(label3);
panel.add(text3);
panel.add(label4);
panel.add(text4);
panel.add(label5);
panel.add(text5);
// JButton button = new JButton("Button");
// panel.add(button);
gui.setVisible(true);
}
public static void main(String[] args) {
new Application();
}
}
You should be setting the GridLayout to your panel and not the frame. The panel is the container for the components, so should be the one with the GridLayout
Get rid of gui.setLayout(new GridLayout(8, 2));
And use panel = new JPanel(new GridLayout(8, 2));
Side Notes
Also note you haven't added all your components. You've only added five of each. You're forgetting to add the other three.
Also, your class is already a JFrame. There's no need to create another one. Choose one or the other. Either use the instance JFrame and don't extends JFrame or extend JFrame and don't use the extra instance. I'd go with the former.
Also, it's best to pack() your frame, instead of setSize(). The pack() should be done after adding all your components.
Also, Swing apps should be run from the Event Dispatch Thread. You can accomplish this by wrapping the main method content in a SwingUtilities.invokeLater(...). See more at Initial Threads

JButton turns non-transparent when hovered over

I'm trying to make a small game (just for fun because i get bored) and at the current time i have a few panels, buttons, frames, images and whatnot already in it working well, but i'm come to a problem that i couldn't figure out for the past few days so i thought i'd ask here
The problem i have is i want to set a JButton transparent so that all you see is the image that is placed onto it (left and right arrows), and it works....except for when you hover your mouse over the transparent button, a different part of the screen becomes the background/foreground of the JButton instead of it still being transparent
(The White Spots are where my mouse is at the time, i do not click or anything, it just changes when you interact with the JButton, look specifically at the larger left arrow)
Image Without Mouse on JButton - http://i.imgur.com/xWWE5E0.png
Image With Mouse on JButton - http://i.imgur.com/8PosnwP.png
As you can see the previously transparent JButton has not got a background/foreground and most definitely not transparent anymore
The code for the JFrame is like this (i tried to make a smaller demo but it works properly in a plain, new frame so here's my actual code(feel free to give tips on improving it by PM))
package com.Braxeo.Games;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class CharacterNew
{
static ImageIcon LArrow = new ImageIcon("Materials/LeftArrow.png");
static ImageIcon RArrow = new ImageIcon("Materials/RightArrow.png");
static ImageIcon bg = new ImageIcon("Materials/CharacterNewBackground.jpg");
public static JPanel CreateNewCharacter()
{
JPanel contentPane = new JPanel();
JPanel BackGround = new JPanel();
JLabel background = new JLabel();
JLabel shipshape = new JLabel();
JButton shapeleft = new JButton();
JButton shaperight = new JButton();
JLabel shapetext = new JLabel();
JPanel ShapeText = new JPanel();
JPanel ShipShape = new JPanel();
JPanel Shape = new JPanel();
JLabel shipcolor = new JLabel();
JButton colorleft = new JButton();
JButton colorright = new JButton();
JLabel colortext = new JLabel();
JPanel ShipColor = new JPanel();
JPanel ShipText = new JPanel();
JPanel SColor = new JPanel();
JLabel chartext = new JLabel();
JLabel charicon = new JLabel();
JButton iconleft = new JButton();
JButton iconright = new JButton();
JPanel iconpane = new JPanel();
JPanel icontext = new JPanel();
JPanel icon = new JPanel();
JButton back = new JButton();
JPanel backtext = new JPanel();
JButton continu = new JButton();
JPanel cont = new JPanel();
JLabel charname = new JLabel();
JTextField typename = new JTextField();
JPanel charnamePane = new JPanel();
// Shape
ShapeText.setLayout(new GridLayout(1,1));
ShipShape.setLayout(new GridLayout(1,3));
Shape.setLayout(new BorderLayout());
shipshape = new JLabel("ADD SSHIPS");
shapeleft = new JButton(LArrow);
shaperight = new JButton(RArrow);
shapeleft.setContentAreaFilled(false);
shapeleft.setOpaque(false);
shapeleft.setBackground(new Color(0,0,0,0));
shapetext = new JLabel("Choose Shape of Ship");
ShipShape.setBackground(new Color(0,0,0,0));
Shape.setBackground(new Color(0,0,0,0));
Shape.setBounds(600, 100, 500, 400);
ShipShape.add(shapeleft);
ShipShape.add(shipshape);
ShipShape.add(shaperight);
ShapeText.add(shapetext);
Shape.add(ShapeText, BorderLayout.PAGE_START);
Shape.add(ShipShape);
contentPane.add(Shape);
// Color
ShipColor.setLayout(new GridLayout(1, 3));
ShipText.setLayout(new GridLayout(1,1));
SColor.setLayout(new BorderLayout());
colortext = new JLabel(" Choose Color of your Ship");
shipcolor = new JLabel("ADD IMAGES");
colorleft = new JButton("ADD LEFT ARROW");
colorright = new JButton("ADD RIGHT ARROW");
colortext.setFont(new Font("Serif", Font.BOLD, 32));
ShipText.setBackground(new Color(0,0,0,0));
colortext.setForeground(Color.red);
SColor.setBackground(new Color(0,0,0,0));
ShipText.setOpaque(true);
ShipText.add(colortext);
ShipColor.add(colorleft);
ShipColor.add(shipcolor);
ShipColor.add(colorright);
SColor.setBounds(Main.x - 750,400,380,145);
SColor.add(ShipText, BorderLayout.PAGE_START);
SColor.add(ShipColor);
contentPane.add(SColor);
// Icon
iconpane.setLayout(new GridLayout(1,3));
icontext.setLayout(new GridLayout(1,1));
icon.setLayout(new BorderLayout());
chartext = new JLabel(" Pick Your Desired Icon");
charicon = new JLabel("PLACE IMAGE");
iconleft = new JButton("PLACE LEFT ICON");
iconright = new JButton("PLACE right ICON");
chartext.setFont(new Font("Serif", Font.BOLD, 32));
icontext.setBackground(new Color(0,0,0,0));
icon.setBackground(new Color(0,0,0,0));
chartext.setForeground(Color.red);
icontext.setOpaque(true);
icontext.add(chartext);
iconpane.add(iconleft);
iconpane.add(charicon);
iconpane.add(iconright);
icon.setBounds(Main.x - 750,550,380,145);
icon.add(icontext, BorderLayout.PAGE_START);
icon.add(iconpane);
contentPane.add(icon);
// back
backtext.setLayout(new BorderLayout());
back = new JButton("Back");
backtext.setBounds(50, Main.y-150, 252,76);
back.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Main.defaultframe.getContentPane().removeAll();
Main.defaultframe.getContentPane().revalidate();
Main.defaultframe.setContentPane(CharacterSelect.CreateLoadCharacter());
Main.defaultframe.setVisible(true);
}
});
backtext.add(back);
contentPane.add(backtext);
// continue
cont.setLayout(new BorderLayout());
continu = new JButton("Continue");
cont.setBounds(Main.x-302, Main.y-150, 252,76);
cont.add(continu);
contentPane.add(cont);
// Name
charnamePane.setLayout(new GridLayout( 2,1));
charname = new JLabel(" Enter Your Name Below");
charname.setFont(new Font("Impact", Font.BOLD, 32));
typename.setSize(252, 76);
typename.setFont(new Font("Razer Text Regular", Font.BOLD, 22));
typename.setBackground(Color.LIGHT_GRAY);
typename.setHorizontalAlignment(JLabel.CENTER);
charnamePane.setBounds(100,200,400,100);
charnamePane.setOpaque(true);
charnamePane.setBackground(new Color(0,0,0,0));
charname.setBackground(new Color(0,0,0,0));
charname.setForeground(Color.white);
charnamePane.add(charname);
charnamePane.add(typename, BorderLayout.PAGE_START);
contentPane.add(charnamePane);
// Background
background = new JLabel(bg);
BackGround.setLayout(new BorderLayout());
BackGround.setBounds(0, 0, Main.x, Main.y);
BackGround.add(background);
contentPane.add(BackGround);
contentPane.setLayout(new BorderLayout());
return contentPane;
}
}
The code that works out the Transparent JButton is
JButton shapeleft = new JButton();
JPanel ShapeText = new JPanel();
JPanel Shape = new JPanel();
shapeleft = new JButton(LArrow);
shapeleft.setContentAreaFilled(false);
shapeleft.setOpaque(false);
shapeleft.setBackground(new Color(0,0,0,0));
ShipShape.setBackground(new Color(0,0,0,0));
Shape.setBackground(new Color(0,0,0,0));
Shape.setBounds(600, 100, 500, 400);
ShipShape.add(shapeleft);
Shape.add(ShipShape);
contentPane.add(Shape);
If anyone could please help me figure out why its playing up like it is that would be a massive help, and i'm still a beginner so all help is good help :) thanks :)
ShipShape.setBackground(new Color(0,0,0,0));
Check out Background With Transparency for the probable problem and a couple of solutions.
Basically you need to make sure the parent component gets painted before you paint your components background.

Java GUI (SWING/AWT) - Empty Frame - Components not showing

I'm trying to create (hand-coded) a GUI similair to the GUI shown below, however, only an empty frame shows.
Mock GUI:
I've used various layouts and SWING/AWT components to create the GUI and 4 JPanels which contain:
mainPanel: Contains all the panels in it.
listPanel: Contains the JTables, JLabels and the two JButtons
infoPanel: Contains the JLabels, JCheckBox and JTextBoxes.
addPanel: Contains the JLists and JButton
This is what I coded so far:
import java.awt.*;
import javax.swing.*;
import javax.swing.JTable;
public class GUI extends JFrame {
public void buildGui() {
JFrame frame = new JFrame("Hotel TV Scheduler");
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
JPanel listPanel = new JPanel();
listPanel.setLayout(new GridLayout(3,3));
JPanel infoPanel = new JPanel();
infoPanel.setLayout(new GridLayout(2,2));
JPanel addPanel = new JPanel();
addPanel.setLayout(new FlowLayout());
mainPanel.add(listPanel, BorderLayout.LINE_START);
mainPanel.add(infoPanel, BorderLayout.LINE_END);
mainPanel.add(addPanel, BorderLayout.PAGE_END);
JTable chOneTable = new JTable();
JTable chTwoTable = new JTable();
JTable listTable = new JTable();
JLabel ch1Label = new JLabel("Channel 1");
JLabel ch2Label = new JLabel("Channel 2");
JLabel listLabel = new JLabel("List");
JButton rmvChOneButton = new JButton("Remove Channel");
JButton rmvChTwoButton = new JButton("Remove Channel");
listPanel.add(ch1Label);
listPanel.add(ch2Label);
listPanel.add(listLabel);
listPanel.add(chOneTable);
listPanel.add(chTwoTable);
listPanel.add(listTable);
listPanel.add(rmvChOneButton);
listPanel.add(rmvChTwoButton);
JLabel titleLabel = new JLabel("Title");
JLabel genreLabel = new JLabel("Genre");
JLabel durationLabel = new JLabel("Duration");
JLabel actorLabel = new JLabel("Actor");
JLabel directorLabel = new JLabel("Director");
JLabel rentableLabel = new JLabel("Rentable");
JLabel synLabel = new JLabel("Synopsis");
JTextField txtTitle = new JTextField();
JTextField txtGenre = new JTextField();
JTextField txtDuration = new JTextField();
JTextField txtActor = new JTextField();
JTextField txtDirector = new JTextField();
JTextField txtSynopsis = new JTextField();
JCheckBox rentCB = new JCheckBox();
infoPanel.add(titleLabel);
infoPanel.add(txtTitle);
infoPanel.add(genreLabel);
infoPanel.add(txtGenre);
infoPanel.add(durationLabel);
infoPanel.add(txtDuration);
infoPanel.add(actorLabel);
infoPanel.add(txtActor);
infoPanel.add(directorLabel);
infoPanel.add(txtDirector);
infoPanel.add(rentableLabel);
infoPanel.add(rentCB);
infoPanel.add(synLabel);
infoPanel.add(txtSynopsis);
JButton btnAddProg = new JButton("Add Program");
JList channelList = new JList();
JList timeList = new JList();
addPanel.add(btnAddProg);
addPanel.add(channelList);
addPanel.add(timeList);
frame.setVisible(true);
}
}
Anyone can tell me why only an empty frame is showing up ?
Thanks and Regards,
Brian
Yep just checked, you'll see something if you actually add the mainPanel to the frame, (looks nothing like the mock though!)
frame.setContentPane(mainPanel);
frame.pack();
You've not added mainPanel to the frame

Categories

Resources