I'm using this java console example https://commons.apache.org/proper/commons-net/examples/telnet/WeatherTelnet.java for working with telnet server. I need frame with input text field for commands(jTextField) and area with commands and server's responce (JTextArea). I has Swing UI, but cant integrate WeatherTelnet.java with it.
IOUtil.readWrite(telnet.getInputStream(), telnet.getOutputStream(), System.in, System.out);
I think I should redirect System.out to JTextArea and jTextField to System.in. Could you help me with it, please.
Swing UI
public class ClientLauncher {
String appName = "Simple Telnet Client";
JFrame frame = new JFrame(appName);
JTextField textField;
JTextArea textArea;
JFrame preFrame;
public void display() {
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
JPanel southPanel = new JPanel();
southPanel.setLayout(new GridBagLayout());
textField = new JTextField(30);
textField.requestFocusInWindow();
textField.addActionListener(new TextFieldListener());
textArea = new JTextArea();
textArea.setEditable(false);
textArea.setFont(new Font("Serif", Font.PLAIN, 15));
textArea.setLineWrap(true);
//MessageConsole mc = new MessageConsole(textComponent);
mainPanel.add(new JScrollPane(textArea), BorderLayout.CENTER);
GridBagConstraints left = new GridBagConstraints();
left.anchor = GridBagConstraints.LINE_START;
left.fill = GridBagConstraints.HORIZONTAL;
left.weightx = 512.0D;
left.weighty = 1.0D;
GridBagConstraints right = new GridBagConstraints();
right.insets = new Insets(0, 10, 0, 0);
right.anchor = GridBagConstraints.LINE_END;
right.fill = GridBagConstraints.NONE;
right.weightx = 1.0D;
right.weighty = 1.0D;
southPanel.add(textField, left);
mainPanel.add(BorderLayout.SOUTH, southPanel);
frame.add(mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(470, 300);
frame.setVisible(true);
}
class TextFieldListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (textField.getText().length() > 1) {
textArea.append(textField.getText() + "\n");
textField.setText("");
textField.requestFocusInWindow();
}
}
}
public static void main(String[] args) {
ClientLauncher clientView = new ClientLauncher();
clientView.display();
}
}
Related
I am making a simple project. It has login window like this
When the user click on button log in - it should "repaint" the window(it should seem to be happened in the same window) and then the window looks like this.
The problem is - I can't "repaint" the window - the only thing I can - it's create a new frame, so there actually are 2 frames totally.
How to make the whole thing in one same frame.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.Border;
public class Client
{
private JFrame frame;
private JTextArea allMessagesArea;
private JTextArea inputArea;
private JButton buttonSend;
private JButton buttonExit;
private String login;
public void addComponentsToPane(Container pane)
{
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(10,10,10,10);
c.fill = GridBagConstraints.HORIZONTAL;
allMessagesArea = new JTextArea(25,50);
c.weighty = 0.6;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx=0;
c.gridy=0;
c.gridwidth=2;
pane.add(allMessagesArea, c);
inputArea = new JTextArea(12,50);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridwidth=2;
c.weighty =0.3;
c.gridx =0;
c.gridy =1;
pane.add(inputArea, c);
buttonSend = new JButton("Send");
c.weightx=0.5;
c.weighty = 0.1;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx =0;
c.gridy=2;
c.gridwidth =1;
pane.add(buttonSend, c);
buttonExit = new JButton("Exit");
c.weightx =0.5;
c.weighty = 0.1;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx =1;
c.gridy=2;
c.gridwidth =1;
pane.add(buttonExit, c);
}
public Client()
{
frame = new JFrame("Simple Client");
frame.setSize(400,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
welcomePage();
frame.setVisible(true);
}
public void welcomePage()
{
JPanel panel = new JPanel();
JLabel label = new JLabel("Your login:");
panel.add(label);
JTextField textField = new JTextField(15);
panel.add(textField);
JButton loginButton = new JButton("log in");
panel.add(loginButton);
JButton exitButton = new JButton("exit");
panel.add(exitButton);
frame.add(panel, BorderLayout.CENTER);
loginButton.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
if(textField.getText().isEmpty())
JOptionPane.showMessageDialog(frame.getContentPane(), "Please enter your login");
else
{
login = textField.getText();
System.out.println(login);
frame = null;
frame = new JFrame("Simple Client");
frame.setSize(400,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
}
});
exitButton.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args)
{
Client frame = new Client();
}
}
Use CardLayout.
This layout allows developers to switch between panels. It works by creating a "deck" panel that'll contain all of panels that'll potentially be displayed:
CardLayout layout = new CardLayout();
JPanel deck = new JPanel();
deck.setLayout(layout);
JPanel firstCard = new JPanel();
JPanel secondCard = new JPanel();
deck.add(firstCard, "first");
deck.add(secondCard, "second");
When you click on a button, that button's ActionListener should call show(Container, String), next(Container) or previous(Container) on the CardLayout to switch which panel is being displayed:
public void actionPerformed(ActionEvent e) {
layout.show(deck, "second");
}
One of the solutions
You can create two panels (one for each view) and add the required components to them. First, you add first panel to the frame (using frame.add(panel1)). If you want to show the second panel in the same window, you can delete first panel (using frame.remove(panel1)) and add the second panel (using frame.add(panel2)). At the end you've to call frame.pack().
This's your code with above solution:
public class Client
{
private JFrame frame;
private JTextArea allMessagesArea;
private JTextArea inputArea;
private JButton buttonSend;
private JButton buttonExit;
private String login;
public void addComponentsToPanel2()
{
panel2.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(10,10,10,10);
c.fill = GridBagConstraints.HORIZONTAL;
allMessagesArea = new JTextArea(25,50);
c.weighty = 0.6;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx=0;
c.gridy=0;
c.gridwidth=2;
panel2.add(allMessagesArea, c);
inputArea = new JTextArea(12,50);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridwidth=2;
c.weighty =0.3;
c.gridx =0;
c.gridy =1;
panel2.add(inputArea, c);
buttonSend = new JButton("Send");
c.weightx=0.5;
c.weighty = 0.1;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx =0;
c.gridy=2;
c.gridwidth =1;
panel2.add(buttonSend, c);
buttonExit = new JButton("Exit");
c.weightx =0.5;
c.weighty = 0.1;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx =1;
c.gridy=2;
c.gridwidth =1;
panel2.add(buttonExit, c);
}
public Client()
{
frame = new JFrame("Simple Client");
frame.setSize(400,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
welcomePage();
frame.setVisible(true);
}
public void welcomePage()
{
panel1 = new JPanel();
JLabel label = new JLabel("Your login:");
panel1.add(label);
JTextField textField = new JTextField(15);
panel1.add(textField);
JButton loginButton = new JButton("log in");
panel1.add(loginButton);
JButton exitButton = new JButton("exit");
panel1.add(exitButton);
frame.add(panel1, BorderLayout.CENTER);
loginButton.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
if(textField.getText().isEmpty())
JOptionPane.showMessageDialog(frame.getContentPane(), "Please enter your login");
else
{
login = textField.getText();
System.out.println(login);
panel2 = new JPanel();
addComponentsToPanel2();
frame.remove(panel1);
frame.add(panel2);
//frame.repaint();
frame.pack();
}
}
});
exitButton.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args)
{
Client frame = new Client();
}
private JPanel panel1;
private JPanel panel2;
}
I am trying to build a sample application using GridLayout in Java. But I am unable to re-size my buttons. Please help me in doing it.
There are four grid layouts in the code.
I have used the setSize(width, height) function and also the setPreferredSize function. But I am not able to set the size of the buttons.
Here is the code
import java.awt.*;
import javax.swing.*;
public class Details2 {
static JButton btn1,btn2;
public static void main(String[] a) {
JFrame myFrame = new JFrame("Medical History Form");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(700,700);
Container myPane = myFrame.getContentPane();
myPane.setLayout(new GridLayout(2,1));
myPane.add(getFieldPanel());
myPane.add(getButtonPanel());
myPane.setPreferredSize(new Dimension(100,100));
myFrame.setVisible(true);
}
private static JPanel getFieldPanel() {
JPanel p = new JPanel(new GridLayout(4,2));
p.setBorder(BorderFactory.createTitledBorder("Details"));
p.add(new JLabel("Please check in the here"));
p.add(new JCheckBox("Nothing till now",false));
p.add(getPanel());
return p;
}
private static JPanel getButtonPanel() {
GridLayout g =new GridLayout(1,2);
JPanel p = new JPanel(g);
btn1 = new JButton("Submit");
btn2 = new JButton("Reset");
p.add(btn1).setPreferredSize(new Dimension(100,100));
p.add(btn2).setPreferredSize(new Dimension(100,100));
p.setPreferredSize(new Dimension(100,100));
return p;
}
private static JPanel getPanel() {
JPanel p = new JPanel(new GridLayout(5,2));
p.add(new JCheckBox("A",false));
p.add(new JCheckBox("B",false));
p.add(new JCheckBox("C",false));
p.add(new JCheckBox("D",false));
p.add(new JCheckBox("E",false));
p.add(new JCheckBox("F",false));
p.add(new JCheckBox("G",false));
p.add(new JCheckBox("E",false));
p.add(new JCheckBox("H",false));
p.add(new JCheckBox("I",false));
return p;
}
}
I believe setting GridLayout(2,2) will override size changes made to the panels. To be more precise, use GridBagConStraints; you can refer following code to understand it.
private JTextField field1 = new JTextField();
private JButton addBtn = new JButton("Save: ");
public void addComponents(Container pane) {
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
// Components
c.gridwidth = 1;
c.weightx = .01;
c.weighty = .2;
c.gridx = 0;
c.gridy = 1;
pane.add(field1, c);
c.gridwidth = 1;
c.weightx = .01;
c.weighty = .2;
c.gridx = 0;
c.gridy = 1;
pane.add(addBtn, c);
}
public MainView() {
//Create and set up the window.
JFrame frame = new JFrame("NAME");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
addComponents(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
frame.setResizable(false);
frame.setSize(400, 125);
frame.setLocation(400, 300);
}
if you want to control the size of your components then don't use a layout. do something like this:
private static JPanel getButtonPanel() {
JPanel p = new JPanel();
p.setLayout(null);
btn1 = new JButton("Submit");
btn2 = new JButton("Reset");
btn1.setBounds(x, y, width, height);
btn2.setBounds(x, y, width, height);
p.add(btn1);
p.add(btn2);
return p;
Basically, this is the structure of my Window.
The actual window looks like this:
My problem is that I want the MainPanel with the ComboBox and and the rest to fit the JFrame window. Stretch horizontally and stick to the top. But I've tried all kinds of layouts (Box, GridBag, Grid). It just remains centered. How do I achieve this?
Here is the MVCC:
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;
public class simplelayout{
JFrame mainFrame;
JPanel mainPanel;
JPanel innerPanel;
JPanel createDomainPanel;
JPanel listDomainPanel = new JPanel(new GridBagLayout());
String comboBoxItems[]={"Create Domain","List Domains"};
#SuppressWarnings("rawtypes")
JComboBox listCombo;
JButton goBtn;
CardLayout layout;
JTextField domainName = new JTextField();
JLabel successMessage = new JLabel("");
public simplelayout(){
initialize();
setCardLayout();
}
#SuppressWarnings({ "rawtypes", "unchecked" })
private void setCardLayout() {
innerPanel = new JPanel();
//innerPanel.setSize(600, 400);
layout = new CardLayout(10,10);
innerPanel.setLayout(layout);
//Default Panel
JPanel defaultPanel = new JPanel(new FlowLayout());
defaultPanel.add(new JLabel("Welcome to Amazon Simple DB"));
//Create Domain Panel
createDomainPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
//createDomainPanel.setSize(600, 300);
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.gridx=0;
gbc.gridy=0;
createDomainPanel.add(new JLabel("Enter the name of the domain"), gbc);
gbc.gridx=0;
gbc.gridy=1;
createDomainPanel.add(domainName, gbc);
JPanel result = new JPanel(new FlowLayout());
result.add(successMessage);
gbc.anchor=GridBagConstraints.LAST_LINE_START;
gbc.gridx=0;
gbc.gridy=2;
createDomainPanel.add(result, gbc);
//List Domain Panel
listDomainPanel.add(new JLabel("List of Domains"), gbc);
//Add to innerPanel
innerPanel.add(defaultPanel, "Default");
innerPanel.add(createDomainPanel, "Create Domain");
innerPanel.add(listDomainPanel, "List Domain");
layout.show(innerPanel, "Default");
DefaultComboBoxModel panelName = new DefaultComboBoxModel();
panelName.addElement("Create Domain");
panelName.addElement("List Domain");
listCombo = new JComboBox(panelName);
listCombo.setSelectedIndex(0);
JScrollPane listComboScroll = new JScrollPane(listCombo);
goBtn = new JButton("Go");
mainPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc1 = new GridBagConstraints();
gbc1.fill=GridBagConstraints.HORIZONTAL;
gbc1.anchor=GridBagConstraints.FIRST_LINE_START;
gbc1.gridx = 0;
gbc1.gridx = 0;
mainPanel.add(listComboScroll, gbc1);
gbc1.gridy = 1;
mainPanel.add(goBtn, gbc1);
gbc1.gridy = 2;
mainPanel.add(innerPanel, gbc1);
GridBagConstraints gbc2 = new GridBagConstraints();
gbc2.fill=GridBagConstraints.HORIZONTAL;
gbc2.anchor=GridBagConstraints.FIRST_LINE_START;
JScrollPane scr = new JScrollPane(mainPanel);
mainPanel.setSize(mainFrame.getPreferredSize());
mainFrame.add(scr, gbc);
mainFrame.setVisible(true);
mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
private void initialize() {
mainFrame = new JFrame("Amazon Simple DB");
mainFrame.setSize(700, 500);
mainFrame.setLayout(new GridBagLayout());
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPanel = new JPanel();
//mainPanel.setLayout(new GridLayout(8,1));
mainFrame.setVisible(true);
}
public static void main(String[] args) {
try {
//UIManager.put("nimbusBase", new Color(178,56,249));
//UIManager.put("nimbusBlueGrey", new Color(102,212,199));
//UIManager.put("control", new Color(212,133,102));
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
// If Nimbus is not available, you can set the GUI to another look and feel.
}
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new simplelayout();
}
});
}
}
It sounds like you want to use the application frame's default BorderLayout, placing the combobox & button components in a nested panel in the BorderLayout.PAGE_START location with the innerPanel in the CENTER position.
Try to add a mainFrame.pack(); in the initialize() and the setCardLayout()methods, Remove all mainFrame.setSize() and mainPanel.setSize() methods.
Firstly, I have only just started coding in Java this evening so apologizes if I seem like a total newb and that my code is atrocious, Im very much a novice. Basically I want an image to change colour when a certain number is entered into a text field on a separate j frame. so the car appears blue when loaded and if they press 1 the car moves and changes color. My issues is I cant seem to access the text variable to manipulate it. Any help would be greatly appreciated, thank you x
public class ImageBackground extends JFrame
{
public static void main(String[] args) throws Exception
{
JFrame F = new JFrame("CrossRoads Simulation");
TextDemo textDemo = new TextDemo();
try
{
F.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("C:/Users/Stacy/workspace/Test/crossRoadsBackground.png")))));
}
catch(IOException e)
{
System.out.println("Image doesnt exist");
}
F.setResizable(false);
F.pack();
F.setVisible(true);
JButton button = new JButton(new ImageIcon("C:/Users/Stacy/workspace /Test/CarBlueOne.png"));
button.setSize(new Dimension(50, 50));
button.setLocation(130, 210);
button.setOpaque(false);
button.setContentAreaFilled(false);
button.setBorderPainted(false);
F.add(button);
textDemo.createAndShowGUI();
//Make sure the new text is visible, even if there
//was a selection in the text area.
textDemo.textArea.setCaretPosition(textDemo.textArea.getDocument().getLength( ));
}
}
TextDemo.java
public class TextDemo extends JPanel implements ActionListener {
public JTextField textField;
public JTextArea textArea;
public final static String newline = "\n";
JFrame F = new JFrame("CrossRoads Simulation");
ImageBackground imageBackground = new ImageBackground();
public TextDemo() {
super(new GridBagLayout());
textField = new JTextField(20);
textField.addActionListener(this);
textArea = new JTextArea(5, 20);
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
//Add Components to this panel.
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
add(textField, c);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
add(scrollPane, c);
}
protected static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("TextDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add contents to the window.
frame.add(new TextDemo());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent evt) {
String text = textField.getText();
int input = Integer.parseInt(text);
if (input == 1)
{
JButton button = new JButton(new ImageIcon("C:/Users/Stacy/workspace /Test/CarRedOne.png"));
button.setSize(new Dimension(50, 50));
button.setLocation(170, 210);
}
textArea.append(text + newline);
textField.selectAll();
}
}
You do not have any code to expose the text variable in TextDemo to other classes. You should read/modify the text from the TextField variable. It should look similar to this:
public String getText() {
return textField.getText();
}
public void setText(String text) {
textField.setText(text);
}
I am facing an alignment issue in Java Swing screen. I have borderLayout here. My requirement is based on different buttons click suitable query will be executed and populated in JTable. But this screen is having an alignment problem, and JTable is hiding all other components. Below is the code:
public class UI {
static JTextField inputText = new JTextField(20);
static JLabel errMsgLbl = new JLabel();
static PreparedStatement ptsmt;
static ResultSet rs;
static JButton nameButton;
static JButton accnoButton;
static JButton countryButton;
static JButton altaccButton;
static JButton showAllButton;
static JButton clearButton;
static JButton exitButton;
static JButton prevButton;
static JButton nextButton;
static JLabel searchBy;
static String[] columnNames = { "ID", "Name", "Original Name",
"Account Number", "Country", "Alternate Account No",
"Related Party Name", "Select" };
static JTable table;
static JComponent createHorizontalSeparator() {
JSeparator x = new JSeparator(SwingConstants.HORIZONTAL);
x.setForeground(Color.BLACK);
x.setPreferredSize(new Dimension(50, 3));
return x;
}
public static void main(String args[]) {
try {
DBConfig.loadEngineConfiguration();
} catch (Exception ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
final JTable table = new JTable(50, 8);
JPanel topPnl = new JPanel(new BorderLayout());
JPanel cntrPnl = new JPanel(new BorderLayout());
JPanel bottomPnl = new JPanel(new BorderLayout());
JPanel hdrPnl = new JPanel((LayoutManager) new FlowLayout(
FlowLayout.LEFT));
JPanel srchPnl = new JPanel((LayoutManager) new FlowLayout(
FlowLayout.LEADING));
JPanel topBtnPnl = new JPanel((LayoutManager) new FlowLayout(
FlowLayout.LEADING));
JPanel bottombtnPnl = new JPanel((LayoutManager) new FlowLayout(
FlowLayout.LEADING));
JPanel navbtnPnl = new JPanel((LayoutManager) new FlowLayout(
FlowLayout.TRAILING));
JPanel tblPnl = new JPanel((LayoutManager) new FlowLayout(
FlowLayout.CENTER));
hdrPnl.add(new JLabel("Welcome"));
srchPnl.add(new JLabel("Input text"));
srchPnl.add(inputText);
topPnl.add(hdrPnl, BorderLayout.NORTH);
topPnl.add(srchPnl, BorderLayout.SOUTH);
searchBy = new JLabel(DBConfig.searchBy);
nameButton = new JButton(DBConfig.nameBtn);
accnoButton = new JButton(DBConfig.accNoBtn);
countryButton = new JButton(DBConfig.countryBtn);
altaccButton = new JButton(DBConfig.altAccNoBtn);
showAllButton = new JButton(DBConfig.showBtn);
clearButton = new JButton(DBConfig.clearBtn);
exitButton = new JButton(DBConfig.exitBtn);
prevButton = new JButton(DBConfig.prevBtn);
nextButton = new JButton(DBConfig.nextBtn);
clearButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
inputText.setText(null);
errMsgLbl.setText(null);
}
});
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Container frame = exitButton.getParent();
do
frame = frame.getParent();
while (!(frame instanceof JFrame));
((JFrame) frame).dispose();
}
});
topBtnPnl.add(searchBy);
topBtnPnl.add(nameButton);
topBtnPnl.add(accnoButton);
topBtnPnl.add(countryButton);
topBtnPnl.add(altaccButton);
bottombtnPnl.add(showAllButton);
bottombtnPnl.add(clearButton);
bottombtnPnl.add(exitButton);
//cntrPnl.add(topBtnPnl, BorderLayout.NORTH);
//cntrPnl.add(bottombtnPnl, BorderLayout.SOUTH);
cntrPnl.add(topBtnPnl, BorderLayout.NORTH);
cntrPnl.add(bottombtnPnl, BorderLayout.CENTER);
cntrPnl.add(createHorizontalSeparator(), BorderLayout.SOUTH);
navbtnPnl.add(prevButton);
navbtnPnl.add(nextButton);
table.getTableHeader().setReorderingAllowed(false);
tblPnl.add(table, BorderLayout.PAGE_END);
bottomPnl.add(navbtnPnl, BorderLayout.CENTER);
bottomPnl.add(tblPnl, BorderLayout.CENTER);
//table.setTableHeader(columnNames);
frame.add(topPnl, BorderLayout.NORTH);
frame.add(cntrPnl, BorderLayout.CENTER);
frame.add(bottomPnl, BorderLayout.SOUTH);
frame.setTitle(DBConfig.appName);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setMinimumSize(new Dimension(600, 400));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
Any help will be much appreciated. Thanks a lot in advance.
The BorderLayout supports only one component per region and you add navbtnPnl and tblPnl to the SOUTH region of bottomPnl, and tblPnl is the last one added which replaces the previously added navbtnPnl component.
Also you should not put your JTable directly in a JPanel if you want your table header to be displayed. You should add it to a JScrollPane instead which will display the header properly.
You are using wrong layout. Use GridBagLayout. i tried it, Here is partial working example
public class UI
{
static JTextField inputText = new JTextField(20);
static JLabel errMsgLbl = new JLabel();
static PreparedStatement ptsmt;
static ResultSet rs;
static JButton nameButton;
static JButton accnoButton;
static JButton countryButton;
static JButton altaccButton;
static JButton showAllButton;
static JButton clearButton;
static JButton exitButton;
static JButton prevButton;
static JButton nextButton;
static JLabel searchBy;
static String[] columnNames = {"ID", "Name", "Original Name", "Account Number", "Country", "Alternate Account No",
"Related Party Name", "Select"};
static JTable table;
static JComponent createHorizontalSeparator()
{
JSeparator x = new JSeparator(SwingConstants.HORIZONTAL);
x.setForeground(Color.BLACK);
x.setPreferredSize(new Dimension(50, 3));
return x;
}
public static void main(String args[])
{
try
{
// DBConfig.loadEngineConfiguration();
}
catch (Exception ex)
{
ex.printStackTrace();
}
JFrame frame = new JFrame();
frame.setLayout(new GridBagLayout());
final JTable table = new JTable(50, 8);
JPanel topPnl = new JPanel(new BorderLayout());
JPanel cntrPnl = new JPanel(new GridLayout(4, 1, 0, 0));
JPanel bottomPnl = new JPanel(new BorderLayout());
JPanel hdrPnl = new JPanel((LayoutManager) new FlowLayout(FlowLayout.LEFT));
JPanel srchPnl = new JPanel((LayoutManager) new FlowLayout(FlowLayout.LEADING));
JPanel topBtnPnl = new JPanel((LayoutManager) new FlowLayout(FlowLayout.LEADING));
JPanel bottombtnPnl = new JPanel((LayoutManager) new FlowLayout(FlowLayout.LEADING));
JPanel navbtnPnl = new JPanel((LayoutManager) new FlowLayout(FlowLayout.TRAILING));
JPanel tblPnl = new JPanel((LayoutManager) new FlowLayout(FlowLayout.CENTER));
hdrPnl.add(new JLabel("Welcome"));
srchPnl.add(new JLabel("Input text"));
srchPnl.add(inputText);
topPnl.add(hdrPnl, BorderLayout.NORTH);
topPnl.add(srchPnl, BorderLayout.SOUTH);
searchBy = new JLabel("searchBy");
nameButton = new JButton("nameBtn");
accnoButton = new JButton("accNoBtn");
countryButton = new JButton("countryBtn");
altaccButton = new JButton("altAccNoBtn");
showAllButton = new JButton("showBtn");
clearButton = new JButton("clearBtn");
exitButton = new JButton("exitBtn");
prevButton = new JButton("prevBtn");
nextButton = new JButton("nextBtn");
clearButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
inputText.setText(null);
errMsgLbl.setText(null);
}
});
exitButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Container frame = exitButton.getParent();
do
frame = frame.getParent();
while (!(frame instanceof JFrame));
((JFrame) frame).dispose();
}
});
topBtnPnl.add(searchBy);
topBtnPnl.add(nameButton);
topBtnPnl.add(accnoButton);
topBtnPnl.add(countryButton);
topBtnPnl.add(altaccButton);
bottombtnPnl.add(showAllButton);
bottombtnPnl.add(clearButton);
bottombtnPnl.add(exitButton);
cntrPnl.add(topBtnPnl);
cntrPnl.add(bottombtnPnl);
cntrPnl.add(createHorizontalSeparator(), BorderLayout.SOUTH);
navbtnPnl.add(prevButton);
navbtnPnl.add(nextButton);
table.getTableHeader().setReorderingAllowed(false);
cntrPnl.add(table, BorderLayout.PAGE_END);
bottomPnl.add(navbtnPnl, BorderLayout.CENTER);
bottomPnl.add(tblPnl, BorderLayout.CENTER);
// table.setTableHeader(columnNames);
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1;
// c.weighty = 10;
c.gridx = 0;
c.gridy = 0;
frame.add(topPnl, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40; // make this component tall
c.weightx = 0.2;
c.weighty = 80;
c.weightx = 0.0;
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 1;
frame.add(cntrPnl, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.weighty = 0;
c.gridx = 0;
c.gridy = 2;
frame.add(bottomPnl, c);
frame.setTitle("appName");
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setMinimumSize(new Dimension(600, 400));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}