I have an Interface class that extends JFrame. I am creating 3 JPanels and adding them to a Container (Border Layout) in different areas of the layout.
When I run the application, nothing shows but a blank window and a title. If I resize the application it will then show all the content and work as intended.
I'm not sure what I did differently this time, I've used this method before and it works in previous programs.
Any help is appreciated.
Here is my Interface Class Constructor code: http://pastebin.com/4UyEXsBr
Code:
public class Interface extends JFrame implements ActionListener {
private Container contentPane;
private JPanel buttonPanel, userPanel;
private JButton loginButton, createUserButton, logoutButton, withdrawButton, depositButton, switchToRegisterButton, switchToLoginButton;
private JLabel headerLabel, inputTopJTFLabel, inputPW1JPFLabel, toastLabel, inputPW2JPFLabel;
public JTextField inputTopJTF;
public JPasswordField inputPW1JPF, inputPW2JPF;
JRootPane rootPane;
public Interface(int width, int height, String title) {
//Setting up Interface
setTitle(title);
setSize(width, height);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(100,100);
setVisible(true);
Font header = new Font("TimesRoman", Font.PLAIN, 50);
///////////////////////BUTTON PANEL///////////////////////
//Button Panel
buttonPanel = new JPanel();
//Buttons
//loginButton
loginButton = new JButton("Login");
loginButton.addActionListener(this);
loginButton.setAlignmentX(Component.CENTER_ALIGNMENT);
//switchToRegisterButton
switchToRegisterButton = new JButton("New User?");
switchToRegisterButton.addActionListener(this);
switchToRegisterButton.setAlignmentX(Component.CENTER_ALIGNMENT);
//switchToLoginButton
switchToLoginButton = new JButton("Switch to Login");
switchToLoginButton.addActionListener(this);
switchToLoginButton.setAlignmentX(Component.CENTER_ALIGNMENT);
switchToLoginButton.setVisible(false);
//createUserButton
createUserButton = new JButton("Register");
createUserButton.addActionListener(this);
createUserButton.setAlignmentX(Component.CENTER_ALIGNMENT);
createUserButton.setVisible(false);
//logoutButton
logoutButton = new JButton("Logout");
logoutButton.addActionListener(this);
logoutButton.setAlignmentX(Component.CENTER_ALIGNMENT);
logoutButton.setVisible(false);
//withdrawButton
withdrawButton = new JButton("Withdraw");
withdrawButton.addActionListener(this);
withdrawButton.setAlignmentX(Component.CENTER_ALIGNMENT);
withdrawButton.setVisible(false);
//depositButton
depositButton = new JButton("Deposit");
depositButton.addActionListener(this);
depositButton.setAlignmentX(Component.CENTER_ALIGNMENT);
depositButton.setVisible(false);
//Adding items to buttonPanel
buttonPanel.add(loginButton);
buttonPanel.add(switchToRegisterButton);
buttonPanel.add(switchToLoginButton);
buttonPanel.add(createUserButton);
buttonPanel.add(logoutButton);
buttonPanel.add(withdrawButton);
buttonPanel.add(depositButton);
///////////////BODY PANEL//////////////////////
//Body Panel
userPanel = new JPanel();
userPanel.setLayout(new BoxLayout(userPanel, BoxLayout.PAGE_AXIS));
//JTextFields
//inputTopJTF
Dimension inputTopJTFDimension = new Dimension(100, 20);
inputTopJTF = new JTextField();
inputTopJTF.setMaximumSize(inputTopJTFDimension);
//inputPW1JPF
Dimension inputPW1JPFDimension = new Dimension(100, 20);
inputPW1JPF = new JPasswordField();
inputPW1JPF.setMaximumSize(inputPW1JPFDimension);
//inputPW2JPF
Dimension inputPW2JPFDimension = new Dimension(100, 20);
inputPW2JPF = new JPasswordField();
inputPW2JPF.setMaximumSize(inputPW2JPFDimension);
inputPW2JPF.setVisible(false);
//JLabels
//toastLabel
toastLabel = new JLabel("Please sign in or create new account.");
toastLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
//inputTopJTFLabel
inputTopJTFLabel = new JLabel("Username:");
inputTopJTFLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
//inputPW1JPFLabel
inputPW1JPFLabel = new JLabel("Password:");
inputPW1JPFLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
//inputPW2JPFLabel
inputPW2JPFLabel = new JLabel("Confirm Password:");
inputPW2JPFLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
inputPW2JPFLabel.setVisible(false);
//Adding items to userPanel
userPanel.add(toastLabel);
userPanel.add(inputTopJTFLabel);
userPanel.add(inputTopJTF);
userPanel.add(inputPW1JPFLabel);
userPanel.add(inputPW1JPF);
userPanel.add(inputPW2JPFLabel);
userPanel.add(inputPW2JPF);
///////////////CONTENT PANE/////////////////////
//Content Pane
contentPane = getContentPane();
//JLabels
//headerLabel
headerLabel = new JLabel("Bank", SwingConstants.CENTER);
headerLabel.setFont(header);
//PAGE_START
contentPane.add(headerLabel, BorderLayout.PAGE_START);
//LINE_START
//CENTER
contentPane.add(userPanel, BorderLayout.CENTER);
//LINE_END
//PAGE_END
contentPane.add(buttonPanel, BorderLayout.PAGE_END);
userPanel.setFocusable(true);
userPanel.requestFocus();
//Default Button
rootPane = getRootPane();
rootPane.setDefaultButton(loginButton);
}
call
setVisible(true);
in last line .because component added after line setvisible will not be shown until you call repaint(),revalidate().when you resize ,repaint() method get called and frame will visible correctly .so call setvisible after add all component
after line rootPane.setDefaultButton(loginButton); call setvisible
rootPane.setDefaultButton(loginButton);
setVisible(true);//after add all component to frame call setvisible method
this is full working code
Related
There are two things that I am trying to figure out. First thing, I want to figure out how to make a Jcomponent background transparent. When placing a JPanel into another JPanel that has a color set as the background, the JPanel that is set within the other JPanel has a white background that I can't seem to get rid of. I tried using the firstpanel.setOpache function and repaint but it doesn't do anything.
And second, I noticed that putting a JPanel within another JPanel thats within another JPanel compresses it size. The images below will show what I am trying to describe. I want to know what to do to avoid compressing the JPanel size and still able to put it within two levels of other JPanels . The code below is what I am practicing with.
import javax.swing.*;
import java.awt.*;
public class LearningFrame extends JFrame {
private JLabel userLabel;
private LearningFrame(){
JPanel backGroundPanel = new JPanel();
JPanel mainPanel = new JPanel();
JPanel firstPanel = new JPanel();
JPanel secondPanel = new JPanel();
userLabel = new JLabel("User");
userLabel.setFont(new Font("Arial",1 ,24));
backGroundPanel.setBackground(new Color(247,211,53));
// backGroundPanel.setLayout(new BoxLayout(backGroundPanel,BoxLayout.Y_AXIS));
setContentPane(backGroundPanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1200,800);
setLocationRelativeTo(null);
//backGroundPanel.setLayout(null);
mainPanel.setLayout(new GridLayout(1,2));
firstPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
secondPanel.setLayout(new BoxLayout(secondPanel,BoxLayout.Y_AXIS));
// firstPanel.setBackground(new Color(211,43,185));
secondPanel.setBackground(new Color(34,233,44));
JButton button = new JButton("Click");
JButton button2 = new JButton("Click");
JButton button3 = new JButton("Click");
JButton button4 = new JButton("Click");
firstPanel.add(button);
firstPanel.add(button2);
firstPanel.add(userLabel);
secondPanel.add(button3);
secondPanel.add(button4);
mainPanel.add(firstPanel);
mainPanel.add(secondPanel);
backGroundPanel.add(mainPanel);
setVisible(true);
}
public JPanel logPanel() {
JPanel logInPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JTextField userTextField = new JTextField(14);
JTextField passTextField = new JTextField(14);
userLabel = new JLabel("Username: ");
JLabel passLabel = new JLabel("Password: ");
passLabel.setFont(new Font("Arial", Font.BOLD, 24));
userLabel.setFont(new Font("Arial", Font.BOLD, 24));
logInPanel.add(userLabel);
logInPanel.add(userTextField);
logInPanel.add(passLabel);
logInPanel.add(passTextField);
logInPanel.setOpaque(true);
logInPanel.repaint();
return logInPanel;
}
public static void main(String[] args){
LearningFrame x = new LearningFrame();
}
}
Just use
firstPanel.setOpaque(false);
This is will make the background of the component invisible, but you will still be able to see any components that are positioned inside it.
I hava a problem with the applet when it is converted into an executable. the applet has three buttons with text as shown in pic_1 below, and when it is converted into an executable, the text of the three buttons disappears and the buttons becomes smalle as shown in pic_2 below.
I downloaded some applications that generates the executable file from a .jar files, and the result after using these applications is the same, the applet runs but with no buttons.
please provide any suggestions to solve this issue.
code_init() method_:
public void init () {
frame = new JFrame ();
frame.setSize(600, 600);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
/* Erzeuge MenuBar*/
menubar = new JMenuBar();
frame.setJMenuBar(menubar);
einstellungen = new JMenu ("Einstellungen");
datei = new JMenuItem ("Datei");
datei.addActionListener(this);
communication = new JMenuItem ("Kommunikation");
communication.addActionListener(this);
simulation = new JMenuItem ("Simulation");
simulation.addActionListener(this);
menubar.add(einstellungen);
einstellungen.add(datei);
einstellungen.add(communication);
einstellungen.add(simulation);
/*Erzeuge Layout */
panel1 = new JPanel ();
top = new JPanel ();
center = new JPanel ();
down = new JPanel ();
bottom = new JPanel ();
frame.add(panel1); //general Panel
panel1.setLayout(new BorderLayout ()); //Separate areas in BorderLayout
panel1.add(top, BorderLayout.NORTH);
panel1.add(center, BorderLayout.CENTER);
panel1.add(bottom, BorderLayout.SOUTH);
Lspeed = new JLabel ("Geschwindigkeit: ");
Lspeedanzeige = new JLabel (" ");
LineBorder lBorder = new LineBorder(new Color(100, 100, 100));
Lspeedanzeige.setBorder(lBorder);
JLabel Lfueller = new JLabel (" ");
Ldrive = new JLabel ("Motordrehzahl: ");
Ldriveanzeige = new JLabel (" ");
LineBorder rBorder = new LineBorder(new Color(100, 100, 100));
Ldriveanzeige.setBorder(rBorder);
top.setLayout(new FlowLayout ()); //add FlowLayout to Panel top
top.add(Lspeed);
top.add(Lspeedanzeige);
top.add(Lfueller);
top.add(Ldrive);
top.add(Ldriveanzeige);
simulationarea = new JTextArea ("Gesendete Daten:");
JScrollPane scrollPane = new JScrollPane(simulationarea);
simulationarea.setPreferredSize(new Dimension(5, 100));
simulationarea.setLineWrap(true);
simulationarea.setWrapStyleWord(true);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(5, 100));
scrollPane.setVisible(true);
simulationarea.setEditable(true);
simulationarea.setEnabled(true);
int pos = simulationarea.getCaretPosition();
simulationarea.insert("file logs", pos+17);
center.setLayout(new BorderLayout ());
center.add(simulationarea, BorderLayout.NORTH); //add JTextfield simulationarea to Panel center
center.add(down, BorderLayout.SOUTH); //add Panel down zu Panel Center
Bplay = new JButton ("Play");
Bplay.setPreferredSize(new Dimension(60, 50));
Bplay.addActionListener(this);
Bbreak = new JButton ("Pause");
Bbreak.addActionListener(this);
Bstop = new JButton ("Stop");
Bstop.addActionListener(this);
down.setLayout(new FlowLayout ()); //add FlowLayout to Panel down
down.add(Bplay);
down.add(Bbreak);
down.add(Bstop);
statusarea = new JTextArea ("Nicht verbunden");
statusarea.setForeground(Color.RED);
statusarea.setPreferredSize(new Dimension(550, 50));
bottom.add(statusarea);
frame.setVisible(true);
System.out.println("init");
}//init
Pic_1
pic_2
I tried to do this from stackoverflow:
adding multiple jPanels to jFrame
But that didn't seem to work out like in the example, could anyone tell me what im doing wrong?
Im trying to add multiple JPanels with each their own sizes to the JFrame. I was also hoping it was possible to give each JPanel specific sizes and ability to put them on the exact spot i want.
Picture of what i try to make:
This is my code so far:
public ReserveringenGUI(ReserveringController controller) {
this.controller = new ReserveringController();
makeFrame();
}
public void makeFrame() {
JFrame frame1 = new JFrame();
frame1.setTitle("Reserveringen");
frame1.setSize(800, 500);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel container = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
JPanel willekeurigPanel = new JPanel();
willekeurigPanel.setSize(400, 500);
willekeurigPanel.setBackground(Color.YELLOW);
willekeurigPanel.setVisible(true);
JPanel overzichtPanel = new JPanel();
overzichtPanel.setSize(400, 500);
overzichtPanel.setBackground(Color.red);
overzichtPanel.setVisible(true);
DateFormat format = new SimpleDateFormat("dd-MM-yyyy");
DateFormatter df = new DateFormatter(format);
JFormattedTextField dateBeginField = new JFormattedTextField(df);
dateBeginField.setPreferredSize(new Dimension(250, 20));
dateBeginField.setValue(new Date());
JFormattedTextField dateEndField = new JFormattedTextField(df);
dateEndField.setPreferredSize(new Dimension(250, 20));
dateEndField.setValue(new Date());
JTextField klantnummer = new JTextField();
klantnummer.setPreferredSize(new Dimension(250, 20));
JTextField artikelnummer = new JTextField();
artikelnummer.setPreferredSize(new Dimension(250, 20));
JLabel dateBeginLabel = new JLabel("Begin Datum ");
JLabel dateEndLabel = new JLabel("Eind datum: ");
JLabel klantID = new JLabel("Klant nummer: ");
JLabel artikelID = new JLabel("Artikel nummer: ");
JButton voegReserveringToe = new JButton("Voeg toe");
voegReserveringToe.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
voegReserveringToeActionPerformed(evt);
}
});
willekeurigPanel.add(dateBeginLabel);
willekeurigPanel.add(dateBeginField);
willekeurigPanel.add(dateEndLabel);
willekeurigPanel.add(dateEndField);
willekeurigPanel.add(klantID);
willekeurigPanel.add(klantnummer);
willekeurigPanel.add(artikelID);
willekeurigPanel.add(artikelnummer);
willekeurigPanel.add(voegReserveringToe);
container.add(willekeurigPanel);
container.add(overzichtPanel);
frame1.add(container);
frame1.setVisible(true);
}
As discussed here, don't set the size and position of components arbitrarily. Instead, let the layout do the work, nesting as required. Use the GroupLayout shown here for the labeled input fields. Add each to the CENTER of a panel having BorderLayout, with a button in the SOUTH on the left. Finally, add both panels to an enclosing panel having GridLayout(1, 0).
public class MainMenu extends JFrame {
private JPanel panel,file_list_panel;
private JPanel contentPane;
private JMenuBar menuBar;
private JMenu mnNewMenu1,mnNewMenu2,mnNewMenu3;
private JMenuItem mt1,mt2,mt3;
private JPanel right,left ,bottom;
private JSplitPane spver ,sphor;
private JTabbedPane tabbedPane;
private JLabel label;
private JList<String> list_1;
private JScrollPane jscroll_list;
private DefaultListModel listmodel_1 = new DefaultListModel();
public MainMenu() {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(0,0,screenSize.width, screenSize.height);
setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
int intValue = Integer.parseInt( "EEEEEE",16);
Color aColor = new Color( intValue );
UIManager.put("TabbedPane.background", new Color(230, 216, 174));
UIManager.put("TabbedPane.selected", Color.WHITE);
UIManager.put("TabbedPane.contentAreaColor",aColor );
UIManager.put("TabbedPane.focus", Color.WHITE);
setTitle("Main Menu");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setBounds(100, 100, 848, 680);
//setLocation(10, 10);
setLocationRelativeTo(null); // set jFrame alignment to center
//parent(first) panel
contentPane = new JPanel();
contentPane.setBackground(Color.BLACK);
contentPane.setBorder(new EmptyBorder(2, 2, 2, 2));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
//Main Menu BAR
menuBar = new JMenuBar();
menuBar.setFont(new Font("Tekton Pro Ext", Font.PLAIN, 11));
setJMenuBar(menuBar);
//Menu 1
mnNewMenu1 = new JMenu("New menu");
menuBar.add(mnNewMenu1);
//Menu 1 MenuItem 1
mt1 = new JMenuItem("Browse New Project");
mt1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
BrowseScreen frame = new BrowseScreen();
frame.setVisible(true);
}
});
mnNewMenu1.add(mt1);
//second panel
panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(new BorderLayout(0, 0));
spver =new JSplitPane(JSplitPane.VERTICAL_SPLIT);
spver.setDividerLocation(500);
spver.setEnabled(false);
bottom=new JPanel();
sphor =new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
sphor.setEnabled(false);
spver.setTopComponent(sphor);
spver.setBottomComponent(bottom);
bottom.setLayout(null);
panel.add(spver);
sphor.setDividerLocation(180);
left =new JPanel();
right =new JPanel();
sphor.setRightComponent(right);
right.setLayout(new GridLayout(0, 1, 0, 0));
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
JPanel tab1 = new JPanel();
tabbedPane.addTab("fffa", tab1);
tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
JPanel tab2 = new JPanel();
tabbedPane.addTab("TAB1", tab2);
tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
right.add(tabbedPane);
sphor.setLeftComponent(left);
left.setLayout(new BorderLayout(0, 0));
file_list_panel= new JPanel();
file_list_panel.setBackground(Color.BLACK);
file_list_panel.setForeground(Color.WHITE);
label = new JLabel("Java Files of Project");
label.setBackground(Color.BLACK);
label.setForeground(Color.WHITE);
label.setFont(new Font("Garamond", Font.BOLD, 14));
file_list_panel.add(label);
left.add(file_list_panel, BorderLayout.NORTH);
list_1 = new JList<String>(listmodel_1);
jscroll_list = new JScrollPane(list_1);
left.add(jscroll_list, BorderLayout.CENTER);
}
public void setList(Vector<String> files){
listmodel_1.removeAllElements();
list_1.removeAll();
for(int i=0;i<files.size();i++)
listmodel_1. addElement(files.elementAt(i));
list_1.setModel(listmodel_1);
this.invalidate();
this.validate();
this.repaint();
}
}
setList is called from the browse window before it calls setVisible(false);
ie . this methods gets called when the browse window disappears .. it does all the things in method but does not update it in the MainMenu
public void setFileList(){
MainMenu mm= new MainMenu();
mm.setList(java_files);
}
list_1 -JList listmodel_1=DefaultListModel
i've tried to refreshing the frame but does not refresh after adding the new list to the Main Window ...
before JList is updated im browsing the the files in another window then it is set to setVisible(false) then MainMenu gets focused and calls the setList method but its not changing
You're creating a new JList<String> and copying the reference to your instance variable - but you're neither removing the old JList from the frame, nor adding the new one. Just changing the variable won't do that.
Rather than creating a new JList<String>, why don't you just replace the model of the existing one? Remove this line:
list_1=new JList<String>(listmodel_1);
and you may find it just works. (You're already setting the model in the subsequent line...)
Basically, you are creating a new JList and associating your model with it, but this has no effect on the JList that is on the screen
public void setList(Vector<String> files){
// Good...
listmodel_1.removeAllElements();
// Not required, has nothing to do with the items in the list
//list_1.removeAll();
// Good
for(int i=0;i<files.size();i++)
listmodel_1. addElement(files.elementAt(i));
// This here is your problem
//list_1=new JList<String>(listmodel_1);
// Because I have no idea what model list_1 is actually using...
list_1.setModel(listmodel_1);
//list_1.setSelectedIndex(0);
//this.invalidate();
//this.validate();
//this.repaint();
}
When I run this program, the window blocks out the buttons in panel2 when I use setSize to determine window size.
In addition, if I use frame.pack() instead of setSize(), all components are on one horizontal line but I'm trying to get them so that panel1 components are on one line and panel2 components are on a line below them.
Could someone explain in detail the answers to both of these problems?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Exercise16_4 extends JFrame{
// FlowLayout components of top portion of calculator
private JLabel jlbNum1 = new JLabel("Number 1");
private JTextField jtfNum1 = new JTextField(4);
private JLabel jlNum2 = new JLabel("Number 2");
private JTextField jtfNum2 = new JTextField(4);
private JLabel jlbResult = new JLabel("Result");
private JTextField jtfResult = new JTextField(8);
// FlowLayout Components of bottom portion of calculator
private JButton jbtAdd = new JButton("Add");
private JButton jbtSubtract = new JButton("Subtract");
private JButton jbtMultiply = new JButton("Multiply");
private JButton jbtDivide = new JButton("Divide");
public Exercise16_4(){
JPanel panel1 = new JPanel();
panel1.setLayout(new FlowLayout(FlowLayout.CENTER, 3, 3));
panel1.add(jlbNum1);
panel1.add(jtfNum1);
panel1.add(jlNum2);
panel1.add(jtfNum2);
panel1.add(jlbResult);
panel1.add(jtfResult);
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout(FlowLayout.CENTER, 3, 10));
panel1.add(jbtAdd);
panel1.add(jbtSubtract);
panel1.add(jbtMultiply);
panel1.add(jbtDivide);
add(panel1, BorderLayout.NORTH);
add(panel2, BorderLayout.CENTER);
}
public static void main(String[] args){
Exercise16_4 frame = new Exercise16_4();
frame.setTitle("Caculator");
frame.setSize(400, 200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setResizable(false);
frame.setVisible(true);
}
}
You're problem is likely a typographical error in that you're adding all components to panel1 and none to panel2:
// you create panel2 just fine
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout(FlowLayout.CENTER, 3, 10));
// but you don't use it! Change below to panel2.
panel1.add(jbtAdd);
panel1.add(jbtSubtract);
panel1.add(jbtMultiply);
panel1.add(jbtDivide);
Add the buttons to panel2, and then call pack() before setVisible(true). Do not set the size of the GUI.