Reputation: 27
My aim here is to be able to create multiple JTextFields, using my ClockBuilder class. I wonder if you could give some advice on how I might achieve this goal. I've included an SSCCE of my code.
My full program creates an array of 145 cities from a txt file. I use this array to fill a jlist, then +/- buttons should allow the user to add/remove cities from being displayed. I'm at the stage where I have the JList and buttons functioning correctly. It adds/removes cities into a separate array list. I've been trying to use this array list to create multiple instances of my JTextField but, I am not able to achieve this, where am I going wrong?
I've simplified this in my SSCCE where I am manually creating two Clockbuilder classes, why is it that New York displays correctly but London doesn't display at all? I'm quite new to programming and am still getting my head around classes, methods and constructors, please excuse me if I have phrased parts of my question incorrectly I hope I have been clear in what I wish to achieve.
ClockBuilder.java
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class ClockBuilder extends JTextField{
ClockView view;
public ClockBuilder(){
super(8);
}
public JTextField ClockBuilder(String city){
Border lineBorder = BorderFactory.createLineBorder(Color.BLACK,1);
Font font = new Font("Bradley Hand ITC", Font.BOLD, 24);
setFont(font);
setEditable(false);
setFocusable(false);
setHorizontalAlignment(JTextField.CENTER);
setBorder(BorderFactory.createTitledBorder(lineBorder, city));
return this;
}
}
View.java
import java.io.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class ClockView{
JFrame clockFrame;
JPanel displayArea;
JPanel clockPanel;
String cities[] = {"London","New York","Barcelona", "Toronto"};
public ClockView() {
clockFrame = new JFrame("World Clock");
clockFrame.setSize(400,400);
clockFrame.setLocationRelativeTo(null);
clockFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = clockFrame.getContentPane();
clockPanel = new JPanel();
clockPanel.setPreferredSize(new Dimension(400, 400));
ClockBuilder clock = new ClockBuilder();
clockPanel.add(clock.ClockBuilder("London"));
ClockBuilder clock2 = new CLockBuilder();
clockPanel.add(clock2.ClockBuilder("New York"));
contentPane.add(clockPanel);
clockFrame.setVisible(true);
}
}
Upvotes: 1
Views: 155
Reputation: 285405
As noted in my comments, you're mis-using inheritance in that the ClockView class should not extend ClockBuilder. ClockView will hold ClockBuilder instances and thus there is a "has-a" relationship here, not an "is-a" relationship. Also and again, your ClockBuilder class has a "pseudo-constructor", a method with the same name as the class, including capitalization, but which is not truly a constructor since it returns something. Get rid of it. Either use an appropriate method with an appropriate method name or a true constructor.
Also, you're using JTextFields in a very non-JTextField way, where you seem to display the text but not let the user edit it. In this situation, better to use JLabels placed into a GridLayout-using JPanel. Better still, if all you want to do is display a list of clock times, then I would use a JList, one with a custom renderer that displays the appropriate time of the clock with a surrounding title border.
Upvotes: 1
Reputation: 324147
why is it that New York displays correctly but London doesn't display at all?
By default a JFrame uses a BorderLayout
. Also by default when you add a component to the frame it will be added to the "CENTER" of the BorderLayout. However, only one component can be displayed in the center, so only the last component added (New York) is displayed.
I don't really understand what you are attempting to do so I can't suggest a complete solution, but the basic solution is to use a different layout manager to manage the components. Read the section from the Swing tutorial on Layout Managers for more information.
As a simple example to get you started add the following to see the difference:
clockFrame = new JFrame("World Clock");
clockFrame.setLayout( new FlowLayout() );
Upvotes: 3