Java - Displaying a title within a contentPane Container - java

I am a newbee so advise and help is always greatly appreciated.
Cannot seem to get my container contentPane to display the title.
My code:
class CreateStockCodeDetails extends JFrame implements ActionListener
{
OptraderSA parent;
OptraderGlobalParameters GV = new OptraderGlobalParameters();
private boolean DEBUG = true; //Set DEBUG = true for Debugging
JButton SAVE_BUTTON = new JButton("SAVE");
JButton CANCEL_BUTTON = new JButton("CANCEL");
Font MyFont = new Font("Helvetica",Font.BOLD,24);
JLabel PriceBidLabel = new JLabel(" Bid Price",JLabel.LEFT);
JLabel PriceAskLabel = new JLabel(" Ask Price",JLabel.LEFT);
JLabel PriceMidLabel = new JLabel(" Mid Price",JLabel.LEFT);
JLabel DividendLabel = new JLabel(" Dividend",JLabel.LEFT);
JTextField PriceBid = new JTextField(5);
JTextField PriceAsk = new JTextField(5);
JTextField PriceMid = new JTextField(5);
JTextField Dividend = new JTextField(5);
JTextField NewUnderlyingCode = new JTextField(10);
String NewCode;
public void CreateStockDetails(String StockCode)
{
super("Hallo All");
Container contentPane = getContentPane();
setSize(400,500);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//Centre Screen To Right Of Main
Dimension sd=Toolkit.getDefaultToolkit().getScreenSize();
super.setLocation(sd.width/2-100/2, sd.height/2-300/2);
Thanks
Kind Regards
Stephen

If it is a JFrame, Window, JInternalFrame, ect, just call setTitle("Hallo"); Also, the call to super must be the first call in your constructor.

You need to set the title to the JFrame and not the ContentPane

A container doesn't have a setTitle() method. Please read the API.
In addition to setting the title of the JFrame, you can add a "titled" border around the content pane:
JPanel content = (JPanel)getContentPane();
content.setBorder( ... );
Read the section from the Swing tutorial on How to Use Borders for an example of using a TitledBorder.

Related

Make JTextArea scrollable in Swing

How can I make it so my JTextArea is scrollable. I want to make it so that keysDisplay has a scrollbar which can be used to scroll through the text area. I have skipped some unrealated code in jFramePrint().
public class ApplicationViewer extends JFrame {
private JTabbedPane tabs = new JTabbedPane();
private JTextArea keyGenDisplay = new JTextArea();
private JTextArea keysDisplay = new JTextArea();
private JPanel controlPanel = new JPanel();
private JButton addNumber = new JButton("Add Number");
private JButton addLetter = new JButton("Add Letter");
private JButton addHyphen = new JButton("Add Hyphen");
private JButton calculateButton = new JButton("Calculate Key");
private JTextField amountField = new JTextField("", 6);
private JLabel amountLabel = new JLabel(" Amount of Keys : ");
private JScrollPane scroll = new JScrollPane(keysDisplay);
public void jFramePrint() {
this.setLayout(new BorderLayout());
this.add(controlPanel, BorderLayout.SOUTH);
controlPanel.add(addNumber);
controlPanel.add(addLetter);
controlPanel.add(addHyphen);
controlPanel.add(amountLabel);
controlPanel.add(amountField);
controlPanel.add(calculateButton);
this.add(scroll);
this.setSize(1400, 900);
this.setTitle("Key Generator");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
keyGenDisplay.append("Key Format: ");
keysDisplay.append("Keys Here: ");
tabs.add("Key Generator", keyGenDisplay);
tabs.add("Keys", keysDisplay);
this.add(tabs);
this.setVisible(true);
}
}
private JTextArea keysDisplay = new JTextArea();
First of all you should use something like:
private JTextArea keysDisplay = new JTextArea(5, 20);
This will allow the text area to calculate its own preferred size. Scrollbars will then work properly when added to a scrollpane and more than 5 rows of text are added to the text area.
this.add(scroll);
...
this.add(tabs);
Your frame is using a BorderLayout. When you don't use a constraint then "CENTER is used by default.
You can't add multile components to the same area of the BorderLayout. So only the last component added is displayed.
Specify a different constraint for the tabs component.

Multiple JPanel inside another

look here
I have to make a format like this, like make a 1,2 panel, then put a 3,1 panel on the left side. Then, I put 4 text fields in the middle left panel and one button in the bottom left panel.
I'm not sure if I can just use GridLayout or if I have to use BorderLayout too.
How can I get this organized because when I set it up, I can't get the layout right. The textfields are not in the right position, they end up on the right side.
public class CreatePanel extends JPanel
{
private Vector projectList;
private JButton button1;
private ProjectSpendingPanel spendingPanel;
private JFrame frame1;
JPanel panel1;
JPanel leftPanel;
JPanel subPanel;
GridLayout layout1;
BorderLayout layout2;
GridLayout layout3;
JLabel message;
JLabel labelName;
JLabel labelNumber;
JLabel labelLocation;
JLabel labelFunding;
JTextField textField1;
JTextField textField2;
JTextField textField3;
JTextField textField4;
//Constructor initializes components and organize them using certain layouts
public CreatePanel(Vector projectList, ProjectSpendingPanel spendingPanel)
{
this.projectList = projectList;
this.spendingPanel = spendingPanel;
//organize components here
layout1 = new GridLayout(1,2);
layout2 = new BorderLayout(1,3);
layout3 = new GridLayout(4,2);
this.setLayout(layout1);
panel1 = new JPanel(layout1);
leftPanel = new JPanel(layout3);
subPanel = new JPanel(layout2);
add(panel1);
panel1.add(subPanel);
panel1.add(leftPanel);
labelName = new JLabel("Project Name");
leftPanel.add(labelName);
textField1 = new JTextField("", 15);
leftPanel.add(textField1,BorderLayout.SOUTH);
labelName = new JLabel("Project Number");
leftPanel.add(labelName);
textField2 = new JTextField("",15);
leftPanel.add(textField2);
labelLocation = new JLabel("Project Location");
leftPanel.add(labelLocation);
textField3 = new JTextField("",15);
leftPanel.add(textField3);
labelFunding = new JLabel("Initial Funding");
leftPanel.add(labelFunding);
textField4 = new JTextField("",15);
leftPanel.add(textField4);
add(leftPanel);
button1 = new JButton("Create a project");
subPanel.add(button1,BorderLayout.SOUTH);
}
To get the result you probably intended, you need to tweak your code just a little.
Your lines
panel1 = new JPanel(layout1);
leftPanel = new JPanel(layout3);
subPanel = new JPanel(layout2);
should be altered to read
panel1 = new JPanel(new GridLayout(2, 1));
leftPanel = new JPanel(layout3);
subPanel = new JPanel(layout2);
GridLayout takes rows first, then columns. Your layout1 defines one row and two columns but you use it for the text fields and the button, so I assume the two panels need to be above each other.
Second, you need to change the order from
add(panel1);
panel1.add(subPanel);
panel1.add(leftPanel);
to
add(panel1);
panel1.add(leftPanel);
panel1.add(subPanel);
Finally, I think you need to change
add(leftPanel);
button1 = new JButton("Create a project");
to
add(spendingPanel);
button1 = new JButton("Create a project");
Adding leftPanelagain seems to destroy the layout. If you do not want to see your spendingPanel here, you could use new JPanel() instead.
Here is a screenshot of the running program

Java scrollbar not visible

I am new to java. I am trying to implement scroll feature to my Jpanel but the schoolpane is not visible. I tried to many methods but nothing works.
public class test extends ContentPanel {
JPanel secondary;
JPanel customerType;
JPanel primary;
JPanel labelPanel;
JLabel lCustNo;
JLabel lCustName;
JLabel lTelNo;
JLabel lAddress;
JLabel lNationality;
JLabel lResident;
JLabel lVisitor;
JLabel lCustomerType;
JLabel lIdCard;
JLabel lBankName;
JLabel lPassportNo;
JLabel lVisitStart;
JLabel lVisitEnd;
JTextField tCustNo;
JTextField tCustName;
JTextField tTelNo;
JTextField tAddress;
JTextField tNationality;
JTextField tIdCard;
JTextField tBankName;
JTextField tPassportNo;
JTextField tVisitStart;
JTextField tVisitEnd;
JPanel radioButton;
JRadioButton rResident;
JRadioButton rVisitor;
ButtonGroup custType;
JScrollPane scrollBar;
JSeparator s1;
public test (String title, JPanel parent) {
super(title, parent);
secondary = new JPanel(new GridLayout(2,0));
secondary.setBounds(10, 10, 500, 800);
labelPanel = new JPanel(new GridLayout(5,2,300,20));
radioButton = new JPanel(new GridLayout(1, 4));
customerType = new JPanel(new GridLayout(3,4, 30, 20));
primary = new JPanel(new BorderLayout());
lCustNo = new JLabel("Customer No: ");
lCustName = new JLabel("Name: ");
lTelNo = new JLabel("Telephone Number: ");
lAddress = new JLabel("Address: ");
lNationality = new JLabel("Nationality: ");
lResident = new JLabel("Resident");
lVisitor = new JLabel("Visitor");
lCustomerType = new JLabel("Customer Type");
lIdCard = new JLabel("Id Card");
lBankName = new JLabel("Bank Name");
lPassportNo = new JLabel("Passport Number");
lVisitStart = new JLabel("Visit Start");
lVisitEnd = new JLabel("Visit End");
tCustNo = new JTextField(10);
tCustName = new JTextField(10);
tTelNo = new JTextField(10);
tAddress = new JTextField(10);
tNationality = new JTextField(10);
tIdCard = new JTextField(10);
tBankName = new JTextField(10);
tPassportNo = new JTextField(10);
tVisitStart = new JTextField(10);
tVisitEnd = new JTextField(10);
rResident = new JRadioButton();
rVisitor = new JRadioButton();
custType = new ButtonGroup();
customerType.add(lPassportNo);
customerType.add(tPassportNo);
customerType.add(lVisitStart);
customerType.add(tVisitStart);
customerType.add(lIdCard);
customerType.add(tIdCard);
customerType.add(lBankName);
customerType.add(tBankName);
customerType.add(lVisitEnd);
customerType.add(tVisitEnd);
custType.add(rResident);
custType.add(rVisitor);
radioButton.add(lVisitor);
radioButton.add(rVisitor);
radioButton.add(lResident);
radioButton.add(rResident);
labelPanel.add(lCustNo);
labelPanel.add(tCustNo);
labelPanel.add(lCustName);
labelPanel.add(tCustName);
labelPanel.add(lTelNo);
labelPanel.add(tTelNo);
labelPanel.add(lAddress);
labelPanel.add(tAddress);
labelPanel.add(lNationality);
labelPanel.add(tNationality);
secondary.add(customerType);
secondary.add(labelPanel);;
primary.add(secondary,BorderLayout.CENTER);
scrollBar= new JScrollPane(primary);
scrollBar.setBounds(10, 10, 400, 555);
this.add(scrollBar);
}
}
My code is above. This class extends contentPanel and contentPanel extends JPanel. Someone please help me.
Thanks.
Your scrollBar is not visible because it is not necessary. If you change JScrollPane settings, for example:
scrollBar.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollBar.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
you will see you have it, but not use it. It is because your scrollBar doesn't have a content which expands dynamically (like JTextArea for example). Your scrollBar has JPanel inside, which has size to fit all its components, so it will not be enough to add some text inside text components.
To make scroll bars visible, you can change a size of JSrollPane. However setBound() will not work if you JPanel use default FlowLayout. To use absolute positioning in JPanel you need to use null layout, what is generally not recommended and is seen as bad programming practice.
Try to delete setBound() lines and use for example scrollBar.setPreferredSize(new Dimension(400,555)); instead. You should see a difference.

JFrame Window not showing any content unless resized

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

My GUI window doesn't show anything

I'm trying to use a grid layout to make a GUI window. I add all my components and it compiles but when it runs it doesn't show anything. I'm trying to make a simple layout grouped and stacked like this.
{introduction message}
{time label
time input text}
{gravity label
gravity input text}
{answer label
answer text box}
{calculate button clear button}
Here is my code
import javax.swing.*;
import java.awt.*;
public class TurnerRandyFallingGUI extends JFrame
{
final int WINDOW_HEIGHT=500;
final int WINDOW_WIDTH=500;
public TurnerRandyFallingGUI()
{
setTitle("Falling Distance Calculator");
setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(1, 5));
//labels
JLabel introMessage = new JLabel("Welcome to the Falling distance"+
"calculator");
JLabel timeLabel = new JLabel("Please enter the amount of time "+
"in seconds the object was falling.");
JLabel gravityLabel = new JLabel("Enter the amount of gravity being "+
"forced onto the object");
JLabel answerLabel = new JLabel("Answer");
//text fields
JTextField fTime = new JTextField(10);
JTextField gForce = new JTextField(10);
JTextField answerT = new JTextField(10);
//buttons
JButton calculate = new JButton("Calculate");
JButton clr = new JButton("clear");
//panels
JPanel introP = new JPanel();
JPanel timeP = new JPanel();
JPanel gravityP = new JPanel();
JPanel answerP = new JPanel();
JPanel buttonsP = new JPanel();
//adding to the panels
//intro panel
introP.add(introMessage);
//time panel
timeP.add(timeLabel);
timeP.add(fTime);
//gravity panel
gravityP.add(gravityLabel);
gravityP.add(gForce);
//answer panel
answerP.add(answerLabel);
answerP.add(answerT);
//button panel
buttonsP.add(calculate);
buttonsP.add(clr);
setVisible(true);
}
public static void main(String[] args)
{
new TurnerRandyFallingGUI();
}
}
You've added nothing to the JFrame that your class above extends. You need to add your components to containers whose hierarchy eventually leads to the top level window, to the this if you will. In other words, you have no add(someComponent) or the functionally similar this.add(someComponent)method call in your code above.
Consider adding all of your JPanels to a single JPanel
Consider adding that JPanel to the JFrame instance that is your class by calling add(thatJPanel).
Even better would be to not extend JFrame and just to create one when needed, but that will likely be the subject of another discussion at another time.
Before setVisible (true) statement add following statements:
add (introP);
add (timeP);
add (gravityP);
add (answerP);
add (buttonsP);
There is nothing in your JFrame. That is the reason
import javax.swing.*;
import java.awt.*;
public class TurnerRandyFallingGUI extends JFrame
{
final int WINDOW_HEIGHT=500;
final int WINDOW_WIDTH=500;
public TurnerRandyFallingGUI()
{
//labels
JLabel introMessage = new JLabel("Welcome to the Falling distance"+
"calculator");
JLabel timeLabel = new JLabel("Please enter the amount of time "+
"in seconds the object was falling.");
JLabel gravityLabel = new JLabel("Enter the amount of gravity being "+
"forced onto the object");
JLabel answerLabel = new JLabel("Answer");
//text fields
JTextField fTime = new JTextField(10);
JTextField gForce = new JTextField(10);
JTextField answerT = new JTextField(10);
//buttons
JButton calculate = new JButton("Calculate");
JButton clr = new JButton("clear");
//panels
JPanel introP = new JPanel();
JPanel timeP = new JPanel();
JPanel gravityP = new JPanel();
JPanel answerP = new JPanel();
JPanel buttonsP = new JPanel();
//adding to the panels
//intro panel
introP.add(introMessage);
//time panel
timeP.add(timeLabel);
timeP.add(fTime);
//gravity panel
gravityP.add(gravityLabel);
gravityP.add(gForce);
//answer panel
answerP.add(answerLabel);
answerP.add(answerT);
//button panel
buttonsP.add(calculate);
buttonsP.add(clr);
setLayout(new GridLayout(5, 1));
this.add(introP);
this.add(timeP);
this.add(gravityP);
this.add(answerP);
this.add(buttonsP);
setTitle("Falling Distance Calculator");
this.pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
this.validate();
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TurnerRandyFallingGUI();
}
});
}
}
Consider the following
In GridLayout, the first parameter is Rows, Second is columns
Never set the size of JFrame manually. Use pack() method to decide
the size
Use SwingUtilities.InvokeLater() to run the GUI in another thread.

Categories